Warm tip: This article is reproduced from stackoverflow.com, please click
javascript node.js makefile

Calling node.js from makefile fails

发布于 2020-03-27 10:26:44

I have a simple hello world js file I run from terminal with node, and everything works fine. When I try to run the exact same thing from my makefile it prints the hello world part but then fails. Here is the relevant target from makefile:

run: ${JS_DIR}/main.js
    node ${JS_DIR}/main.js

And here is the error I get:

$ make run
node /some/dir/main.js
Hello World
makefile:44: recipe for target 'run' failed
make: *** [run] Error 254

What's going on here?

HardcoreHenry 2019-07-03 23:07

A make target will fail if any of its recipes return an error code (i.e., non-zero). node is returning a non-zero. Try running

node /some/dir/main.js; echo $?

to confirm.

You could either update main.js to not return an error (I believe this is done by calling process.exit() with no parameters or a 0 parameter, but I'm not a node or js expert, so take that with a grain of salt).

Alternatively, you could make make ignore the return code by adding a - to the beginning of the recipe:

run: ${JS_DIR}/main.js
    -node ${JS_DIR}/main.js