所以,我正在使用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;
}

我try 过:

  • 我已try 更新我的 node .js至最新版本(8.11.2版)
  • 我已try 更改Tconfig中的"lib"设置.json是"es5"、"es6"和"es7"的不同组合
  • 我已try 更改Tconfig的"目标".json用于上述要点中列出的目标.
  • 我曾try 将实体文件中的导入语句从import (lib) from (module)更改为const (lib) require (module)";,但这会导致更多问题,效果不好.

我一直在谷歌上搜索这个问题,这让我抓狂.我们将不胜感激.

推荐答案

好吧,我只是个傻瓜.

所以,ormconfig.json将实体指向/src文件夹中的ts文件.构建项目时,它应该指向实体所在的位置.我确实觉得很奇怪,它试图在构建后引用TS文件.

奥姆孔菲格.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相关问答推荐

如何使用axios在前端显示错误体

角形标题大小写所有单词除外

无法将select选项的值设置为ngFor循环中的第一个元素

Typescript泛型类型:按其嵌套属性映射记录列表

TypeScrip界面属性依赖于以前的属性-针对多种可能情况的简明解决方案

如何在编译时为不带常量的参数引发错误

为什么我的条件类型不能像我预期的那样工作?

缩小并集子键后重新构造类型

带下拉菜单的Angular 响应式选项卡

从TypeScrip中的其他类型检索泛型类型

Angular文件上传到Spring Boot失败,多部分边界拒绝

@TANSTACK/REACT-QUERY中发生Mutations 后的数据刷新问题

类型脚本-在调用期间解析函数参数类型

如何从一个泛型类型推断多个类型

TS2739将接口类型变量赋值给具体变量

TaskListProps[]类型不可分配给TaskListProps类型

如何使函数参数数组不相交?

使用RXJS获取数据的3种不同REST API

元组解释

在对象类型的类型别名中,属性是用分号 (;) 还是逗号 (,) 分隔的?