ES5 code can be run easily with Bash heredoc in terminal:
node <<HEREDOC
var fs = require("fs");
...
HEREDOC
But ES6 code doesn't run, even with the correct --experimental-modules flag:
node --experimental-modules <<HEREDOC
import fs from "fs";
...
HEREDOC
The error shown is:
(node:4130) ExperimentalWarning: The ESM module loader is experimental.
[stdin]:1
import fs from "fs";
^^
SyntaxError: Unexpected identifier
at new Script (vm.js:83:7)
at createScript (vm.js:267:10)
at Proxy.runInThisContext (vm.js:319:10)
at Object.<anonymous> ([stdin]-wrapper:6:22)
at Module._compile (internal/modules/cjs/loader.js:722:30)
at evalScript (internal/bootstrap/node.js:670:27)
at ReadStream.<anonymous> (internal/bootstrap/node.js:340:15)
at ReadStream.emit (events.js:187:15)
at endReadableNT (_stream_readable.js:1098:12)
at process.internalTickCallback (internal/process/next_tick.js:72:19)
It does show this info "ExperimentalWarning: The ESM module loader is experimental." which means Node.js is running correctly with ES6 module feature, however, the import
keyword is just not working.
How to run ES6 code inline in terminal with Bash heredoc? I know I can write the code to file to load as ES6 module normally, but it's a short temporary code, should it be in heredoc better.