gpt4 book ai didi

typescript - 语法错误 : Unexpected token import TypeORM entity

转载 作者:搜寻专家 更新时间:2023-10-30 20:33:20 25 4
gpt4 key购买 nike

因此,我正在使用 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)

我的tsconfig.json:

{
"compilerOptions": {
"lib": [
"es5",
"es6"
],
"target": "es5",
"module": "commonjs",
"moduleResolution": "node",
"outDir": "./build",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"sourceMap": true
},
"exclude": [
"client"
]
}

我的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"
}
}

抛出错误的文件:

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;
}

我尝试过的:

  • 我已尝试将我的 node.js 更新到最新版本(8.11.2 版)
  • 我尝试以“es5”、“es6”和“es7”的各种组合更改 tsconfig.json 中的“lib”设置
  • 我已尝试为上述要点中列出的目标更改我的 tsconfig.json 的“目标”。
  • 我尝试将实体文件中的导入语句从 import (lib) from (module) 更改为 const (lib) require (module)"; 但是,这会导致更多问题并且效果不佳。

我一直在谷歌上广泛搜索这个问题,这让我摸不着头脑。我们将不胜感激任何帮助。

最佳答案

好吧,我就是傻了。

因此,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"
}
}

关于typescript - 语法错误 : Unexpected token import TypeORM entity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50361948/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com