Nest: [BUG] Relative import

Created on 31 Aug 2018  路  4Comments  路  Source: nestjs/nest

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"
  ]
}

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.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",

All 4 comments

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

rafal-rudnicki picture rafal-rudnicki  路  3Comments

VRspace4 picture VRspace4  路  3Comments

artaommahe picture artaommahe  路  3Comments

marshall007 picture marshall007  路  3Comments

rlesniak picture rlesniak  路  3Comments