250

I'm working with node.js, and in one of my js files I'm using const in "strict mode". When trying to run it, I'm getting an error:

SyntaxError: Use of const in strict mode.

What is the best practice to do this?

Edit:

'use strict'
const MAX_IMAGE_SIZE = 1024*1024; // 1 MB
7
  • @thefourtheye check in chrome, ff is working fine. Mar 24, 2014 at 7:23
  • use var. Add semikolon 'use strict'; Mar 24, 2014 at 7:24
  • @ZaheerAhmed Actually I am using Node.js yaar. It works fine in that. Mar 24, 2014 at 7:24
  • Aah... Fails in ideone.com Mar 24, 2014 at 7:25
  • After updating the latest Node version v5.3.0, it works well now.
    – zangw
    Dec 29, 2015 at 9:39

11 Answers 11

293

The const and let are part of ECMAScript 2015 (a.k.a. ES6 and Harmony), and was not enabled by default in Node.js 0.10 or 0.12. Since Node.js 4.x, “All shipping [ES2015] features, which V8 considers stable, are turned on by default on Node.js and do NOT require any kind of runtime flag.”. Node.js docs has an overview of what ES2015 features are enabled by default, and which who require a runtime flag. So by upgrading to Node.js 4.x or newer the error should disappear.

To enable some of the ECMAScript 2015 features (including const and let) in Node.js 0.10 and 0.12; start your node program with a harmony flag, otherwise you will get a syntax error. For example:

node --harmony app.js

It all depends on which side your strict js is located. I would recommend using strict mode with const declarations on your server side and start the server with the harmony flag. For the client side, you should use Babel or similar tool to convert ES2015 to ES5, since not all client browsers support the const declarations.

4
  • 8
    This is the most complete and relevant answer.
    – zamnuts
    Jun 17, 2014 at 17:10
  • Strict mode can also applied using a flag (rather than a literal expression) node --harmony --use_strict app.js Which is nice.
    – Adria
    Aug 21, 2014 at 14:25
  • 7
    I would like to advice against using the "use_strict" flag. It's running someone else's code in an interpreter that it wasn't written for. Always use 'use strict'; yourself, but let third party code decide for itself. Sep 7, 2014 at 10:19
  • 1
    FWIW I've found the same issue running node v8.1 but armfh version (raspberry pi).
    – James
    Jun 27, 2017 at 8:41
242

If this is happening in nodejs, it is due to the older version of nodejs. Update node by using,

1) Clear NPM's cache:

sudo npm cache clean -f

2) Install a little helper called 'n'

sudo npm install -g n

3) Install latest stable NodeJS version

sudo n stable

Update nodejs instructions taken from, https://stackoverflow.com/a/19584407/698072

6
  • 3
    I got this "npm ERR! notsup Unsupported npm ERR! notsup Not compatible with your operating system or architecture: [email protected]" while trying to do step 2. Good times.
    – VSO
    Sep 11, 2016 at 18:30
  • 1
    Depending on your use-case, you may find it safer to specify the node version. For example, as of the time of this writing, sudo n 6.9.4 will install the latest LTS, where sudo n stable will install 7.4.0. Obviously, that's a breaking change, so take care with n stable. Alternatively, sudo n lts installs the most recent LTS.
    – jfmercer
    Jan 6, 2017 at 22:46
  • 1
    Works on centos 6.5. Earlier I was getting the following error " SyntaxError: Use of const in strict mode ". Thanks May 16, 2017 at 5:57
  • 1
    This resolved the issue for me on a legacy machine where the --harmony flag did not. Setup: Trying to launch a screen npm start monitor using scripts in crontab caused the strict error where running it from the cli did not. Previous node version was 0.10.48 running on ubuntu 12.04.5 LTS I followed this step and the one changing the softlink /usr/bin/node from /etc/alternatives/node to /usr/local/n/versions/node/8.0.0/bin/node and everything just worked. Thank you for this! Jun 2, 2017 at 19:28
  • 1
    Badass thanks for this, it not only solved that problem but all the inherent little foibles when building SCA Local
    – jk121960
    Jun 9, 2017 at 12:04
24

Usually this error occurs when the version of node against which the code is being executed is older than expected. (i.e. 0.12 or older).

if you are using nvm than please ensure that you have the right version of node being used. You can check the compatibility on node.green for const under strict mode

I found a similar issue on another post and posted my answer there in detail

1
  • 1
    I ran into this when running my Node project in WebStorm. My local version in the repo was running Node 8.2.1, but WebStorm looked at my /usr/bin/node folder, which had 0.10.46.
    – Shadoninja
    Mar 30, 2018 at 14:54
20

One important step after you update your node is to link your node binary to the latest installed node version

sudo ln -sf /usr/local/n/versions/node/6.0.0/bin/node /usr/bin/node  
4
  • 1
    How can I figure out which version just got installed? Feb 16, 2017 at 20:21
  • 1
    You can find where your current version of node is installed by running which node
    – Steve
    May 1, 2017 at 23:12
  • Also you will have seen the directory when running "n stable" In this line: mkdir : /usr/local/n/versions/node/8.0.0 So you could just go to /usr/local/n/versions/node/ and see what directories exist. Jun 2, 2017 at 19:33
  • @JoseLlausas node --version prints out the version number e.g. v0.10.29
    – Markus
    Feb 2, 2020 at 17:10
15

This is probably not the solution for everyone, but it was for me.

If you are using NVM, you might not have enabled the right version of node for the code you are running. After you reboot, your default version of node changes back to the system default.

Was running into this when working with react-native which had been working fine. Just use nvm to use the right version of node to solve this problem.

3
  • 2
    and any code to illustrate your stated "solution" above? Jul 29, 2016 at 23:16
  • yes, this was my initial issue. although i had to switch to another linux server anyway due to node and gcc version compatibility issues on an outdated centos version.
    – Robot70
    Dec 13, 2017 at 18:54
  • @Kermit_ice_tea The same code in the question. The bug is in the version of node, using NVM to set it to a version that does not have that bug makes that code pass. nvm use version-number-you-need is all you have to do.
    – boatcoder
    Dec 14, 2017 at 20:35
8

Since the time the question was asked, the draft for the const keyword is already a living standard as part of ECMAScript 2015. Also the current version of Node.js supports const declarations without the --harmony flag.

With the above said you can now run node app.js, with app.js:

'use strict';
const MB = 1024 * 1024;
...

getting both the syntax sugar and the benefits of strict mode.

1
  • 1
    A megabyte MB is a power of ten, while a mebibyte MiB is a power of two. i.e. 1MB = 1e6 and 1MiB = 1024 * 1024. See wikipedia: Mebibyte
    – DJDaveMark
    Nov 5, 2018 at 8:21
0

I had a similar issue recently and ended up in this Q&A. This is not the solution the OP was looking for but may help others with a similar issue.

I'm using PM2 to run a project and in a given staging server I had a really old version of Node, NPM and PM2. I updated everything, however, I kept keeping the same error:

SyntaxError: Use of const in strict mode.

I tried to stop and start the application several times. Also tried to update everything again. Nothing worked. Until I noticed a warning when I ran pm2 start:

>>>> In-memory PM2 is out-of-date, do:
>>>> $ pm2 update
In memory PM2 version: 0.15.10
Local PM2 version: 3.2.9

Gotcha! After running pm2 update, I finally was able to get the application running as expected. No "const in strict mode" errors anymore.

0

I was using pm2 to start and maintain the node processes.

From the CLI it worked perfectly.

which node
/usr/local/bin/node
node -v
v10.15.0

However, I set up a cronjob and I got the syntax error.

Then wrote a cronjob to check which node and node -v and surprisingly, it was a different path and version.

which node
/usr/bin/node
node -v
v0.10.48

To fix the cronjob I had to use the flag --interpreter for pm2, like so:

pm2 start dist/server.js --interpreter=/usr/local/bin/node 
-2

The use of const in strict mode is available with the release of Chrome 41. Currently, Chrome 41 Beta is already released and supports it.

1
  • 6
    Just to clarify, I wanted to comment that since the OP asked for Node.js, browser compatibility isn't relevant. Yet somehow the answer had 4 upvotes, and it obscured other more relevant answers. Aug 16, 2015 at 18:32
-3
cd /
npm install -g nave
nave use 6.11.1
node app.js
5
  • For linux server
    – Ayhmi
    Jul 13, 2017 at 22:12
  • 1
    Welcome to Stack Overflow. When contributing an answer, it's best to give both some code and an explanation.
    – O. Jones
    Jul 13, 2017 at 22:31
  • 2
    Down vote because there is no explanation. why nave package should be installed ..
    – Sean83
    Jul 22, 2017 at 11:58
  • Why would such a solution work? An explanation is needed. Oct 26, 2018 at 7:08
  • Although there is no explanation, for me the error only solved after running the commands described here: npm install -g nave; nave use 6.11.1 +1 Jan 7, 2020 at 15:02
-4

const is not supported by ECMAScript. So after you specify strict mode, you get syntax error. You need to use var instead of const if you want your code to be compatible with all browsers. I know, not the ideal solution, but it is what it is. There are ways to create read-only properties in JavaScript (see Can Read-Only Properties be Implemented in Pure JavaScript?) but I think it might be overkill depending on your scenario.

Below is browser compatibility note from MDN:

Browser compatibility

The current implementation of const is a Mozilla-specific extension and is not part of ECMAScript 5. It is supported in Firefox & Chrome (V8). As of Safari 5.1.7 and Opera 12.00, if you define a variable with const in these browsers, you can still change its value later. It is not supported in Internet Explorer 6-10, but is included in Internet Explorer 11. The const keyword currently declares the constant in the function scope (like variables declared with var).

Firefox, at least since version 13, throws a TypeError if you redeclare a constant. None of the major browsers produce any notices or errors if you assign another value to a constant. The return value of such an operation is that of the new value assigned, but the reassignment is unsuccessful only in Firefox and Chrome (at least since version 20).

const is going to be defined by ECMAScript 6, but with different semantics. Similar to variables declared with the let statement, constants declared with const will be block-scoped.

3
  • 3
    const is supported in ECMAScript 6 and it is widely supported: kangax.github.io/compat-table/es6/#const But you're right in your answer - 'use strict'; and const does not mix. May 29, 2014 at 20:38
  • A quick test shows that const is re-declarable in Safari 7 but not in Opera 21, Chrome 35 and Firefox 29. May 29, 2014 at 21:01
  • 21
    Browse compatibility is of no concern... this is Node.js
    – zamnuts
    Jun 17, 2014 at 17:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.