我已经用 express、node.js 和 webpack 建立了一个非常简单的项目。在配置 webpack.config.js 时安装 TypeORM 后,它会触发实体文件夹中 User.ts 的意外 token 错误。
问题似乎与 ormconfig.json 引用 .ts 实体文件有关。
类似线程中的解决方案似乎是使用带有一些额外参数的 ts-node,但我在这个项目中使用 webpack 并执行捆绑文件。
索引.ts
import 'reflect-metadata';
import { createConnection } from 'typeorm';
import { User } from './entity/User';
createConnection()
.then(async connection => {
console.log('Inserting a new user into the database...');
const user = new User();
user.firstName = 'Timber';
user.lastName = 'Saw';
user.age = 25;
await connection.manager.save(user);
console.log('Saved a new user with id: ' + user.id);
console.log('Loading users from the database...');
const users = await connection.manager.find(User);
console.log('Loaded users: ', users);
console.log(
'Here you can setup and run express/koa/any other framework.'
);
})
.catch(error => console.log(error));
ormconfig.json
{
"type": "mssql",
"host": "*",
"port": 27017,
"username": "*",
"password": "*",
"database": "*",
"synchronize": true,
"logging": false,
"entities": ["src/entity/**/*.ts"],
"migrations": ["src/migration/**/*.ts"],
"subscribers": ["src/subscriber/**/*.ts"],
"cli": {
"entitiesDir": "src/entity",
"migrationsDir": "src/migration",
"subscribersDir": "src/subscriber"
}
}
webpack.config.js
module.exports = {
mode: 'development',
entry: path.resolve(path.join(__dirname, './src/index.ts')),
externals: [nodeExternals()],
name: 'API',
context: __dirname,
target: 'node',
output: {
path: __dirname + '/dist',
filename: '[name].bundle.js',
publicPath: '/',
libraryTarget: 'commonjs2',
},
resolve: {
extensions: ['.ts', '.tsx', '.js', '.json'],
modules: [path.resolve(__dirname, 'node_modules')],
},
module: {
rules: [
{
test: /\.tsx?$/,
loader: 'awesome-typescript-loader',
exclude: [/node_modules/, /dist/],
},
{
test: /\.js$/,
loader: 'babel-loader',
exclude: [/node_modules/, /dist/],
options: {
babelrc: true,
},
},
],
},
plugins: [new CleanWebpackPlugin()],
};
报错信息如下(代码引用TypeORM默认自带的用户实体文件
api/src/entity/User.ts:1
(function (exports, require, module, __filename, __dirname) { import { Entity, PrimaryGeneratedColumn, Column } from 'typeorm';
^
SyntaxError: Unexpected token {
at new Script (vm.js:80:7)
at createScript (vm.js:274:10)
at Object.runInThisContext (vm.js:326:10)
at Module._compile (internal/modules/cjs/loader.js:664:28)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:712:10)
at Module.load (internal/modules/cjs/loader.js:600:32)
at tryModuleLoad (internal/modules/cjs/loader.js:539:12)
at Function.Module._load (internal/modules/cjs/loader.js:531:3)
at Module.require (internal/modules/cjs/loader.js:637:17)
at require (internal/modules/cjs/helpers.js:22:18)
最佳答案
我昨天通过 Soufiaane 找到了解决方案没有涵盖 webpack 方面,但仍然解决了问题。
删除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
]
})
关于node.js - TypeORM + Webpack 导致 SyntaxError : Unexpected token for entity file,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56769663/