【发布时间】:2018-09-23 12:47:24
【问题描述】:
我想构建我的 graphql 服务器应用程序,当我通过 webpack 4 构建时,我在 typeorm 的装饰器上遇到了这个错误:
ERROR in ./src/models/user.ts 15:0
Module parse failed: Unexpected character '@' (15:0)
You may need an appropriate loader to handle this file type.
| import { JWT } from './jwt';
|
> @Entity()
| @Unique(['username'])
| @Unique(['email'])
@ ./src/server.ts 15:0-37 34:45-49
webpack.config.js 是这样的:
var ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin');
var fs = require('fs');
var path = require('path');
var nodeExternals = require('webpack-node-externals');
module.exports = {
mode: 'production',
entry: './src/server.ts',
output: {
path: __dirname + '/dist',
filename: '[name].[chunkhash:8].js',
},
resolve: {
extensions: ['.ts', '.tsx', '.js'],
},
module: {
rules: [
{
test: '/\.tsx?$/',
exclude: '/node_modules/',
include: path.resolve(__dirname, 'src'),
use: {
loader: 'ts-loader',
options: {
transpileOnly: true
}
}
}
]
},
plugins: [
new ForkTsCheckerWebpackPlugin()
],
externals: [nodeExternals()],
};
tsconfig.json 是这样的:
{
"exclude": [
"fixtures",
"tests"
],
"compilerOptions": {
"target": "es2016",
"module": "commonjs",
"outDir": "dist",
"rootDir": "src",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"typeRoots": [
"node_modules/@types"
],
"lib": ["es2016", "esnext.asynciterable"]
}
}
当我使用 tsc 编译时,我没有任何错误,但是当我通过 node dist/server.js 启动应用程序时出现此错误:
(function (exports, require, module, __filename, __dirname) { import { Field, ID, Int, ObjectType } from 'type-graphql';
SyntaxError: Unexpected token {
你能帮我解决这个错误吗? 感谢提前
【问题讨论】:
标签: node.js typescript webpack