新的npm安装webpack.js正在抛出块范围错误

时间:2018-03-12 21:37:35

标签: node.js webpack task-runner-explorer

我是Webpack,Visual Studio和Task Runner的新手,但这些是我被告知要在工作中安装/使用的,所以我在努力弄清楚如何使它全部工作。我刚刚使用NPM全局安装webpack和webpack-cli的新副本。我将Task Runner插件安装到Visual Studio,并使用Run>提供的开发选项。神秘的是,我的唯一机器出现以下错误,没有人知道原因:

C:\Users\[me]\AppData\Roaming\npm\node_modules\webpack\bin\webpack.js:3
let webpackCliInstalled = false;
^^^
SyntaxError: Block-scoped declarations (let, const, function, class) not yet supported outside strict mode
    at exports.runInThisContext (vm.js:53:16)
    at Module._compile (module.js:373:25)
    at Object.Module._extensions..js (module.js:404:10)
    at Module.load (module.js:343:32)
    at Function.Module._load (module.js:300:12)
    at Function.Module.runMain (module.js:429:10)
    at startup (node.js:139:18)
    at node.js:999:3
Process terminated with code 1.

我正在使用最新版本的NodeJS和NPM。任何想法为什么未经修改的webpack下载都会导致块范围错误?

修改

我看到这个问题引起了一些关注,所以我想我应该提一下问题已经解决了。不幸的是,解决方案是完全卸载webpack和webpack-cli并重新安装它们。然后就行了。为什么...?谁知道?我听说其他人也遇到过这个问题,虽然我第一次没有复制它。

3 个答案:

答案 0 :(得分:9)

转到Tools > Options > Projects and Solutions > Web Package Management > External Web Tools DESELECT $(VSINSTALLDIR)\Web\External的选项。

enter image description here

参考Visual Studio Task Runner Error with ES6  并 Visual Studio Task Runner "SyntaxError: Use of const in strict mode."

答案 1 :(得分:1)

今天遇到同样的问题,对于其他任何人来说,解决方案就是确保节点和npm是最新的。更新那些(建议查看nvm以实现此目的)然后重新安装webpack和webpack-cli包并且所有包都已经过排序。

答案 2 :(得分:0)

尝试为像babel及其预设的ES6 systax添加加载程序。你可以这样做:npm installpackage.json中添加这些依赖项之后(我的依赖项没有更新,你可以毫无问题地更新它们):

"devDependencies": {
"babel-core": "^6.24.0",
"babel-loader": "^7.0.0",
"babel-plugin-react-html-attrs": "^2.0.0",
"babel-plugin-transform-class-properties": "^6.23.0",
"babel-plugin-transform-decorators-legacy": "^1.3.4",
"babel-preset-es2015": "^6.24.0",
"babel-preset-react": "^6.23.0",
"babel-preset-stage-0": "^6.22.0",
}

此外,您必须将此添加到webpack.config.js> loaders(设置新加载器 - babel-loader - ):

loaders: [
        {
            test: /\.(js|jsx)$/,
            loader: 'babel-loader',
            query: {
                presets: [
                    'es2015',
                    'react',
                    'stage-0'
                ],
                plugins: [
                    'react-html-attrs',
                    'transform-decorators-legacy',
                    'transform-class-properties'
                ],
                compact: true
            }
        }
    ]