Node.js &引用;模块分析失败:意外的令牌;添加类字段时
以下TypeScript代码(表示静态类字段和其他TypeScript功能可用)已使用Webpack成功生成: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
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']
}
};