Block-scoped declarations not yet supported outside strict mode
Asked Answered
G

7

42

I'm running Laravel 5.4 on my Homestead vagrant box. I've installed all the npm dependencies with npm install command. That didn't produce any errors.

In my webpack.min.js file I have:

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');

When I want to run npm run production I'm getting the following errors:

> @ 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

What could be the problem with this?

Gies answered 31/1, 2017 at 14:33 Comment(3)
nodejs 5 doesn't support let outside strict mode. a) update nodejs or b) type 'use strict'; at the very first line of your js file, or c) use babel to transpile the file to es5Speaking
@baao, I've added "use strict"; to the top of my webpack.mix.js file but nothing is changed.Gies
I think it needs to be in the file where let is actually used. If you can't change that file, you still have options a and cSpeaking
D
65

I had the same problem coused by the old version of nodejs package on Ubuntu. I've just updated to 7.5 and it's working.

curl -sL https://deb.nodesource.com/setup_7.x | sudo -E bash -
sudo apt-get install -y nodejs
Denunciatory answered 30/3, 2017 at 17:58 Comment(3)
As of July '18, nodejs 7 is no longer supported - this link should be updated to: deb.nodesource.com/setup_8.xPhillie
Here is the link to the update document github.com/nodesource/distributions/blob/master/README.md. You can choose your preffered version from the list. I solved the problem by doing the proper upgrade too anyways.Sestina
this is OUTDATED, it instructs people to install node 7Hildehildebrand
G
31

Upgrading node package is the solution here. Adding altenative steps to upgrade node package as there is no need to download, install and manage node versions yourself. You can use a module called n to upgrade you node package in Mac/Ubuntu

sudo npm install -g n
sudo n stable

This will install latest stable node package. You can run

node --version

If you are still seeing old version it might be directory issues where new package is installed. I had to create a symlink to make it work-

sudo ln -s  /usr/local/n/versions/node/9.0.0/bin/node  /usr/local/bin/node
Gottuard answered 7/11, 2017 at 11:16 Comment(0)
F
6

This is because you don't have the latest version of NodeJS on your machine. I'm on Ubuntu so if you're on any kind of Linux distro try this.

curl -sL https://deb.nodesource.com/setup_11.x | sudo -E bash -
sudo apt-get install -y nodejs
Florentinaflorentine answered 1/3, 2019 at 17:54 Comment(0)
U
5

I had faced the same problem with one of my test script in NodeJS and i resolved my error by using ECMAScript 5's strict mode.

"use strict";

Added above line at the top of my script and it works well.

Strict mode makes several changes to normal JavaScript semantics:

  1. Eliminates some JavaScript silent errors by changing them to throw errors.

  2. Fixes mistakes that make it difficult for JavaScript engines to perform optimizations: strict mode code can sometimes be made to run faster than identical code that's not strict mode.

  3. Prohibits some syntax likely to be defined in future versions of ECMAScript.

Upstart answered 17/7, 2018 at 19:8 Comment(0)
B
1

Your node isn't updated.

remove your node version, download and install the latest version.

in project root run:

npm rebuild

And try again:

npm run production

PS.: If you don't want remove your node version, download the new and run with this.

Beeman answered 14/2, 2017 at 11:54 Comment(0)
N
1
$ npm run dev

> @ dev /var/www/laratest2
> npm run development


> @ development /var/www/laratest2
> cross-env NODE_ENV=development node_modules/webpack/bin/webpack.js --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js

/var/www/laratest2/node_modules/laravel-mix/setup/webpack.config.js:6
let mix = require('../src/index');
^^^

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:374:25)
    at Object.Module._extensions..js (module.js:417:10)
    at Module.load (module.js:344:32)
    at Function.Module._load (module.js:301:12)
    at Module.require (module.js:354:17)
    at require (internal/module.js:12:17)
    at requireConfig (/var/www/laratest2/node_modules/webpack/bin/convert-argv.js:97:18)
    at /var/www/laratest2/node_modules/webpack/bin/convert-argv.js:104:17
    at Array.forEach (native)

npm ERR! Linux 4.15.0-45-generic
npm ERR! argv "/usr/bin/nodejs" "/usr/bin/npm" "run" "development"
npm ERR! node v4.2.6
npm ERR! npm  v3.5.2
npm ERR! code ELIFECYCLE
npm ERR! @ development: `cross-env NODE_ENV=development 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 @ development script 'cross-env NODE_ENV=development node_modules/webpack/bin/webpack.js --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js'.
npm ERR! Make sure you have the latest version of node.js and npm installed.
npm ERR! If you do, 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!     cross-env NODE_ENV=development node_modules/webpack/bin/webpack.js --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js
npm ERR! You can get information on how to open an issue for this project with:
npm ERR!     npm bugs 
npm ERR! Or if that isn't available, 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!     /var/www/laratest2/npm-debug.log

npm ERR! Linux 4.15.0-45-generic
npm ERR! argv "/usr/bin/nodejs" "/usr/bin/npm" "run" "dev"
npm ERR! node v4.2.6
npm ERR! npm  v3.5.2
npm ERR! code ELIFECYCLE
npm ERR! @ dev: `npm run development`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the @ dev script 'npm run development'.
npm ERR! Make sure you have the latest version of node.js and npm installed.
npm ERR! If you do, 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!     npm run development
npm ERR! You can get information on how to open an issue for this project with:
npm ERR!     npm bugs 
npm ERR! Or if that isn't available, 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!     /var/www/laratest2/npm-debug.log

When i was running npm run dev i was getting the above error, its because i had older version of nodejs.

Use the following command to update the nodejs package.

curl -sL https://deb.nodesource.com/setup_11.x | sudo -E bash -

sudo apt-get install -y nodejs

You can check version using:

node -v
npm -v

now run the following command:

npm rebuild

Then try running npm run dev , its working for me.

OUTPUT

 DONE  Compiled successfully in 9626ms                                                                                               3:14:07 PM

       Asset    Size  Chunks             Chunk Names
/css/app.css  226 kB       0  [emitted]  mix
Naissant answered 4/8, 2020 at 10:6 Comment(0)
C
0

I was also facing the same issue and I have tried to run these command and now it is working. But before that uninstall the nodejs first

sudo apt-get update

apt-cache policy nodejs

sudo apt install nodejs

I hope your problem will be solved.

Candlenut answered 8/3, 2019 at 5:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.