DeW*_*ulf 13 typescript typeorm
所以,我正在使用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)
Run Code Online (Sandbox Code Playgroud)
我的tsconfig.json:
{
"compilerOptions": {
"lib": [
"es5",
"es6"
],
"target": "es5",
"module": "commonjs",
"moduleResolution": "node",
"outDir": "./build",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"sourceMap": true
},
"exclude": [
"client"
]
}
Run Code Online (Sandbox Code Playgroud)
我的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"
}
}
Run Code Online (Sandbox Code Playgroud)
抛出错误的文件:
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;
}
Run Code Online (Sandbox Code Playgroud)
我尝试过的:
我一直在谷歌搜索这个问题,这让我摸不着头脑.任何和所有的帮助将不胜感激.
DeW*_*ulf 51
好吧,我只是愚蠢.
因此,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"
}
}
Run Code Online (Sandbox Code Playgroud)
您还可以使用ts-node运行 typeorm,这将允许您运行ts文件,并允许您避免编译和运行单独的js文件。
根据此 github 评论,您可以执行以下操作:
1.- 全局安装 ts-node 和 typescript:
$ npm install -g ts-node typescript2.- 使用 ts-node 执行 typeorm 命令:
$ ts-node ./node_modules/.bin/typeorm migrations:generate -n
如果你在windows上,你会遇到问题。根据此评论,您需要直接引用cli.js:
ts-node node_modules\typeorm\cli.js
| 归档时间: |
|
| 查看次数: |
7479 次 |
| 最近记录: |