Search code examples
node.jsbashecmascript-6heredocmjs

Can not load local dir Node.js modules when using --input-type


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?


Solution

  • 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