0

I'm studying the jsconfig.json file, the documentation doesn't mention explicitly whether it works with "import" and "require", or just "import".

This is the jsconfig.json I created (for Express project):

{
  "compilerOptions": {
    "baseUrl": "./",
    "module": "CommonJS",
    "target": "es2020",
    "paths": {
      "@/controllers/*": ["controllers/*"],
    }
  },
  "exclude": ["node_modules", "**/node_modules/*"]
}

I can't do like this:

const userController = require('@/controllers/user-controller');
// Error: Cannot find module '@/controllers/user-controller'

Why can't I use "require"? I Tried it in React and it worked with "import".

R.M. Reza
  • 725
  • 1
  • 8
  • 20
  • 1
    This likely has nothing to do with `require` vs `import`. Check that `@/controllers/user-controller` is installed in the Node project that that code is running in. Babel or a similar tool is likely compiling that module automatically with the React portion of your project. – Michael Rothkopf Oct 10 '21 at 03:40
  • Related: https://stackoverflow.com/questions/68675994/what-is-jsconfig-json – Anson Kao Mar 29 '22 at 17:42
  • Did you solve this problem? Because I have the same problem. – Joe199382 Jun 16 '22 at 13:31

0 Answers0