typescript 语法错误:意外的令牌导入类型ORM 实体
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/50361948/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
SyntaxError: Unexpected token import typeORM entity
提问by DeWulf
So, I am working with typeORM and am getting an odd error when I transpile my TypeScript to JavaScript. I am receiving the following error:
因此,我正在使用 typeORM 并且在将我的 TypeScript 转换为 JavaScript 时遇到了一个奇怪的错误。我收到以下错误:
(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)
My tsconfig.json:
我的 tsconfig.json:
{
"compilerOptions": {
"lib": [
"es5",
"es6"
],
"target": "es5",
"module": "commonjs",
"moduleResolution": "node",
"outDir": "./build",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"sourceMap": true
},
"exclude": [
"client"
]
}
My package.json:
我的 package.json:
{
"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"
}
}
The file throwing the error:
抛出错误的文件:
import { Entity, PrimaryGeneratedColumn, ManyToOne, OneToMany, TreeChildren, TreeParent, JoinColumn, Column, Tree, TreeLevelColumn } from "typeorm";
import { User } from "./User";
import { Debate } from "./Debate";
@Entity()
@Tree("closure-table")
export class Comment {
@PrimaryGeneratedColumn("uuid")
id: string;
@Column()
text: string;
@ManyToOne(type => User)
user: User;
@ManyToOne(type => Debate, debate => debate.comments)
debate: Debate;
@TreeChildren()
children: Comment[];
@TreeParent()
parent: Comment;
}
What I have tried:
我尝试过的:
- I have tried updating my node.js to the latest (version 8.11.2)
- I have tried changing the "lib" settings in my tsconfig.json in various combinations of "es5", "es6", and "es7"
- I have tried changing the "target" for my tsconfig.json for the targets listed in the above bullet point.
- I have tried changing the import statements in my entity files from "import (lib) from (module)" to "const (lib) require (module)"; However, this causes more issues and doesn't work well.
- 我尝试将 node.js 更新到最新版本(8.11.2 版)
- 我曾尝试以“es5”、“es6”和“es7”的各种组合更改 tsconfig.json 中的“lib”设置
- 我已经尝试为上面列出的目标更改 tsconfig.json 的“目标”。
- 我尝试将实体文件中的导入语句从“import (lib) from (module)”更改为“const (lib) require (module)”;但是,这会导致更多问题并且效果不佳。
I have been googling for this issue extensively and it has left me scratching my head. Any and all help would be greatly appreciated.
我一直在广泛地搜索这个问题,这让我摸不着头脑。任何和所有的帮助将不胜感激。
回答by DeWulf
Alright, I am simply dumb.
好吧,我就是傻。
So, the ormconfig.json points your entities to your ts files in your /src folder. When you build your project, it should point to where your entities are. I did think it was odd that it was trying to reference a TS file after building.
因此,ormconfig.json 将您的实体指向 /src 文件夹中的 ts 文件。当您构建项目时,它应该指向您的实体所在的位置。我确实认为它在构建后尝试引用 TS 文件很奇怪。
ormconfig.json
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"
}
}
回答by Nicholas Barry
You can also use ts-nodeto run typeorm, which will allow you to run tsfiles, and which will allow you to avoid compiling and running separate jsfiles.
您还可以使用ts-node运行 typeorm,这将允许您运行ts文件,并允许您避免编译和运行单独的js文件。
Per this github comment, you can do the following:
根据此 github 评论,您可以执行以下操作:
1.- Install ts-node and typescript globally:
$ npm install -g ts-node typescript2.- Execute the typeorm command with ts-node:
$ ts-node ./node_modules/.bin/typeorm migrations:generate -n
1.- 全局安装 ts-node 和 typescript:
$ npm install -g ts-node typescript2.- 使用 ts-node 执行 typeorm 命令:
$ ts-node ./node_modules/.bin/typeorm migrations:generate -n
If you're on windows, you'll have problems with that. Per this comment, you'll want to directly reference cli.js:
如果你在windows上,你会遇到问题。根据此评论,您需要直接参考cli.js:
ts-node node_modules\typeorm\cli.js
ts-node node_modules\typeorm\cli.js
回答by ericgio
I had a similar issue and was also being dumb. I had accidentally used the .jsextension to name the file instead of .ts
我有一个类似的问题,也很愚蠢。我不小心使用.js扩展名来命名文件而不是.ts
回答by oded
it happened to me too, the problem occurred because i've accidently imported 'BaseEntity' from 'typeorm/browser' instead of 'typeorm'.
它也发生在我身上,出现问题是因为我不小心从“typeorm/browser”而不是“typeorm”导入了“BaseEntity”。
the first one is js file, and the second is ts file.
第一个是js文件,第二个是ts文件。

