Problem with module resolution

Asked

Viewed 1,143 times

0

I am trying to create a mapping in a project that is running with Nestjs. But when executing the command npm run start:dev Node returns the error Cannot find module '@entities/user' on the console.

I have done several searches and could not locate the problem. Can anyone help me? I noticed that the command npm run start:dev executes the following instruction: concurrently --handle-input \"wait-on dist/main.js && nodemon\" \"tsc -w -p tsconfig.build.json\".

tsconfig.build.json file

{
  "extends": "./tsconfig.json",
  "exclude": ["node_modules", "test", "dist", "**/*spec.ts"]
}

tsconfig.json file

{
  "compilerOptions": {
    "module": "commonjs",
    "declaration": true,
    "removeComments": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "target": "es6",
    "sourceMap": true,
    "outDir": "./dist",
    "baseUrl": ".",
    "incremental": true,
    "paths": {
      "@entities-services/*": ["src/modules/database/services/*"],
      "@entities/*": ["src/entities/*"],
      "@modules/*": ["src/modules/*"],
    }
  },
  "exclude": ["node_modules"]
}

Error returned

module.js:549
[0]     throw err;
[0]     ^
[0]
[0] Error: Cannot find module '@entities/user'
[0]     at Function.Module._resolveFilename (module.js:547:15)
[0]     at Function.Module._load (module.js:474:25)
[0]     at Module.require (module.js:596:17)
[0]     at require (internal/module.js:11:18)
[0]     at Object.<anonymous> (C:\Users\hiago\Documents\Development\Jewels\api\dist\modules\database\services\user.service.js:18:16)
[0]     at Module._compile (module.js:652:30)
[0]     at Object.Module._extensions..js (module.js:663:10)
[0]     at Module.load (module.js:565:32)
[0]     at tryModuleLoad (module.js:505:12)
[0]     at Function.Module._load (module.js:497:3)
[0] [nodemon] app crashed - waiting for file changes before starting...
[1]
[1] 21:06:08 - Found 0 errors. Watching for file changes.

package.json file

{
  "name": "api",
  "version": "0.0.1",
  "description": "",
  "author": "",
  "license": "MIT",
  "scripts": {
    "build": "tsc -p tsconfig.build.json",
    "format": "prettier --write \"src/**/*.ts\"",
    "start": "ts-node -r tsconfig-paths/register src/main.ts",
    "start:dev": "concurrently --handle-input \"wait-on dist/main.js && nodemon\" \"tsc -w -p tsconfig.build.json\" ",
    "start:debug": "nodemon --config nodemon-debug.json",
    "generate:orm": "node generate-orm.js",
    "typeorm-model-generator": "typeorm-model-generator",
    "prestart:prod": "rimraf dist && npm run build",
    "start:prod": "node dist/main.js",
    "lint": "tslint -p tsconfig.json -c tslint.json",
    "test": "jest",
    "test:watch": "jest --watch",
    "test:cov": "jest --coverage",
    "test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
    "test:e2e": "jest --config ./test/jest-e2e.json"
  },
  "dependencies": {
    "@nestjs/common": "^6.0.0",
    "@nestjs/core": "^6.0.0",
    "@nestjs/platform-express": "^6.0.0",
    "@nestjs/typeorm": "^6.1.2",
    "dotenv": "^8.0.0",
    "mysql": "^2.17.1",
    "reflect-metadata": "^0.1.12",
    "rimraf": "^2.6.2",
    "rxjs": "^6.3.3",
    "typeorm": "^0.2.18"
  },
  "devDependencies": {
    "@nestjs/testing": "^6.0.0",
    "@types/dotenv": "^6.1.1",
    "@types/express": "^4.16.0",
    "@types/jest": "^23.3.13",
    "@types/node": "^10.12.18",
    "@types/supertest": "^2.0.7",
    "concurrently": "^4.1.0",
    "jest": "^23.6.0",
    "nodemon": "^1.18.9",
    "prettier": "^1.15.3",
    "supertest": "^3.4.1",
    "ts-jest": "24.0.2",
    "ts-node": "8.1.0",
    "tsconfig-paths": "3.8.0",
    "tslint": "5.16.0",
    "typeorm-model-generator": "^0.3.4",
    "typescript": "3.4.3",
    "wait-on": "^3.2.0"
  },
  "jest": {
    "moduleFileExtensions": [
      "js",
      "json",
      "ts"
    ],
    "rootDir": "src",
    "testRegex": ".spec.ts$",
    "transform": {
      "^.+\\.(t|j)s$": "ts-jest"
    },
    "coverageDirectory": "../coverage",
    "testEnvironment": "node"
  }
}
  • Can you pass the repository link please? That way, I can do some tests...

1 answer

1


This happens because you are using pathcustom s, as evidenced in your Typescript configuration:

"paths": {
  "@entities-services/*": ["src/modules/database/services/*"],
  "@entities/*": ["src/entities/*"],
  "@modules/*": ["src/modules/*"]
}

Unfortunately, so far, Typescript is not able to solve this by itself. Thus, if you are using Node.js, you should turn to other modules.

The most advanced solution would be using Webpack in conjunction with Typescript, or Rollup. However, to make the answer simpler, we will use libraries that require a simpler configuration (only those who worked with Webpack know how boring it is).

Guidebook:

Install the dependency module-alias:

npm i --save module-alias

Add the following field to your file package.json:

"_moduleAliases": {
  "@entities-services": "dist/modules/database/services",
  "@entities": "dist/entities",
  "@modules": "dist/modules"],
}

And add the import then in the file that is responsible for startup of the application (entry point):

import 'module-alias/register';

Reference:

  • I was making a mistake... But your answer showed me the way. In "_moduleAliases" you don’t need the /* or the vectors. They should only be string’s. You can correct that I will put as correct.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.