SyntaxError: Use of const in strict mode

2019-01-01 03:05发布

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

9条回答
萌妹纸的霸气范
2楼-- · 2019-01-01 03:42

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楼-- · 2019-01-01 03:46

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.

查看更多
荒废的爱情
4楼-- · 2019-01-01 03:47

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

查看更多
永恒的永恒
5楼-- · 2019-01-01 03:50

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.

查看更多
其实,你不懂
6楼-- · 2019-01-01 03:53

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.

查看更多
十年一品温如言
7楼-- · 2019-01-01 03:55

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  
查看更多
登录 后发表回答