I'm using Node LTS v14. I have a list of JavaScript files as strings that I'd like to evaluate. With CJS, this was possible using e.g. eval:
Welcome to Node.js v14.15.4.
Type ".help" for more information.
> eval("const fs = require('fs')");
undefined
With ESM, however:
Welcome to Node.js v14.15.4.
Type ".help" for more information.
> eval("import fs from 'fs'");
Uncaught SyntaxError: Cannot use import statement outside a module
Apart from using the dynamic import function, is there any possibility to use eval to evaluate ESM JavaScript? Is there any other reasonable way of evaluating? I found an approach that seems to work partially with Blobs and the dynamic import function (NOTE: It cannot resolve a dependency!). I've also looked at Workers, but it seems that issue is still open #30682. vm doesn't look like a fit either.
Any suggestions?
My expectation would either be that eval treats any input as a module, or that at least there's somehow a way to force eval to treat an input as a module, e.g. eval("string", { type: "module"});. Otherwise, how can any JavaScript be evaluate that contains the import ... from statement.
I'm using Node LTS v14. I have a list of JavaScript files as strings that I'd like to evaluate. With CJS, this was possible using e.g.
eval:With ESM, however:
Apart from using the dynamic
importfunction, is there any possibility to useevalto evaluate ESM JavaScript? Is there any other reasonable way of evaluating? I found an approach that seems to work partially with Blobs and the dynamic import function (NOTE: It cannot resolve a dependency!). I've also looked at Workers, but it seems that issue is still open #30682.vmdoesn't look like a fit either.Any suggestions?
My expectation would either be that
evaltreats any input as a module, or that at least there's somehow a way to force eval to treat an input as a module, e.g.eval("string", { type: "module"});. Otherwise, how can any JavaScript be evaluate that contains the import ... from statement.