严格模式外尚不支持块范围的声明

node.js Webpack

前端凯

2020-03-24

我在Homestead流浪者盒子上运行Laravel 5.4我已经使用npm install命令安装了所有npm依赖项那没有产生任何错误。

在我的webpack.min.js文件中,我有:

const { mix } = require('laravel-mix');

/*
 |--------------------------------------------------------------------------
 | Mix Asset Management
 |--------------------------------------------------------------------------
 |
 | Mix provides a clean, fluent API for defining some Webpack build steps
 | for your Laravel application. By default, we are compiling the Sass
 | file for the application as well as bundling up all the JS files.
 |
 */

mix.js([
        'resources/assets/plugins/jquery-1.11.3.min.js',
        'resources/assets/plugins/bootstrap/js/bootstrap.min.js',
        'resources/assets/js/main.js'
    ], 'public/js'
);

mix.combine([
       'resources/assets/plugins/bootstrap/css/bootstrap.min.css',
       'resources/assets/plugins/font-awesome/css/font-awesome.css',
       'resources/assets/css/styles.css'
   ], 'public/css/all.css');

当我要运行时npm run production,出现以下错误:

> @ production /home/vagrant/projects/nielsvroman
> node node_modules/cross-env/bin/cross-env.js NODE_ENV=production node_modules/webpack/bin/webpack.js --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js

/home/vagrant/projects/nielsvroman/node_modules/laravel-mix/setup/webpack.config.js:120
        let extractPlugin = new plugins.ExtractTextPlugin(
        ^^^

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:404:25)
    at Object.Module._extensions..js (module.js:432:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:311:12)
    at Module.require (module.js:366:17)
    at require (module.js:385:17)
    at requireConfig (/home/vagrant/projects/nielsvroman/node_modules/webpack/bin/convert-argv.js:96:18)
    at /home/vagrant/projects/nielsvroman/node_modules/webpack/bin/convert-argv.js:109:17
    at Array.forEach (native)

npm ERR! Linux 3.19.0-25-generic
npm ERR! argv "/usr/bin/nodejs" "/usr/bin/npm" "run" "production"
npm ERR! node v5.0.0
npm ERR! npm  v3.3.6
npm ERR! code ELIFECYCLE
npm ERR! @ production: `node node_modules/cross-env/bin/cross-env.js NODE_ENV=production node_modules/webpack/bin/webpack.js --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the @ production script 'node node_modules/cross-env/bin/cross-env.js NODE_ENV=production node_modules/webpack/bin/webpack.js --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js'.
npm ERR! This is most likely a problem with the  package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR!     node node_modules/cross-env/bin/cross-env.js NODE_ENV=production node_modules/webpack/bin/webpack.js --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js
npm ERR! You can get their info via:
npm ERR!     npm owner ls
npm ERR! There is likely additional logging output above.

npm ERR! Please include the following file with any support request:
npm ERR!     /home/vagrant/projects/nielsvroman/npm-debug.log

这可能是什么问题?

第3734篇《严格模式外尚不支持块范围的声明》来自Winter(https://github.com/aiyld/aiyld.github.io)的站点

6个回答
米亚猴子 2020.03.24

我在NodeJS中使用其中一个测试脚本遇到了相同的问题,并且通过使用ECMAScript 5的严格模式解决了我的错误。

"use strict";

在脚本的上方添加了以上代码,效果很好。

严格模式对常规JavaScript语义进行了几处更改:

  1. 通过将它们更改为引发错误来消除一些JavaScript静默错误。

  2. 修复了使JavaScript引擎难以执行优化的错误:严格模式代码有时可以比非严格模式下的相同代码运行得更快。

  3. 禁止在将来的ECMAScript版本中定义某些语法。

猿JinJin 2020.03.24

我也遇到了同样的问题,我尝试运行这些命令,现在它可以工作了。但在此之前先卸载nodejs

sudo apt-get update

apt-cache policy nodejs

sudo apt install nodejs

希望您的问题能得到解决。

西里神奇 2020.03.24

您的节点未更新。

删除您的节点版本,下载并安装最新版本。

在项目根目录下运行:

npm rebuild

然后再试一次:

npm run production

PS .:如果您不想删除您的节点版本,请下载新版本并与此一起运行。

猿樱 2020.03.24

我在Ubuntu上的旧版本的nodejs包也遇到了同样的问题。我刚刚更新到7.5,并且可以正常工作。

curl -sL https://deb.nodesource.com/setup_7.x | sudo -E bash -
sudo apt-get install -y nodejs
泡芙A 2020.03.24

这是因为您的计算机上没有最新版本的NodeJS。我在Ubuntu上,所以如果您在任何Linux发行版上,请尝试此操作。

curl -sL https://deb.nodesource.com/setup_11.x | sudo -E bash -
sudo apt-get install -y nodejs
卡卡西米亚 2020.03.24

升级节点程序包是这里的解决方案。由于无需自行下载,安装和管理节点版本,因此添加了替代性步骤来升级节点软件包。您可以n在Mac / Ubuntu中使用名为的模块升级节点软件包

sudo npm install -g n
sudo n stable

这将安装最新的稳定节点程序包。你可以跑

node --version

如果仍然看到旧版本,则可能是安装新软件包的目录问题。我必须创建一个符号链接才能使其正常工作-

sudo ln -s  /usr/local/n/versions/node/9.0.0/bin/node  /usr/local/bin/node

问题类别

JavaScript Ckeditor Python Webpack TypeScript Vue.js React.js ExpressJS KoaJS CSS Node.js HTML Django 单元测试 PHP Asp.net jQuery Bootstrap IOS Android