AR7 AR7 - 1 year ago 79
Javascript Question

node can't find modules when using process substitution in bash script

I have a command that I normally would run in the

section of my
, but the problem is that
npm run
, which has no support for process substitution.

I need process substitution because I would like to pass args to the result of
flow-remove-types index.js
, and can't do that with a pipe.

So I wrote the following script:



case $1 in
node <(flow-remove-types index.js) ${@:2}
flow-remove-types index.js | node

Now the issue is that when I use process substitution, node can't find the installed modules. Running with the
command in the case statement that runs
flow-remove-types index.js | node
has no problems with finding the modules, but when I run the command
with process substitution. I get the following stack trace

throw err;

Error: Cannot find module 'moment'
at Function.Module._resolveFilename (module.js:455:15)
at Function.Module._load (module.js:403:25)
at Module.require (module.js:483:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (/dev/fd/63:3:16)
at Module._compile (module.js:556:32)
at Object.Module._extensions..js (module.js:565:10)
at Module.load (module.js:473:32)
at tryModuleLoad (module.js:432:12)
at Function.Module._load (module.js:424:3)

Does anyone know why this happens? Why wouldn't node have access to the modules installed in

rsp rsp
Answer Source

My guess is that when you run:

flow-remove-types index.js | node

then node tries to find the node_modules directory in the current working directory because it doesn't know where the script is located - it gets it via STDIN.

But when you run:

node <(flow-remove-types index.js)

then node doesn't get the script on STDIN, but in a file like /dev/fd/63 or something like that, and it gets its filename as the command line argument.

Try this to see what it gets evaluated to:

echo node <(flow-remove-types index.js)

It then probably tries to find the node_modules directory in /dev/fd and it's not there.

Maybe running something like this would help:

NODE_PATH=`pwd`/node_modules node <(flow-remove-types index.js)

I can't test it because for me running:

node <(cat app.js)

doesn't work even if app.js doesn't require any modules.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download