Fail to create variable using eval in Node.js ES6

时光总嘲笑我的痴心妄想 提交于 2019-12-23 04:39:11

问题


It seems not possible to create a variable using eval in Node.js ES6 but I can't understand why. This happens to me on CentOS 7, but I don't believe OS is the problem here.

Regular Node.js file (test.js):

eval("var a=1");
console.log(a);

Make the same file with .mjs extension to run with Node.js ES6 (test.mjs):

eval("var a=1");
console.log(a);

After that, run the 2 files with Node.js, and Node.js ES6:

$ node test.js
1

$ node --experimental-modules test.mjs
(node:9966) ExperimentalWarning: The ESM module loader is experimental.
ReferenceError: a is not defined
    at file:///temp/test.mjs:2:13
    at ModuleJob.run (internal/modules/esm/module_job.js:96:12)

Is it an issue related to ES6? I tried on browser's console and the problem is the same:

>> eval("var a=1"); console.log(a);
   1

>> class c { static f(){ eval("var a=1"); console.log(a); } }
   c.f()
   ReferenceError: a is not defined

I'm using Node.js 10.9.0, is it a bug or there's a reason behind it?


回答1:


In strict mode, variables created inside an eval() statement are available only to that code. It does not create new variables in your local scope (here's a good article on the topic) whereas it can create variables in the local scope when not in strict mode.

And, mjs modules run in strict mode by default. A regular node.js script file is not in strict mode by default. So, the difference in strict mode setting causes a difference in behavior of eval().




回答2:


With the answer from @jfriend00 and from my testing:

Calling eval directly doesn't work in es6 class or .mjs file:

eval("var a=1");
console.log(a);

However, calling eval INDIRECTLY does work in es6 class or .mjs file:

var geval = eval;
geval("var a=1");
console.log(a);


来源:https://stackoverflow.com/questions/52160557/fail-to-create-variable-using-eval-in-node-js-es6

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!