Search code examples
javascriptnode.jsnpmcommand-lineglob

Regex command line argument changing value between node and npm script


Working with node.js, I stumbled upon a behavior I cannot explain regarding command line arguments :

I've got a program which takes a regex to detect test files. This regex is passed via a command line argument :

node index.js --require src/**/*.js

When I do that, I obtain what I imagined. Let's say for the example I got the following files detected in my src folder (I log with a simple console.log(process.argv)) :

a.js
b.js
shared/c.js
shared/d.js

Now if I configure a npm script which launch the same command :

"test": "node index.js --require src/**/*.js

and launch it :

npm test

The result is :

a.js
b.js

Can someone explain to me why this is happening 😅 ? Thanks

I created a mini repo to reproduce for those interested (I run node 16.19.0)


Solution

  • npm uses /bin/sh by default to execute your scripts. Unlike bash or zsh (which you are probably using on the command line), sh does not understand **.

    You can change the shell used by npm with:

    npm config set script-shell bash