Reputation: 100020
Normally when pointing node to a folder, if there is an index.js
file there, we don't need to specify it.
I installed an NPM dependency that I am working on, npm install --save-dev suman
.
Suman has an index.js file at the root of its NPM project.
In my NPM scripts for a project that depends on suman, I have this:
"scripts": {
"test": "node node_modules/suman/index.js --rnr test"
}
The above works!
But this doesn't work:
"scripts": {
"test": "node node_modules/suman --rnr test"
}
Why is this?
Perhaps the answer is obvious - the require function in node is capable of such behavior, but node itself is not.
Upvotes: 1
Views: 161
Reputation: 12458
Sorry that I can't include my thoughts simply as a comment rather than as an answer but I don't yet have enough reputation points to comment. However, perhaps the following is relevant:
If, in your problem, the node
command finds the appropriate index.js
the same way as is shown in the node documentation for modules, then the documented look-up routine will find a different index.js
before finding the one that (I think) you want. Specifically, before trying node_modules/suman/index.js
(which is the one you want), node will look to see if the "main"
field in node_modules/suman/package.json
exists. In suman, it does, and it references lib/index.js
(i.e. node_modules/suman/lib/index.js
) which is different than the one you want. Is that relevant?
UPDATE: Just to clarify my answer with more generic language...
Your original question is a valid one because, in the absence of any other complications, if dir/index.js
exists, then both of the following
node dir/index.js ...
node dir ...
should refer to the same (default) file, i.e. dir/index.js
. Thus it is reasonable to be confused when those two commands give different results. The explanation is that there IS a complication: the dir/package.json
file effectively redirects node dir ...
to use a non-default file. This is because the "main"
property refers to dir/some/other/file.js
. The rules of precedence specify that, if only a directory name is used, this other file will be used before the default. Thus, you are forced to continue to use the longer and more explicit "working" command from your question.
Upvotes: 1
Reputation: 15903
You need to add the correct path:
"scripts": {
"test": "node ./node_modules/suman --rnr test"
}
Notice the ./
After thinking about this a bit more, It may not be this easy. But take a look at this link: https://docs.npmjs.com/misc/scripts - @elssar seems to be on the right track.
Upvotes: 1
Reputation: 5871
Since the library has a bin
in its package.json
, you don't need to explicitly provide the path to it. Just run node suman --rnr test
and npm
will take care of using the correct file.
When you install a dependency with a binary in your node project, npm creates a symlink to that file in ./node_modules/.bin
and uses those when running npm scripts.
Upvotes: 1