20

jsconfigerror is an example repository showing the jsconfig is not working.

I have the following set inside my jsconig.json file:

{
  "compilerOptions": {
    "baseUrl": "./"
  }
}

However, when I do an import, it fails:

./pages/index.js
Module not found: Can't resolve 'components/AThing' in '/var/www/gd.hutuber.com/pages'

Folder Structure

¬ components
   ¬ AThing
¬ pages
   ¬ index.js

pages/index.js

import Head from 'components/AThing'

How can I get baseUrl to work with Next.js?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Jamie Hutber
  • 26,790
  • 46
  • 179
  • 291

7 Answers7

23

From Next.js 9.4 onward, you can easily create the jsconfig.json or *tsconfig.json files in the root of your project and then simply enter following:

{
  "compilerOptions": {
    "baseUrl": "."
  }
}

Next thing you must do is to import your component like following:

import Head from 'components/AThing';

Assuming components folder placed in the root of your project as well.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Hooman
  • 1,775
  • 1
  • 16
  • 15
18

Since Next.js 9.4, Next.js automatically supports the tsconfig.json and jsconfig.json "paths" and "baseUrl" options.

Read more in the official documentation.


For prior versions, Next.js doesn't read the configurations written in file jsconfig.json. You have to customize the Webpack configuration the Next.js way.

Create a file named next.config.js at the root directory of your project next to file package.json. Then restart.

const path = require('path')

module.exports = {
  webpack: config => {
    config.resolve.modules.push(path.resolve('./'))

    return config
  }
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Pengson
  • 845
  • 5
  • 10
  • 7
    While correct at some point, since Next 9.4 both `tsconfig.json` and `jsconfig.json` file `compilerOptions.baseUrl` and `compilerOptions.paths` are supported: https://nextjs.org/docs/advanced-features/module-path-aliases (as shared in [this answer](https://stackoverflow.com/a/61838859/2224331)) – SidOfc Feb 14 '21 at 13:03
  • 1
    @SidOfc Thanks for letting me know. – Pengson Nov 04 '21 at 08:17
9

Before starting, you should check that your Next.js version is at least 9.4.

When you configure the jsconfig.json, you should kill the process and restart.

    {
    "compilerOptions": {
      "baseUrl": ".",
      "paths": {
        "@components/*": ["components/*"],
        "@pages/*": ["pages/*"],
      }
    }
  }

While baseUrl is useful and it is enough for most of the cases, you can use the prefix @ to be clear that is an aliases folder.

Be sure that, when you import something, you do not start with ./, like ./pages/index.js, because you are breaking the absolute alias and start to import in relative mode.

import Head from '@components/AThing'
Pablopvsky
  • 209
  • 2
  • 5
3

Create file jsconfig.json in the root folder and add this:

{
  "compilerOptions": {
    "baseUrl": "."
  }
}

Then "npm run dev" or "yarn dev".

If you have the 'next dev' running when you define a new path, make sure to restart 'next dev'.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
1

Just to clarify the fragments here. JSconfig and absolute paths are supported with all modern iterations of Next.js.

  1. Create the configuration file:

    jsconfig.json
    
  2. Specify you base URL:

    {
      "compilerOptions": {
        "baseUrl": "."
      }
    }
    
  3. Change your imports like so:

    import 'components/header'
    
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
0

Try:

{
  "compilerOptions": {
    "baseUrl": "src"
  }
}
Michael Rovinsky
  • 6,807
  • 7
  • 15
  • 30
Shyamm24
  • 19
  • 1
  • 2
-1

This seem to work for me in next v12.3.0

jsconfig.json

{
  "compilerOptions": {
    "baseUrl": "."
  }
}

in component

import Navigation from "src/components/navigation/Navigation";

enter image description here

atazmin
  • 4,757
  • 1
  • 32
  • 23