Nest: compiled js path resolution error

Created on 26 Nov 2018  路  8Comments  路  Source: nestjs/nest

On ubuntu 18.04 with nodejs 8,
with this tsconfig.json

{
  "compilerOptions": {
    "module": "commonjs",
    "declaration": true,
    "noImplicitAny": false,
    "removeComments": true,
    "noLib": false,
    "allowSyntheticDefaultImports": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "target": "es6",
    "sourceMap": true,
    "outDir": "./dist",
    "baseUrl": "./src"
  },
  "include": [
    "src/**/*"
  ],
  "exclude": [
    "node_modules",
    "**/*.spec.ts"
  ]
}

i have this class on my scr/domain/service/realtyModel.service.ts

import { Repository } from 'typeorm';
import { Realty } from 'domain/model/realty.entity';
import { Injectable, NotFoundException } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { User } from 'domain/model/user.entity';

@Injectable()
export class RealtyModelService {

i algo got the entity class on i have this class on my scr/domain/model/user.entity.ts

On my packages.json i got this
```json
  "scripts": {
    "format": "prettier --write \"src/**/*.ts\"",
    "start": "ts-node -r tsconfig-paths/register -r dotenv/config src/main.ts",
    "start:dev": "nodemon",
    "start:debug": "nodemon --config nodemon-debug.json",
    "prestart:prod": "rimraf dist && tsc",
    "start:prod": "node -r tsconfig-paths/register -r dotenv/config dist/main.js",
    "start:hmr": "node -r tsconfig-paths/register -r dotenv/config dist/server",
    "lint": "tslint -p tsconfig.json -c tslint.json",
    "test": "jest",
    "test:watch": "jest --watch",
    "test:cov": "jest --coverage",
    "test:e2e": "jest --config ./test/jest-e2e.json",
    "webpack": "webpack --config webpack.config.js"
  },

when i run yarn run start:dev, it runs fine, however if i run start:prod i get the following error:

Error: Cannot find module 'domain/model/realty.entity'
    at Function.Module._resolveFilename (module.js:548:15)
    at Function.Module._resolveFilename (/home/jhonatan/projects/api-facs/node_modules/tsconfig-paths/lib/register.js:75:40)
    at Function.Module._load (module.js:475:25)
    at Module.require (module.js:597:17)
    at require (internal/module.js:11:18)
    at Object.<anonymous> (/home/jhonatan/projects/api-facs/dist/domain/service/realtyModel.service.js:16:25)
    at Module._compile (module.js:653:30)
    at Object.Module._extensions..js (module.js:664:10)
    at Module.load (module.js:566:32)
    at tryModuleLoad (module.js:506:12)
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
error Command failed with exit code 1.

What am i doing wrong?

Most helpful comment

@marcus-sa we were all beginners.. Keep it civil..

@jhonatanTeixeira
Reading through your issue again I saw you've added the paths to the mentioned files.
Therefore your important statement in the file src/domain/service/realtyModel.service.ts
should look like this:

import { Realty } from '../model/realty.entity';
import { User } from '../model/user.entity';

import { Repository } from 'typeorm';
import { Injectable, NotFoundException } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';

[...]

This will hopefully solve your issue. As we already mentioned this is not a problem with Nest nor the CLI skeleton.
These import statements were added by you, and since they're incorrect your program failed. Maybe go a step back and first try to learn NodeJS & then TypeScript before you start with NestJS. Knowledge of this are the minimum prerequisites for this framework.

All 8 comments

Please use StackOverflow for non related Nest questions.

https://stackoverflow.com/a/43330003

However, im using the skeleton created by the nest cli, shouldn't it work out of the box? Isn't it a bug on the skeleton generator somehow?

@jhonatanTeixeira
The problem lies in these two import statements:

import { Realty } from 'domain/model/realty.entity';
import { User } from 'domain/model/user.entity';

TypeScript would try to look up this path in node_modules/domain/model/user.entity.
But I am guessing you鈥檙e trying to import these entity relative from the import statement. For that you need to prepend a ./ so TypeScript knows it needs to resolve the module relatively.

import { Realty } from './domain/model/realty.entity';
import { User } from './domain/model/user.entity';

If this does not solve your issue, would need more information about your project. Nonetheless I am 99% sure this is not a NestJS problem, so please in the future use StackOverflow for these questions.

Again... My project is using the nestjs cli generated solution... All build scripts were generated by the tool. Shouldnt it work without me having to look through stack overflow? Isnt it why people use generators? So they dont need to sort that kind if problem out?

No it shouldn't, since the domain path resolution is something you added yourself.
This isn't a Nest problem @BrunnerLivio its just him being lazy and not knowing the difference between his project and what generators are capable of.

@marcus-sa we were all beginners.. Keep it civil..

@jhonatanTeixeira
Reading through your issue again I saw you've added the paths to the mentioned files.
Therefore your important statement in the file src/domain/service/realtyModel.service.ts
should look like this:

import { Realty } from '../model/realty.entity';
import { User } from '../model/user.entity';

import { Repository } from 'typeorm';
import { Injectable, NotFoundException } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';

[...]

This will hopefully solve your issue. As we already mentioned this is not a problem with Nest nor the CLI skeleton.
These import statements were added by you, and since they're incorrect your program failed. Maybe go a step back and first try to learn NodeJS & then TypeScript before you start with NestJS. Knowledge of this are the minimum prerequisites for this framework.

Thanks @BrunnerLivio for your friendly answer. @jhonatanTeixeira please, let's use StackOverflow for such things.

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

mishelashala picture mishelashala  路  3Comments

JulianBiermann picture JulianBiermann  路  3Comments

menme95 picture menme95  路  3Comments

cojack picture cojack  路  3Comments

thohoh picture thohoh  路  3Comments