当我尝试运行编译的打字稿代码时,出现语法错误:
\entity\Config.ts:1
(function (exports, require, module, __filename, __dirname) { import { Entity, PrimaryGeneratedColumn, Column, BaseEntity } from "typeorm";
^
SyntaxError: Unexpected token {
但是当我使用ts-node
和nodemon
代码运行打字稿代码时,运行得很好。
因此,我一直在进行一些日志记录,以找出问题出在哪里,而当我在TypeORM上点击createConnection()
方法时,问题似乎就发生了。我是Typescript和TypeORM库的新手。
entity / config.ts
import { Entity, PrimaryGeneratedColumn, Column, BaseEntity } from "typeorm";
@Entity()
export class Config extends BaseEntity {
@PrimaryGeneratedColumn()
id: number;
@Column()
app: String;
@Column()
endpoint: String;
@Column()
token: String;
}
server.ts
import { createConnection } from "typeorm";
// Database connected
createConnection()
.then(() => {
console.log("Test");
})
.catch(err => {
console.log(err);
});
index.ts
require("reflect-metadata");
require("dotenv/config");
require("./server");
package.json相关性
"scripts": {
"dev:server": "ts-node src",
"dev": "nodemon -e ts -w src -x npm run dev:server",
"build:server": "tsc",
"start:server": "node build/index.js",
"start": "npm run build:server && npm run start:server"
},
"keywords": [],
"author": "",
"license": "MIT",
"devDependencies": {
"@types/axios": "^0.14.0",
"@types/graphql": "^14.0.3",
"@types/node": "^10.12.18",
"@types/winston": "^2.4.4",
"nodemon": "^1.18.9",
"ts-node": "^7.0.1",
"typescript": "^3.2.2"
},
"dependencies": {
"apollo-server-express": "^2.3.1",
"axios": "^0.18.0",
"dotenv": "^6.2.0",
"express": "^4.16.4",
"graphql": "^14.0.2",
"pg": "^7.7.1",
"reflect-metadata": "^0.1.12",
"sequelize": "^4.42.0",
"typeorm": "^0.2.9",
"winston": "^3.1.0"
}
}
答案 0 :(得分:2)
因此TypeORM Slack的成员(由uladzimir回答此问题时)解决了该问题。问题出在我的ormconfig文件中。
问题:
"entities": ["src/database/entity/**/*.ts", "build/database/entity/**/*.js"],
"migrations": [
"src/database/migration/**/*.ts",
"build/database/migration/**/*.js"
],
"subscribers": [
"src/database/subscriber/**/*.ts",
"build/database/subscriber/**/*.js"
],
"cli": {
"entitiesDir": "src/entity",
"migrationsDir": "src/migration",
"subscribersDir": "src/subscriber"
}
解决方案:
"entities": ["build/database/entity/**/*.js"],
"migrations": ["build/database/migration/**/*.js"],
"subscribers": ["build/database/subscriber/**/*.js"],
"cli": {
"entitiesDir": "src/entity",
"migrationsDir": "src/migration",
"subscribersDir": "src/subscriber"
}
出于某种原因,我认为我必须添加*ts
文件用于测试/开发目的,但事实并非如此,这是导致问题的原因。我不确定为什么会导致此问题,但是如果发现,我会将其作为对此答案的评论。
感谢大家在此问题上的帮助。
答案 1 :(得分:1)
我设法通过删除ormconfig.json
文件并在createConnection
函数中传递数据库配置来解决此问题。
示例:
import { User } from './entity'
// import every other entity you have
// .......
await createConnection({
type: 'sqlite',
database: 'database.sqlite',
synchronize: true,
logging: true,
entities: [
User // pass your entities in here
]
})
答案 2 :(得分:1)
如果您使用的是 TypeOrm,则可以将其用作配置
entities: [__dirname + '/models/**/*.entity.{ts,js}'],
migrations: [__dirname + '/migrations/**/*.{ts,js}'],
subscribers: [__dirname + '/subscriber/**/*.{ts,js}'],
添加 __dirname
可以正确解析模块,具体取决于您是在 dev(解析为 ts 版本)还是 prod(解析为 js 版本)中运行它