Typeorm 生产与开发 - 模块外的导入语句
Typeorm production vs development - import statement outside a module
我使用 ts-node-dev 使用 NodeJS、typescript 和 TypeORM 设置了一个项目。目前开发工作没有问题,但是当我尝试使用 babel 和 运行 构建它时,我在运行时(构建后)出现以下错误:
SyntaxError: Cannot use import statement outside a module
这发生在 运行 node dist/server.js
之后
我的package.json设置如下:
{
"main": "src/server.ts",
"scripts": {
"start": "node dist/server.js",
"dev": "ts-node-dev --transpile-only --no-notify --ignore-watch node_modules .",
"typeorm": "ts-node-dev -r tsconfig-paths/register ./node_modules/typeorm/cli.js",
"test": "jest",
"build": "babel src --extensions \".ts\" --out-dir dist --copy-files"
},
"author": "Bruno Mello",
"dependencies": {
"cors": "^2.8.5",
"dotenv": "^8.2.0",
"express": "^4.17.1",
"jsonwebtoken": "^8.5.1",
"pg": "^8.5.1",
"reflect-metadata": "^0.1.13",
"tsyringe": "^4.5.0",
"typeorm": "^0.2.31",
"uuid": "^8.3.2"
},
"devDependencies": {
"@babel/cli": "^7.13.14",
"@babel/core": "^7.13.14",
"@babel/node": "^7.13.13",
"@babel/plugin-proposal-class-properties": "^7.13.0",
"@babel/plugin-proposal-decorators": "^7.13.5",
"@babel/preset-env": "^7.13.12",
"@babel/preset-typescript": "^7.13.0",
"@types/cors": "^2.8.10",
"@types/express": "^4.17.11",
"@types/jest": "^26.0.22",
"@types/jsonwebtoken": "^8.5.1",
"@types/node": "^14.14.37",
"@types/uuid": "^8.3.0",
"@typescript-eslint/eslint-plugin": "^4.19.0",
"@typescript-eslint/parser": "^4.19.0",
"babel-plugin-module-resolver": "^4.1.0",
"babel-plugin-transform-typescript-metadata": "^0.3.2",
"eslint": "^7.23.0",
"eslint-config-airbnb-base": "^14.2.1",
"eslint-config-prettier": "^8.1.0",
"eslint-import-resolver-typescript": "^2.4.0",
"eslint-plugin-import": "^2.22.1",
"eslint-plugin-prettier": "^3.3.1",
"jest": "^26.6.3",
"prettier": "2.2.1",
"ts-jest": "^26.5.4",
"ts-node-dev": "^1.1.6",
"tsconfig-paths": "^3.9.0",
"typescript": "^4.2.3"
}
}
我的ormconfig.js:
if (process.env.NODE_ENV === 'development') {
const dotenv = require('dotenv');
dotenv.config();
}
module.exports = {
name: 'default',
type: 'postgres',
host: process.env.DBHOST,
port: process.env.DBPORT,
username: process.env.DBUSER,
password: process.env.DBPASSWORD,
database: process.env.DBNAME,
synchronize: false,
logging: process.env.NODE_ENV === 'development',
entities: ['dist/modules/**/infra/typeorm/models/*.js'],
migrations: ['dist/shared/typeorm/migrations/*.js'],
cli: {
migrationsDir: 'dist/shared/typeorm/migrations',
},
};
我的babel.config.js:
module.exports = {
presets: [
['@babel/preset-env', { targets: { node: 'current' } }],
'@babel/preset-typescript',
],
plugins: [
'babel-plugin-transform-typescript-metadata',
['@babel/plugin-proposal-decorators', { legacy: true }],
['@babel/plugin-proposal-class-properties', { loose: true }],
],
};
和我的tsconfig.json
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"outDir": "./dist",
"strict": true,
"strictPropertyInitialization": false,
"esModuleInterop": true,
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true
}
}
经过一些研究,我发现有人建议我将 orm 配置上的实体和迁移替换为引用 dist 文件夹而不是 src 文件夹,更改自:
entities: ['src/modules/**/infra/typeorm/models/*.ts'],
migrations: ['src/shared/typeorm/migrations/*.ts'],
cli: {
migrationsDir: 'src/shared/typeorm/migrations',
},
至:
entities: ['dist/modules/**/infra/typeorm/models/*.js'],
migrations: ['dist/shared/typeorm/migrations/*.js'],
cli: {
migrationsDir: 'dist/shared/typeorm/migrations',
},
这实际上解决了 dist 代码的问题,但破坏了我的开发环境(使用 ts-node-dev 运行的环境),因此它不是一个有效的选项。
您可以动态更改路径:
ormconfig.js
const baseDir = process.env.NODE_ENV == 'production' ? 'dist/src' : 'src';
module.exports = {
...
entities: [baseDir + '/entity/**/*.{ts,js}'],
migrations: [baseDir + '/migration/**/*.{ts,js}'],
subscribers: [baseDir + '/subscriber/**/*.{ts,js}'],
cli: {
entitiesDir: baseDir + '/entity',
migrationsDir: baseDir + '/migration',
subscribersDir: baseDir + '/subscriber',
},
};
我使用 ts-node-dev 使用 NodeJS、typescript 和 TypeORM 设置了一个项目。目前开发工作没有问题,但是当我尝试使用 babel 和 运行 构建它时,我在运行时(构建后)出现以下错误:
SyntaxError: Cannot use import statement outside a module
这发生在 运行 node dist/server.js
之后我的package.json设置如下:
{
"main": "src/server.ts",
"scripts": {
"start": "node dist/server.js",
"dev": "ts-node-dev --transpile-only --no-notify --ignore-watch node_modules .",
"typeorm": "ts-node-dev -r tsconfig-paths/register ./node_modules/typeorm/cli.js",
"test": "jest",
"build": "babel src --extensions \".ts\" --out-dir dist --copy-files"
},
"author": "Bruno Mello",
"dependencies": {
"cors": "^2.8.5",
"dotenv": "^8.2.0",
"express": "^4.17.1",
"jsonwebtoken": "^8.5.1",
"pg": "^8.5.1",
"reflect-metadata": "^0.1.13",
"tsyringe": "^4.5.0",
"typeorm": "^0.2.31",
"uuid": "^8.3.2"
},
"devDependencies": {
"@babel/cli": "^7.13.14",
"@babel/core": "^7.13.14",
"@babel/node": "^7.13.13",
"@babel/plugin-proposal-class-properties": "^7.13.0",
"@babel/plugin-proposal-decorators": "^7.13.5",
"@babel/preset-env": "^7.13.12",
"@babel/preset-typescript": "^7.13.0",
"@types/cors": "^2.8.10",
"@types/express": "^4.17.11",
"@types/jest": "^26.0.22",
"@types/jsonwebtoken": "^8.5.1",
"@types/node": "^14.14.37",
"@types/uuid": "^8.3.0",
"@typescript-eslint/eslint-plugin": "^4.19.0",
"@typescript-eslint/parser": "^4.19.0",
"babel-plugin-module-resolver": "^4.1.0",
"babel-plugin-transform-typescript-metadata": "^0.3.2",
"eslint": "^7.23.0",
"eslint-config-airbnb-base": "^14.2.1",
"eslint-config-prettier": "^8.1.0",
"eslint-import-resolver-typescript": "^2.4.0",
"eslint-plugin-import": "^2.22.1",
"eslint-plugin-prettier": "^3.3.1",
"jest": "^26.6.3",
"prettier": "2.2.1",
"ts-jest": "^26.5.4",
"ts-node-dev": "^1.1.6",
"tsconfig-paths": "^3.9.0",
"typescript": "^4.2.3"
}
}
我的ormconfig.js:
if (process.env.NODE_ENV === 'development') {
const dotenv = require('dotenv');
dotenv.config();
}
module.exports = {
name: 'default',
type: 'postgres',
host: process.env.DBHOST,
port: process.env.DBPORT,
username: process.env.DBUSER,
password: process.env.DBPASSWORD,
database: process.env.DBNAME,
synchronize: false,
logging: process.env.NODE_ENV === 'development',
entities: ['dist/modules/**/infra/typeorm/models/*.js'],
migrations: ['dist/shared/typeorm/migrations/*.js'],
cli: {
migrationsDir: 'dist/shared/typeorm/migrations',
},
};
我的babel.config.js:
module.exports = {
presets: [
['@babel/preset-env', { targets: { node: 'current' } }],
'@babel/preset-typescript',
],
plugins: [
'babel-plugin-transform-typescript-metadata',
['@babel/plugin-proposal-decorators', { legacy: true }],
['@babel/plugin-proposal-class-properties', { loose: true }],
],
};
和我的tsconfig.json
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"outDir": "./dist",
"strict": true,
"strictPropertyInitialization": false,
"esModuleInterop": true,
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true
}
}
经过一些研究,我发现有人建议我将 orm 配置上的实体和迁移替换为引用 dist 文件夹而不是 src 文件夹,更改自:
entities: ['src/modules/**/infra/typeorm/models/*.ts'],
migrations: ['src/shared/typeorm/migrations/*.ts'],
cli: {
migrationsDir: 'src/shared/typeorm/migrations',
},
至:
entities: ['dist/modules/**/infra/typeorm/models/*.js'],
migrations: ['dist/shared/typeorm/migrations/*.js'],
cli: {
migrationsDir: 'dist/shared/typeorm/migrations',
},
这实际上解决了 dist 代码的问题,但破坏了我的开发环境(使用 ts-node-dev 运行的环境),因此它不是一个有效的选项。
您可以动态更改路径:
ormconfig.js
const baseDir = process.env.NODE_ENV == 'production' ? 'dist/src' : 'src';
module.exports = {
...
entities: [baseDir + '/entity/**/*.{ts,js}'],
migrations: [baseDir + '/migration/**/*.{ts,js}'],
subscribers: [baseDir + '/subscriber/**/*.{ts,js}'],
cli: {
entitiesDir: baseDir + '/entity',
migrationsDir: baseDir + '/migration',
subscribersDir: baseDir + '/subscriber',
},
};