Error [ERR_MODULE_NOT_FOUND]: Cannot find package 'models' imported from

4.3k Views Asked by At

I'm trying to configure my project to use absolute imports, so I have configured jsconfig.json as follows:

{
    "compilerOptions": {
       "baseUrl": "./src",
       "module": "commonjs",
       "target": "es2016"
    },
   "exclude": ["node_modules", "**/node_modules/*"]
}

My project structure looks like this:

enter image description here

but when I try to do an import like import { Account } from "models"; I get the following error:

Error [ERR_MODULE_NOT_FOUND]: Cannot find package 'models' imported from...

How can I solve this ?

1

There are 1 best solutions below

0
On

The jsconfig.json file is used by VSCode's language server for static JavaScript analysis, it is not read by Node.js and therefore doesn't affect module loading in any way.

Options which come to mind are to use a transpiler, for example tsc, to transpile JS files (see this answer), or use native ESM support in Node.js possibly with custom loader. Mind that by default ESM imports expect full filename, including file extension (omitting file extensions in ESM is TypeScript specific quirk).