【发布时间】:2021-02-04 09:52:49
【问题描述】:
TLDR; 应用构建但尝试从 src 文件夹而不是 dist 导入文件
我有一个使用 TypeScript 构建的 Express 应用。
这是 tsconfig.json
{
"compilerOptions": {
"target": "es5", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */
"module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */
"sourceMap": true, /* Generates corresponding '.map' file. */
"outDir": "dist", /* Redirect output structure to the directory. */
"strict": false, /* Enable all strict type-checking options. */
"noImplicitAny": false, /* 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": "./dist", /* Base directory to resolve non-absolute module names. */
"paths": {
"*": [
"node_modules/*"
]
}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
"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. */
"forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */
},
"include": [
"src/**/*",
"custom.d.ts"
]
}
这些是我的 npm 脚本:
"scripts": {
"clean": "rimraf dist/*",
"dev": "nodemon",
"tsc": "tsc",
"start": "node dist/index.js",
"build": "npm-run-all clean tsc",
},
npm run build 工作正常,一切看起来都不错,但当我运行 npm run start 时,我收到以下错误:
/app/server/src/entity/Category.ts:1
import {Entity, PrimaryGeneratedColumn, Column, ManyToMany, BaseEntity} from "typeorm";
^^^^^^
SyntaxError: Cannot use import statement outside a module
错误似乎是我的构建出于某种原因似乎是从 src 文件夹而不是从构建文件夹导入的。我不知道出了什么问题,感谢任何指导或想法,以帮助我走得更远。
【问题讨论】:
-
我的错误。我错误地配置了 TypeORM。使用绝对路径运行时,它按预期工作。
标签: node.js typescript express typeorm