I have this on my tsconfig.json
"baseUrl": "app",
"paths": {
"~/*": ["*"]
}
This allows me to import any module as following
import { Foo } from '~/auth'
This works, and tsc compiles without any errors.
However, when I try to run it using node, I get the error
Error: Cannot find module '~/auth'
What can be? I think something is missing because I used to do the same on Next project.
Here is my full tsconfig.json
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"skipLibCheck": true,
"experimentalDecorators": true,
"strictPropertyInitialization": false,
"emitDecoratorMetadata": true,
"outDir": "dist",
"baseUrl": "app",
"paths": {
"~/*": ["*"]
}
},
"include": ["app/**/*.ts"],
"exclude": ["node_modules"]
}
CodePudding user response:
This error occurs because custom Typescript import paths are not replaced at build time by Typescript and cannot be natively resolved by Node.js.
You must have some kind of transformation that happen at build time, whether using a build tool like Webpack, or some library like tsc-alias (I strongly recommend this option).