模块解析失败:意外字符'@'

时间:2018-09-22 20:43:07

标签: node.js typescript webpack

我想构建我的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 {

您能帮助我解决此错误吗? 感谢前进

1 个答案:

答案 0 :(得分:0)

似乎没有使用ts-loader。我认为这是因为您已将test指定为字符串而不是正则表达式。尝试删除周围的单引号。请参见the documentation中的示例。

相关问题