所以,我正在使用typeORM,当我将TypeScript转换为JavaScript时,我遇到了一个奇怪的错误。我收到以下错误:
a, b, c, d
我的tsconfig.json:
(function (exports, require, module, __filename, __dirname) { import { Entity, PrimaryGeneratedColumn, ManyToOne, OneToMany, TreeChildren, TreeParent, JoinColumn, Column, Tree, TreeLevelColumn } from "typeorm";
^^^^^^
SyntaxError: Unexpected token import
at createScript (vm.js:80:10)
at Object.runInThisContext (vm.js:139:10)
at Module._compile (module.js:616:28)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)
at Module.require (module.js:596:17)
at require (internal/module.js:11:18)
at Function.PlatformTools.load (C:\Users\*redacted*\Workspace\experimental\*redacted*\node_modules\typeorm\platform\PlatformTools.js:126:28)
我的package.json:
{
"compilerOptions": {
"lib": [
"es5",
"es6"
],
"target": "es5",
"module": "commonjs",
"moduleResolution": "node",
"outDir": "./build",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"sourceMap": true
},
"exclude": [
"client"
]
}
抛出错误的文件:
{
"name": "*redacted*",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"dev": "nodemon --watch 'src/**/*.ts' --ignore 'src/**/*.spec.ts' --exec ts-node src/index.ts",
"start": "tsc && node ./build/index.js",
"migrate": "ts-node ./node_modules/typeorm/cli.js migration:generate"
},
"author": "*redacted*",
"license": "ISC",
"dependencies": {
"bcryptjs": "^2.4.3",
"body-parser": "^1.18.3",
"class-validator": "^0.8.5",
"express": "^4.16.3",
"jwt-simple": "^0.5.1",
"morgan": "^1.9.0",
"pg": "^7.4.3",
"reflect-metadata": "^0.1.10",
"typeorm": "0.2.5"
},
"devDependencies": {
"@types/bcryptjs": "^2.4.1",
"@types/body-parser": "^1.17.0",
"@types/express": "^4.11.1",
"@types/jwt-simple": "^0.5.33",
"@types/node": "^8.10.15",
"ts-node": "3.3.0",
"typescript": "2.5.2"
}
}
我尝试过:
我一直在谷歌上搜索这个问题,这让我摸不着头脑。任何和所有的帮助将不胜感激。
答案 0 :(得分:18)
好吧,我只是愚蠢。
因此,ormconfig.json将您的实体指向/ src文件夹中的ts文件。在构建项目时,它应指向您的实体所在的位置。我确实认为在构建之后尝试引用TS文件是奇怪的。
ormconfig.json
{
"type": "postgres",
"host": "**********************",
"port": 5432,
"username": "**************",
"password": "*************",
"database": "*************",
"synchronize": true,
"logging": false,
"entities": [
// changed this
"dist/entity/*.js"
],
"migrations": [
"src/migration/**/*.ts"
],
"subscribers": [
"src/subscriber/**/*.ts"
],
"cli": {
"entitiesDir": "src/entity",
"migrationsDir": "src/migration",
"subscribersDir": "src/subscriber"
}
}
答案 1 :(得分:3)
您还可以使用ts-node
运行typeorm,这将允许您运行ts
文件,并且可以避免编译和运行单独的js
文件。
每this github comment,您可以执行以下操作:
1.-全局安装ts-node和typescript:
$ npm install -g ts-node typescript
2.-使用ts-node执行typeorm命令:
$ ts-node ./node_modules/.bin/typeorm migrations:generate -n
如果您使用的是Windows,则会遇到问题。对于this comment,您将要直接引用cli.js
:
ts-node node_modules\typeorm\cli.js
答案 2 :(得分:0)
我有一个类似的问题,也很愚蠢。我不小心使用了.js
扩展名而不是.ts
答案 3 :(得分:0)
这也发生在我身上,出现问题是因为我不小心从'typeorm / browser'而不是'typeorm'导入了'BaseEntity'。
第一个是js文件,第二个是ts文件。