网站搜索

node.js - 无法编译 TypeScript 项目


在运行 TypeScript 文件时我遇到了错误。运行 npm start 脚本后,我得到了以下错误。

    return new TSError(diagnosticText, diagnosticCodes)
           ^
TSError: ⨯ Unable to compile TypeScript:
src/config/index.ts:4:23 - error TS2307: Cannot find module 'path'.

4 import * as path from "path";
                        ~~~~~~
src/config/index.ts:17:37 - error TS2304: Cannot find name '__dirname'.

17             .file({ file: path.join(__dirname, "/appConfig.json") });
                                       ~~~~~~~~~
src/config/index.ts:44:45 - error TS2304: Cannot find name '__dirname'.

44             this.sequelizeClient.addModels([__dirname + "../entities/*.js", __dirname + "../entities/*.ts"]);

tsconfig.json

{
  "compilerOptions": {
    "target": "es2015",
    "lib": ["es2015"],
    "types": ["reflect-metadata"],
    "module": "commonjs",
    "moduleResolution": "node",
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true,
    "allowSyntheticDefaultImports": true,
    "sourceMap": true,
    "declaration": false
  },
  "exclude": ["node_modules"]
}

package.json

{"scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "nodemon --watch \"**/*.ts\" --ignore 'node_modules/**/*' --exec ts-node src/localInit.ts"
  },
  "dependencies": {
    "nconf": "^0.10.0",
    "reflect-metadata": "^0.1.13",
    "sequelize": "^5.21.7",
    "sequelize-typescript": "^1.1.0"
  },
  "devDependencies": {
    "@types/bluebird": "^3.5.30",
    "@types/nconf": "^0.10.0",
    "@types/node": "^13.13.2",
    "@types/validator": "^13.0.0",
    "nodemon": "^2.0.3",
    "prettier": "^2.0.5",
    "ts-node": "^8.9.1",
    "tslint": "^6.1.1",
    "tslint-config-prettier": "^1.18.0",
    "typescript": "^3.8.3"
  }
}

我已经安装了 @types/node,但是将类型更改为 node 会抛出另一个错误。我在另一个项目中使用相同的配置,它工作正常。