Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/38.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js &引用;模块分析失败:意外的令牌;添加类字段时_Node.js_Typescript_Webpack - Fatal编程技术网

Node.js &引用;模块分析失败:意外的令牌;添加类字段时

Node.js &引用;模块分析失败:意外的令牌;添加类字段时,node.js,typescript,webpack,Node.js,Typescript,Webpack,以下TypeScript代码(表示静态类字段和其他TypeScript功能可用)已使用Webpack成功生成: export default class ConfigRepresentative { constructor() { console.log('ok'); } } 失败(如果删除私有和静态,则相同): 错误: ERROR in ./TypeScriptSource/index.ts 7:10 Module parse failed: Unexpected token

以下TypeScript代码(表示静态类字段和其他TypeScript功能可用)已使用Webpack成功生成:

export default class ConfigRepresentative {
  constructor() {
    console.log('ok');
  }
}
失败(如果删除
私有
静态
,则相同):

错误:

ERROR in ./TypeScriptSource/index.ts 7:10
Module parse failed: Unexpected token (7:10)
You may need an appropriate loader to handle this file type.
| export default class ConfigRepresentative {
| 
>   private static ownInstanceHasBeenCreated: boolean = false;
| 
|   constructor() {
webpack.config.js 为了节省您重现此问题的时间,我附上了源文件。

这是因为项目位于
节点模块
文件夹中。设置
exclude:/node\u modules/
取消规则
{test:/\.ts?$/,使用:'ts loader'}
,但没有类属性代码的是纯JavaScript

(我知道这是一种不好的做法-在
节点\ U模块
中开发一些东西,但是我不知道开发依赖项的其他解决方案。在这种情况下,如果其他项目不使用单个
ConfigRepresentative
,则它是无用的)

ERROR in ./TypeScriptSource/index.ts 7:10
Module parse failed: Unexpected token (7:10)
You may need an appropriate loader to handle this file type.
| export default class ConfigRepresentative {
| 
>   private static ownInstanceHasBeenCreated: boolean = false;
| 
|   constructor() {
module.exports = {

  entry: './TypeScriptSource/index.ts',
  output: {
    filename: 'index.js',
    path: __dirname,
    libraryTarget: 'umd'
  },

  target: 'node',
  mode: 'production',
  watch: true,

  module: {
    rules: [
      {
        test: /\.ts?$/,
        use: 'ts-loader',
        exclude: /node_modules/
      }
    ]
  },
  resolve: {
    extensions: ['.ts', '.js']
  }
};