node.js - 指向 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
使用的命令:
- 纱线构建(“rimraf ./build && npx tsc”)
- 纱线开始(“节点构建/index.js”)
我可以看到 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 环境,但我必须了解为什么会发生这种情况。
解决方案
推荐阅读
- python - 如何使用 SQLAlchemy 插入复合主键?
- java - Api Java ElasticSearch | 通过传递列表并返回所有匹配项进行搜索
- encryption - 确保已部署软件的公钥安全
- javascript - 子类的父方法未被调用 React
- angular8 - “无法读取未定义的属性'组件'”,同时通过 Angular 8 中的 params.context 从另一个组件调用一个组件的方法
- shopware - StorefrontRenderEvent::class 事件
- r - R - 重新排序数据框保持第一列
- django - 编辑部分/站点 Django 时限制管理员中的 ForeignKey 字段
- laravel - Vue 条纹元素的 createPaymentMethod 在 Laravel 中不起作用,TypeError: Object(...) is not a function 发生错误
- django - 尽管使用了正确的共享访问签名值,但 Azure 服务总线消息间歇性地未经授权......为什么?