最近开发一个老项目,发现之前好好的项目现在运行起来报错了 ,各种切换npm 源和 node版本都不行,百度和谷歌都没找到相关信息,报错如下,
error in ./node_modules/@lit/reactive-element/decorators/state.js
Module parse failed: Unexpected token (6:27)
You may need an appropriate loader to handle this file type.
| * Copyright 2017 Google LLC
| * SPDX-License-Identifier: BSD-3-Clause
| */function t(t){return r({...t,state:!0})}export{t as state};
| //# sourceMappingURL=state.js.map
说是处理不了当前文件。
error in ./node_modules/@lit/reactive-element/decorators/state.js
Module parse failed: Unexpected token (6:27)
You may need an appropriate loader to handle this file type.
| * Copyright 2017 Google LLC
| * SPDX-License-Identifier: BSD-3-Clause
| */function t(t){return r({...t,state:!0})}export{t as state};
| //# sourceMappingURL=state.js.map
|
发现是@lit这个模块下的文件报错,点开详情发现这里的是源文件,并不是es5语法。
看了下package.json里的版本号用的是^,意味着这个模块可以自动小版本升级。猜想应该是模块自动升级了,导致某些地方不兼容所以报错了,
最后发现项目中配置了,babel解析,这个插件输入出的是es6代码,需要用bable编译一下。
{
test: /\.js$/,
loader: 'babel-loader',
include: [
[resolve('src'), resolve('/node_modules/_@lit')]
],
}
看报错信息是./node_modules/@lit/reactive-element ,原来新版的原路径改成了 @lit,旧版是_@lit 所以没有经过bable解析,导致报错。经过如下修改增加了新的解析路径,问题得到解决。
{
test: /\.js$/,
loader: 'babel-loader',
include: [
[resolve('src'), resolve('/node_modules/_@lit'),resolve('/node_modules/@lit')]
],
}