简体   繁体   English

语法错误:在严格模式下使用 const

[英]SyntaxError: Use of const in strict mode

I'm working with node.js, and in one of my js files I'm using const in "strict mode" .我正在使用 node.js,在我的一个 js 文件中,我在"strict mode"使用const 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

The const and let are part of ECMAScript 2015 (aka ES6 and Harmony), and was not enabled by default in Node.js 0.10 or 0.12. constlet是ECMAScript 2015(又名ES6和Harmony)的一部分,默认情况下在Node.js 0.10或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 4.x开始,“所有出货[ES2015]功能,V8认为稳定,默认情况下在Node.js上打开,不需要任何类型的运行时标志。” Node.js docs has an overview of what ES2015 features are enabled by default, and which who require a runtime flag . Node.js文档概述了默认情况下启用的ES2015功能,以及需要运行时标志的功能 So by upgrading to Node.js 4.x or newer the error should disappear. 因此,通过升级到Node.js 4.x或更新版本,错误应该消失。

To enable some of the ECMAScript 2015 features (including const and let ) in Node.js 0.10 and 0.12; 在Node.js 0.10和0.12中启用一些ECMAScript 2015功能(包括constlet ); 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. 这完全取决于您的严格js所在的一侧。 I would recommend using strict mode with const declarations on your server side and start the server with the harmony flag. 我建议在服务器端使用带有const声明的严格模式,并使用和声标志启动服务器。 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. 对于客户端,您应该使用Babel或类似工具将ES2015转换为ES5,因为并非所有客户端浏览器都支持const声明。

If this is happening in nodejs, it is due to the older version of nodejs. 如果在nodejs中发生这种情况,则归因于较旧版本的nodejs。 Update node by using, 使用更新节点,

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://stackoverflow.com/a/19584407/698072 更新来自https://stackoverflow.com/a/19584407/698072的 nodejs指令

Usually this error occurs when the version of node against which the code is being executed is older than expected. 通常,当执行代码的节点版本比预期的更旧时会发生此错误。 (ie 0.12 or older). (即0.12或更早)。

if you are using nvm than please ensure that you have the right version of node being used. 如果您使用的是nvm,请确保您使用的节点版本正确。 You can check the compatibility on node.green for const under strict mode 您可以在严格模式下检查node.green上的const兼容性

I found a similar issue on another post and posted my answer there in detail 我在另一篇文章中发现了类似的问题,并在那里详细发布了我的答案

更新节点后的一个重要步骤是将节点二进制文件链接到最新安装的节点版本

sudo ln -sf /usr/local/n/versions/node/6.0.0/bin/node /usr/bin/node  

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. 如果您使用的是NVM,则可能没有为正在运行的代码启用正确版本的节点。 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. 只需使用nvm即可使用正确版本的节点来解决此问题。

Since the time the question was asked, the draft for the const keyword is already a living standard as part of ECMAScript 2015 . 自提出问题以来, const关键字的草案已经成为ECMAScript 2015的一部分。 Also the current version of Node.js supports const declarations without the --harmony flag. 此外,当前版本的Node.js 支持没有--harmony标志的const声明

With the above said you can now run node app.js , with app.js : 有了上面说过,您现在可以使用app.js运行node app.js app.js

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

getting both the syntax sugar and the benefits of strict mode. 获得语法糖和严格模式的好处。

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. 这不是OP正在寻找的解决方案,但可能会帮助处理类似问题的其他人。

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. 我正在使用PM2来运行项目,在给定的登台服务器中,我有一个非常旧的Node,NPM和PM2版本。 I updated everything, however, I kept keeping the same error: 我更新了所有内容,但是,我一直保持相同的错误:

SyntaxError: Use of const in strict mode. SyntaxError:在严格模式下使用const。

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 : 直到我在pm2 start时发现警告:

>>>> In-memory PM2 is out-of-date, do: >>>>内存中的PM2已过时,请执行以下操作:
>>>> $ pm2 update >>>> $ pm2更新
In memory PM2 version: 0.15.10 在内存中PM2版本:0.15.10
Local PM2 version: 3.2.9 本地PM2版本:3.2.9

Gotcha! 疑难杂症! After running pm2 update , I finally was able to get the application running as expected. 运行pm2 update ,我终于能够按预期运行应用程序了。 No "const in strict mode" errors anymore. 没有“const in strict mode”错误了。

I was using pm2 to start and maintain the node processes.我使用 pm2 来启动和维护节点进程。

From the CLI it worked perfectly.在 CLI 中,它运行良好。

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

However, I set up a cronjob and I got the syntax error.但是,我设置了一个 cronjob 并且出现了语法错误。

Then wrote a cronjob to check which node and node -v and surprisingly, it was a different path and version.然后写了一个cronjob来检查which nodenode -v ,令人惊讶的是,它是不同的路径和版本。

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:要修复 cronjob,我必须使用标志--interpreter for --interpreter ,如下所示:

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

const is not supported by ECMAScript. ECMAScript不支持const。 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. 如果希望代码与所有浏览器兼容,则需要使用var而不是const。 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. 有一些方法可以在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). const的当前实现是特定于Mozilla的扩展,不是ECMAScript 5的一部分。它在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. 从Safari 5.1.7和Opera 12.00开始,如果在这些浏览器中使用const定义变量,您仍可以在以后更改其值。 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). 它在Internet Explorer 6-10中不受支持,但包含在Internet Explorer 11中.const关键字当前在函数作用域中声明了常量(就像用var声明的变量一样)。

Firefox, at least since version 13, throws a TypeError if you redeclare a constant. Firefox,至少从版本13开始,如果你重新声明一个常量,就会抛出一个TypeError。 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). 此类操作的返回值是指定的新值,但重新分配仅在Firefox和Chrome中失败(至少从版本20开始)。

const is going to be defined by ECMAScript 6, but with different semantics. const将由ECMAScript 6定义,但具有不同的语义。 Similar to variables declared with the let statement, constants declared with const will be block-scoped. 与使用let语句声明的变量类似,使用const声明的常量将是块作用域。

随着Chrome 41的发布,可以在严格模式下使用const 。目前, Chrome 41 Beta已经发布并支持它。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 const os = require('os')SyntaxError:在严格模式下使用const - const os = require('os') SyntaxError: Use of const in strict mode Visual Studio 任务运行程序“语法错误:在严格模式下使用 const。” - Visual Studio Task Runner "SyntaxError: Use of const in strict mode." 未捕获的SyntaxError:在严格模式下使用const Travis-ci业力测试与铬投掷 - Uncaught SyntaxError: Use of const in strict mode | Travis-ci karma test with chromium throws npm start命令我遇到了语法错误:在严格模式下使用const - npm start command I am getting syntaxerror: use of const in strict mode 严格模式下函数的SyntaxError - SyntaxError for functions in strict mode Typescript和Uncaught SyntaxError:在严格模式之外尚不支持块范围的声明(let,const,function,class) - Typescript and Uncaught SyntaxError: Block-scoped declarations (let, const, function, class) not yet supported outside strict mode SyntaxError:'with'语句在严格模式下无效 - SyntaxError: 'with' statements are not valid in strict mode 未捕获的语法错误:意外的严格模式保留字 - Uncaught SyntaxError: Unexpected strict mode reserved word Reactjs - SyntaxError: Octal literals are not allowed in strict mode - Reactjs - SyntaxError: Octal literals are not allowed in strict mode 函数如何继承严格模式(“use strict”;)? - How is strict mode ("use strict";) inherited by functions?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM