2

Node.js 12+ has new command-line option --input-type to run script inside Bash heredoc. However, it doesn't seem to be able to find Node.js modules installed in ./node_modules

Here's how I install a module:

cd test
npm i moment

And run Node.js with script in heredoc:

cd test

#this works
node --experimental-modules --input-type module <<<"import fs from 'fs'"

#this doesn't
node --experimental-modules --input-type module <<<"import moment from 'moment'"

Even installing moment with global option -g, it still yields ERR_MODULE_NOT_FOUND error. Any solutions?

Dee
  • 7,455
  • 6
  • 36
  • 70

1 Answers1

2

As mentioned in this answer: https://stackoverflow.com/a/55568877/5581893

There's no way currently to load modules in Node.js REPL (run 'node' with Bash heredoc), except those built-in modules like: fs, http, etc.

So the only work-around now is something this way, for example, 'moment' module:

#bash script
echo '//begin
import moment from "moment";
...
//end' >test.mjs

node --experimental-modules test.mjs
Dee
  • 7,455
  • 6
  • 36
  • 70
  • 1
    Yeah, I don't totally get it, but it seems like only works if you also pass in an `eval` string argument as well https://dmitripavlutin.com/ecmascript-modules-nodejs/ – Cameron Nov 05 '21 at 23:10