charliesneath
charliesneath

Reputation: 2039

How does require work when calling node module directly?

When calling a node module directly, e.g. $ ./node_modules/.bin/webpack -d, how is the module aware of how to handle any require functions?

I understand how the require function works, but I'm confused where it is defined.

I had assumed that using something like $ npm start would give context to handle require, but how does Node get involved (and define how to handle require) when the module is called directly?

Upvotes: 0

Views: 27

Answers (1)

robertklep
robertklep

Reputation: 203529

You're not calling the module directly, you're calling an executable that got installed as part of a package.

That executable runs a full Node interpreter, with the contents of the executable file as the script.

Basically, it's similar to running this on the command line:

node ./node_modules/.bin/webpack

Upvotes: 1

Related Questions