最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Fail to create variable using eval in Node.js ES6 - Stack Overflow

programmeradmin1浏览0评论

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?

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?

Share Improve this question edited Sep 4, 2018 at 7:09 Dan D asked Sep 4, 2018 at 7:01 Dan DDan D 8,6617 gold badges45 silver badges79 bronze badges 5
  • It's probably a scoping issue. The scope of the variable is just within eval. – Barmar Commented Sep 4, 2018 at 7:03
  • it's global scope, only 2 lines of code – Dan D Commented Sep 4, 2018 at 7:04
  • 2 I suspect when you enable modules, scoping rules change. – Barmar Commented Sep 4, 2018 at 7:05
  • i still dont get it, the 'eval' and the 'console.log' are in the same scope, aren't they? – Dan D Commented Sep 4, 2018 at 7:07
  • I'm suggesting that eval creates a nested scope for the expressions being executed. – Barmar Commented Sep 4, 2018 at 7:08
Add a ment  | 

2 Answers 2

Reset to default 8

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

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);
发布评论

评论列表(0)

  1. 暂无评论