首页 > 解决方案 > 指向 TS 文件的 TypeScript 构建错误

问题描述

当我运行 nodejs 项目的 typescript 构建并在构建主文件上运行 node 时,我收到以下错误:

HOME_PATH/PROJECT/src/entity/CEP.ts:1
import { Entity, ObjectIdColumn, ObjectID, Column } from 'typeorm';
^^^^^^

SyntaxError: Cannot use import statement outside a module

使用的命令:

我可以看到 build 正在尝试执行 .ts 文件,例如尝试访问 build 文件夹之外的文件。

tsconfig.json

{
  "compilerOptions": {
    "target": "es6" /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019' or 'ESNEXT'. */,
    "module": "CommonJS" /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */,
    "lib": [
      "es6"
    ] /* Specify library files to be included in the compilation. */,
    "allowJs": true /* Allow javascript files to be compiled. */,
    "outDir": "build" /* Redirect output structure to the directory. */,
    "strict": true /* Enable all strict type-checking options. */,
    "noImplicitAny": true /* Raise error on expressions and declarations with an implied 'any' type. */,
    "moduleResolution": "node" /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */,
    "baseUrl": "." /* Base directory to resolve non-absolute module names. */,
    "esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */,
    "experimentalDecorators": true /* Enables experimental support for ES7 decorators. */,
    "emitDecoratorMetadata": true /* Enables experimental support for emitting type metadata for decorators. */,
    "resolveJsonModule": true /* Include modules imported with '.json' extension */
  },
  "include": ["src/**/*"]
}

包.json

{
  "name": "nodets",
  "version": "1.0.0",
  "main": "build/index.js",
  "license": "MIT",
  "scripts": {
    "start": "node build/index.js",
    "dev": "ts-node-dev --respawn --transpile-only src/index.ts --watch src",
    "build": "rimraf ./build && npx tsc",
    "lint": "eslint . --ext .ts",
    "fix": "eslint . --ext .ts --fix",
    "prettier": "prettier --config .prettierrc 'src/**/*.ts' --write",
    "test": "jest"
  },
  "devDependencies": {
    "@types/cors": "^2.8.10",
    "@types/express": "^4.17.11",
    "@types/jest": "^26.0.23",
    "@types/node": "^14.14.20",
    "@types/supertest": "^2.0.11",
    "@typescript-eslint/eslint-plugin": "^4.12.0",
    "@typescript-eslint/parser": "^4.12.0",
    "eslint-config-prettier": "^7.1.0",
    "eslint-plugin-prettier": "^3.3.1",
    "jest": "^26.6.3",
    "prettier": "^2.2.1",
    "rimraf": "^3.0.2",
    "supertest": "^6.1.3",
    "ts-jest": "^26.5.5",
    "ts-node-dev": "^1.1.6",
    "typescript": "^4.1.3"
  },
  "dependencies": {
    "axios": "^0.21.1",
    "cors": "^2.8.5",
    "dotenv": "^8.2.0",
    "express": "^4.17.1",
    "mongodb": "^3.6.6",
    "reflect-metadata": "^0.1.10",
    "typeorm": "0.2.32"
  }
}

我试图添加"type": "module"到 package.json 并--experimental-modules返回错误,例如exports is not defined

当我运行与 src 代码 () 隔离的构建主文件时,cd build && node index.js它无法访问 ormconfig.json 和 .env 以获取环境变量和 typeorm 数据库配置。是的,我可以构建一个复制 env 文件并删除源代码的 docker 环境,但我必须了解为什么会发生这种情况。

标签: node.jstypescriptbuild

解决方案


推荐阅读