3

I created a file in my route called jsconfig.json:

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

I have the latest nextjs "next": "latest"

Currently to import components, i have to do something like this:

import AppHeader from "../../../../components/common/AppHeader";

However with those changes above i thought I could do this:

import AppHeader from "src/components/common/AppHeader";

But I get this message:

Module not found: Can't resolve 'src/components/common/AppHeader'

Directory structure:

/
-->lib
-->src
   -->components
-->public

Any ideas? I also tried adding this to my next.config.js file but also doesnt make a difference: config.resolve.modules.push(__dirname)

strangeQuirks
  • 3,639
  • 6
  • 31
  • 51
  • Can I know about your folder structure? I guess you can try replace `src/components/common/AppHeader` by `components/common/AppHeader` – nghiaht Oct 17 '20 at 02:13
  • I updated my question with that. I tried what you mentioned and that says module not installed. I would also like to access things in lib using absolute paths. – strangeQuirks Oct 17 '20 at 09:02
  • 1
    I ended up just going with this solution using webpack: https://stackoverflow.com/a/59542342/1009698 `config.resolve.modules.push(path.resolve('./'))` – strangeQuirks Oct 17 '20 at 10:07
  • Does this answer your question? [Using baseUrl in jsconfig.json not working with NextJS](https://stackoverflow.com/questions/59474480/using-baseurl-in-jsconfig-json-not-working-with-nextjs) – juliomalves Dec 25 '21 at 22:16

5 Answers5

2

Seems your folder structure is :

root->src->components

add another property in your jsconfig file

{

   "compilerOptions": {
     "baseUrl": "."
    },
 
    "include":["."]

}
cigien
  • 55,661
  • 11
  • 60
  • 99
Tahid
  • 31
  • 5
0

Try to use my tsconfig.json, it works for me:

{
  "compilerOptions": {
    "module": "commonjs",
    "declaration": true,
    "removeComments": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "allowSyntheticDefaultImports": true,
    "target": "es2017",
    "sourceMap": true,
    "outDir": "./dist",
    "baseUrl": "./",
    "incremental": true,
    "resolveJsonModule": true
  }
}

It also could be cause of running script if you running it in production mode In my case i use:

"build": "nest build"
"start:prod": "node dist/src/main"

0

Remove src folder and put components directly in your source folder, then do following in jsconfig.json:

{
"compilerOptions": {
    "baseUrl": ".",
    "paths": {
      "src/components/*": ["components/*"],
      "src/lib/*": ["lib/*"]
    }
  },
  "exclude": [
    "node_modules"
  ]
}

Now You can reach your folders like this:

src/components/..
src/lib/..
Vladyslav Semeniuk
  • 389
  • 3
  • 5
  • 14
0

Install @types with npm install --save-dev typescript @types/react command.

Your tsconfig.json file should be something like that:

{
  "compilerOptions": {
    "baseUrl": ".",
    "module": "commonjs",
    "target": "es5",
    "sourceMap": true,
    "paths": {
      "@/components/*": [
        "components/*"
      ],
      "@/public/*": [
        "public/*"
      ]
    },
    "lib": [
      "dom",
      "dom.iterable",
      "esnext"
    ],
    "allowJs": true,
    "skipLibCheck": true,
    "strict": false,
    "forceConsistentCasingInFileNames": true,
    "noEmit": true,
    "incremental": true,
    "esModuleInterop": true,
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "jsx": "preserve"
  },
  "include": [
    "."
  ],
  "exclude": [
    "node_modules"
  ]
}
-1

Your configs, worked for me. Just stop and restart the nextJs server.

FATCHOLA
  • 129
  • 1
  • 2