My project is in node/typescript and when I build, I have a dist folder created, which is ok, but when I launch "node dist/index.js" I have an error "Cannot find module" which is inside my dist folder. Here is my organization when I build :
|api
|src
| -controllers
| -services
| -jwt.ts
| -index.ts
|dist
| -controllers
| -services
| -jwt.js
| -index.js
The error I get is :
Error: Cannot find module 'services/jwt'
Here is my tsconfig :
{
"compilerOptions": {
"baseUrl": "./src",
"esModuleInterop": true,
"target": "es5",
"module": "CommonJS",
"lib": ["es6"],
"allowJs": true,
"outDir": "dist",
"rootDir": "src",
"strict": true,
"noImplicitAny": true,
},
"include": ["**/*"],
}
In my service/jwt.ts file is :
import jwt from 'jsonwebtoken';
import jwtDatamapper from 'datamappers/jwt';
export const jwtService = {
some functions
};
I am probably not seeing something, but can't figure out what.
Thanks a lot
You probable did use:
import {jwtService} from "services/jwt";
Instead of:
import {jwtService} from "./services/jwt";
If so, the reason is that when the id of the imported module is not a path - that is something starting with a dot - then it is considered as a Node module. Here, Node searches for a module named "services/jwt" inside one of the node_modules
directory. And doesn't find it.
EDIT:
By the way, you should definitely remove the baseUrl
directive of your tsconfig
manifest. The compiler would have refused to compile your code would this directive not be set, and you would have detected the issue at compile time instead of runtime.
It creates JS that don't work except with AMD module loaders, as explained in the doc:
This feature was designed for use in conjunction with AMD module loaders in the browser, and is not recommended in any other context.