npm run start:dev找不到模块

时间:2019-10-17 17:04:02

标签: nestjs typeorm

npm运行启动工作正常,但是npm运行启动:开发人员抛出此错误:

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)

这个错误从我的项目开始就出现了,我不明白为什么。我决定忘记此问题,因为npm run start效果很好。但是有点不舒服(

My app.module.ts 我的package.json文件:

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

请帮助我解决这个问题。

1 个答案:

答案 0 :(得分:0)

您的AppModule看起来不错,所以我想知道您的NoteService中是否发生了某些情况,这一行使得at Object.<anonymous> (C:\Users\Anuitex-169\Desktop\VersF\nest\server\dist\note\note.service.js:17:22)就是这种情况。

如果我必须打赌,那么您在src目录中时,尝试从dist目录中读取模块(至少通过标准Typescript编译)。不要使用像src/models/note.model这样的导入方式,而应使用考虑到当前文件(例如../models/note.model)路径的导入方式。您可能需要再进行一些更改,但这就是应该发生的想法