SyntaxError:在严格模式下使用const

时间:2022-09-04 11:04:30

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:

我处理节点。在我的一个js文件中,我在“严格模式”中使用const。当我试图运行它时,我得到了一个错误:

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 个解决方案

#1


265  

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.

const和let是ECMAScript 2015(即ES6和Harmony)的一部分,在Node默认情况下没有启用。js 0.10或0.12。由于节点。js 4。“所有的shipping [ES2015]特性,V8认为是稳定的,在Node上默认打开。”不需要任何类型的运行时标志。节点。js文档概述了默认启用的ES2015特性,以及哪些特性需要运行时标志。通过升级到节点。js 4。错误应该消失。

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中启用一些ECMAScript 2015特性(包括const和let)。js 0.10和0.12;用一个harmony标志启动节点程序,否则会出现语法错误。例如:

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.

这完全取决于您的严格js位于哪一边。我建议使用严格的模式,在服务器端使用const声明,并使用和谐标志启动服务器。对于客户端,您应该使用Babel或类似工具将ES2015转换为ES5,因为并非所有客户端浏览器都支持const声明。

#2


219  

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

如果这种情况发生在node . js中,那是因为nodejs的旧版本。更新节点使用,

1) Clear NPM's cache:

1)明确NPM的缓存:

sudo npm cache clean -f

2) Install a little helper called 'n'

2)安装一个名为“n”的小助手

sudo npm install -g n

3) Install latest stable NodeJS version

3)安装最新稳定的NodeJS版本

sudo n stable

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

更新来自https://*.com/a/19584407/698072的nodejs指令

#3


17  

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


16  

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).

当执行代码的节点版本比预期的要早时,通常会发生此错误。(即0.12或以上)。

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

如果您正在使用nvm,请确保使用了正确的节点版本。您可以检查节点上的兼容性。绿色为const在严格模式下

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

我在另一篇文章中发现了一个类似的问题,并将我的答案详细地贴在那里

#5


12  

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.

如果您正在使用NVM,那么您可能没有为正在运行的代码启用正确的节点版本。重新启动后,您的默认节点版本将更改为系统默认。

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.

在与反应物-native合作时遇到了这个问题。只需使用nvm使用正确的节点版本来解决这个问题。

#6


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.

自从这个问题被提出以来,const关键字的草稿已经成为了ECMAScript 2015的一部分。还有节点的当前版本。js支持没有-harmony标志的const声明。

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

有了上面所说的,现在可以使用app.js运行node app.js:

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

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

获得语法糖和严格模式的好处。

#7


0  

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.

ECMAScript不支持const。当你指定了严格模式后,你会得到语法错误。如果希望代码与所有浏览器兼容,则需要使用var而不是const。我知道,这不是理想的解决方案,但它就是它。有一些方法可以在JavaScript中创建只读属性(请参阅在纯JavaScript中实现只读属性?),但我认为根据您的场景可能会过度使用。

Below is browser compatibility note from MDN:

以下是来自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).

const的当前实现是一个特定于mozilla的扩展,不是ECMAScript 5的一部分。它支持Firefox和Chrome (V8)。在Safari 5.1.7和Opera 12.00中,如果您在这些浏览器中定义了一个const变量,那么您仍然可以在以后更改它的值。在Internet Explorer 6-10中不支持它,但在Internet Explorer 11中包含它。const关键字当前在函数作用域中声明常量(如用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).

至少从第13版开始,如果重新声明一个常量,Firefox就会抛出一个类型错误。如果您将另一个值赋给一个常量,那么所有主要的浏览器都不会产生任何通知或错误。这种操作的返回值是指定的新值,但是重新赋值只在Firefox和Chrome中失败(至少在版本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.

const将由ECMAScript 6定义,但是语义不同。与let语句声明的变量类似,使用const声明的常量将在块范围内。

#8


-1  

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.

随着Chrome 41的发布,在严格模式下使用const是可行的。目前,Chrome 41测试版已经发布并支持它。

#9


-4  

cd /
npm install -g nave
nave use 6.11.1
node app.js

#1


265  

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.

const和let是ECMAScript 2015(即ES6和Harmony)的一部分,在Node默认情况下没有启用。js 0.10或0.12。由于节点。js 4。“所有的shipping [ES2015]特性,V8认为是稳定的,在Node上默认打开。”不需要任何类型的运行时标志。节点。js文档概述了默认启用的ES2015特性,以及哪些特性需要运行时标志。通过升级到节点。js 4。错误应该消失。

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中启用一些ECMAScript 2015特性(包括const和let)。js 0.10和0.12;用一个harmony标志启动节点程序,否则会出现语法错误。例如:

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.

这完全取决于您的严格js位于哪一边。我建议使用严格的模式,在服务器端使用const声明,并使用和谐标志启动服务器。对于客户端,您应该使用Babel或类似工具将ES2015转换为ES5,因为并非所有客户端浏览器都支持const声明。

#2


219  

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

如果这种情况发生在node . js中,那是因为nodejs的旧版本。更新节点使用,

1) Clear NPM's cache:

1)明确NPM的缓存:

sudo npm cache clean -f

2) Install a little helper called 'n'

2)安装一个名为“n”的小助手

sudo npm install -g n

3) Install latest stable NodeJS version

3)安装最新稳定的NodeJS版本

sudo n stable

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

更新来自https://*.com/a/19584407/698072的nodejs指令

#3


17  

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


16  

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).

当执行代码的节点版本比预期的要早时,通常会发生此错误。(即0.12或以上)。

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

如果您正在使用nvm,请确保使用了正确的节点版本。您可以检查节点上的兼容性。绿色为const在严格模式下

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

我在另一篇文章中发现了一个类似的问题,并将我的答案详细地贴在那里

#5


12  

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.

如果您正在使用NVM,那么您可能没有为正在运行的代码启用正确的节点版本。重新启动后,您的默认节点版本将更改为系统默认。

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.

在与反应物-native合作时遇到了这个问题。只需使用nvm使用正确的节点版本来解决这个问题。

#6


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.

自从这个问题被提出以来,const关键字的草稿已经成为了ECMAScript 2015的一部分。还有节点的当前版本。js支持没有-harmony标志的const声明。

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

有了上面所说的,现在可以使用app.js运行node app.js:

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

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

获得语法糖和严格模式的好处。

#7


0  

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.

ECMAScript不支持const。当你指定了严格模式后,你会得到语法错误。如果希望代码与所有浏览器兼容,则需要使用var而不是const。我知道,这不是理想的解决方案,但它就是它。有一些方法可以在JavaScript中创建只读属性(请参阅在纯JavaScript中实现只读属性?),但我认为根据您的场景可能会过度使用。

Below is browser compatibility note from MDN:

以下是来自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).

const的当前实现是一个特定于mozilla的扩展,不是ECMAScript 5的一部分。它支持Firefox和Chrome (V8)。在Safari 5.1.7和Opera 12.00中,如果您在这些浏览器中定义了一个const变量,那么您仍然可以在以后更改它的值。在Internet Explorer 6-10中不支持它,但在Internet Explorer 11中包含它。const关键字当前在函数作用域中声明常量(如用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).

至少从第13版开始,如果重新声明一个常量,Firefox就会抛出一个类型错误。如果您将另一个值赋给一个常量,那么所有主要的浏览器都不会产生任何通知或错误。这种操作的返回值是指定的新值,但是重新赋值只在Firefox和Chrome中失败(至少在版本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.

const将由ECMAScript 6定义,但是语义不同。与let语句声明的变量类似,使用const声明的常量将在块范围内。

#8


-1  

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.

随着Chrome 41的发布,在严格模式下使用const是可行的。目前,Chrome 41测试版已经发布并支持它。

#9


-4  

cd /
npm install -g nave
nave use 6.11.1
node app.js