Search code examples
javascriptnode.jsistanbul

node cli - require file before running another


So I have a setup.js file that I would like to execute/setup for another file.

Currently the setup file is just simply this:

require("module-alias/register")

However, it may expand in the future which is why I'm asking this question.

I'm wondering if it's possible to require or execute the file to setup variables, etc. while executing another,

(just an example from nyc, i'd like to know if node itself can do this):

nyc mocha-webpack --require ./tests/setup.js ./tests/unit/$FILE"

Solution

  • Yes, --require or -r can be passed as a parameter to node, and it loads the module using the same logic as the require() call. Check the docs for details.

    --require is also implemented as an option to nyc as you pointed out, and mentioned in their documentation