Why Nest.JS doesn't support relative import?
When I run yarn dev, nodaemon find easily all the dependencies with the relative import.
But, when I run yarn build and node dist/main.js, I've got this error:
Error: Cannot find module '..........'
I want to import with:
import { test } from 'tools/test';
and not with:
import { test } from '../../../../../tools/test';
Here's my tsconfig.json file:
{
"compilerOptions": {
"module": "commonjs",
"declaration": false,
"noImplicitAny": false,
"removeComments": true,
"noLib": false,
"allowSyntheticDefaultImports": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"target": "es6",
"sourceMap": true,
"allowJs": true,
"outDir": "dist",
"baseUrl": "src",
"paths": {
"tools/*": [ "tools/*" ]
}
},
"include": [
"src/**/*"
],
"exclude": [
"node_modules",
"**/*.spec.ts"
]
}
This issue isn't related to Nest. Duplicate of #986
It's working! Thank you very much! 馃憤
I've just created a file at the root of the project with this name: tsconfig-paths-bootstrap.js
I put this content into it:
const tsConfig = require('./tsconfig.json');
const tsConfigPaths = require('tsconfig-paths');
const baseUrl = './dist'; // Either absolute or relative path. If relative it's resolved to current working directory.
tsConfigPaths.register({
baseUrl,
paths: tsConfig.compilerOptions.paths,
});
Then, I've edited the start:prod script from package.json to look like this:
"start:prod": "node -r ./tsconfig-paths-bootstrap.js dist/main.js",
馃敟
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Most helpful comment
It's working! Thank you very much! 馃憤
I've just created a file at the root of the project with this name:
tsconfig-paths-bootstrap.jsI put this content into it:
Then, I've edited the
start:prodscript from package.json to look like this: