Search code examples
nestjstypeorm

npm run start:dev cannot find module


npm run start works fine, but npm run start: dev throws this error:

7:14:22 PM - Found 0 errors. Watching for file changes.
internal/modules/cjs/loader.js:638
throw err;
^
Error: Cannot find module 'src/models/note.model'
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:636:15)
at Function.Module._load (internal/modules/cjs/loader.js:562:25)
at Module.require (internal/modules/cjs/loader.js:692:17)
at require (internal/modules/cjs/helpers.js:25:18)
at Object.<anonymous> (C:\Users\Anuitex-169\Desktop\VersF\nest\server\dist\note\note.service.js:17:22)
at Module._compile (internal/modules/cjs/loader.js:778:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)

This error appeared from the very beginning of my project, and I can’t understand why. I decided to forget about this problem since npm run start works well. But somehow uncomfortable (

My app.module.ts My package.json file:

    {
      "name": "server",
      "version": "0.0.1",
      "description": "",
      "author": "",
      "license": "MIT",
      "scripts": {
        "build": "rimraf dist && tsc -p tsconfig.build.json",
        "format": "prettier --write \"src/**/*.ts\"",
        "start": "ts-node -r tsconfig-paths/register src/main.ts",
        "start:dev": "tsc-watch -p tsconfig.build.json --onSuccess \"node dist/main.js\"",
        "start:debug": "tsc-watch -p tsconfig.build.json --onSuccess \"node --inspect-brk dist/main.js\"",
        "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.3",
        "mongodb": "^3.3.2",
        "path": "^0.12.7",
        "reflect-metadata": "^0.1.12",
        "rimraf": "^2.6.2",
        "rxjs": "^6.3.3",
        "typeorm": "^0.2.19"
      },
      "devDependencies": {
        "@nestjs/testing": "^6.0.0",
        "@types/express": "4.16.1",
        "@types/jest": "24.0.11",
        "@types/multer": "^1.3.10",
        "@types/node": "11.13.4",
        "@types/supertest": "2.0.7",
        "jest": "24.7.1",
        "prettier": "1.17.0",
        "supertest": "4.0.2",
        "ts-jest": "24.0.2",
        "ts-node": "8.1.0",
        "tsc-watch": "2.2.1",
        "tsconfig-paths": "3.8.0",
        "tslint": "5.16.0",
        "typescript": "3.4.3"
      },
      "jest": {
        "moduleFileExtensions": [
          "js",
          "json",
          "ts"
        ],
        "rootDir": ".",
        "roots": [
          "<rootDir>/src/",
          "<rootDir>/libs/",
          "<rootDir>/apps/"
        ],
        "testRegex": ".spec.ts$",
        "transform": {
          "^.+\\.(t|j)s$": "ts-jest"
        },
        "coverageDirectory": "./coverage",
        "testEnvironment": "node"
      }
    }

Please help me deal with this problem.


Solution

  • Your AppModule looks fine, so I have to wonder if something is happening in your NoteService, which this line makes it looks like that is the case at Object.<anonymous> (C:\Users\Anuitex-169\Desktop\VersF\nest\server\dist\note\note.service.js:17:22).

    If I had to bet, you are trying to read the module from your src directory while you are in your dist directory (at least by standard Typescript compilation). Instead of using an import like src/models/note.model you should use an import that takes into account the path from the current file (such as ../models/note.model). You may need to change that a bit more, but that's the idea of what should happen