Regex command line argument changing value between node and npm script - javascript

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)

you need to execute the command using bash to make use of its filename wildcard extensions.
"test": "bash -c 'node index.js --require src/**/*.js'"

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

Related

How does npm know to start my React app when I type `npm start`? [duplicate]

When I type "npm run test" in the command line, npm goes to package.json, to the "scripts" section and tries to match "test" there.
So far so good.
Now, the line behind "test" is the following: "JASMINE_CONFIG_PATH=./spec/support/jasmine.json jasmine-run" but the first part (everything except "jasmine-run") can be removed witout problems. [I have a similarily structured project where it works, so I can test these modifications]
now:
WHERE is npm looking for "jasmine-run" ???
Because since I have a project where the script provided work, I could look for it, but the answer is: in the node_modules folder next to package.json is a module, in whose package.json has, in the "bin" section:
"jasmine-run": "tools/jasmine-run/jasmine-run.js",
However, this exact setup exists in both projects. and in one everything works, while in the other "jasmine-run" cannot be found.
As ana lternative to an answer I'd also take a proper explanation (or source) on how/where npm run actually looks for its stuff, because then I could probably find the error myself.
When you run a script with npm like :
npm run-script <name>
or with a shortcut like
npm test or npm start,
your current package directory's bin directory is placed at the front of your path.
For your and in many of the cases will probably be
./node_modules/.bin/,
which contains a link to your package's executable scripts.
Anyway you have all the explanation how npm runs work here : https://docs.npmjs.com/cli/run-script
npm run adds node_modules/.bin to the PATH provided to scripts. Any binaries provided by locally-installed dependencies can be used without the node_modules/.bin prefix.
Check if you can locate jasmine-run inside node_modules/.bin directory.
For reference have a look at this post: https://docs.npmjs.com/cli/run-script
In case anyone else ends up here looking for the answer after running create-react-app, here is where I found them.
The line from package.json is
"start": "react-scripts start"
In VSCode, open the node_modules and scroll down to react-scripts.
Then open up 'scripts', and there they are.

After npm reads package.json, what runs Electron?

I'm just starting to learn about how JavaScript, HTML, and Electron all work, and I want to know what runs electron . in the "scripts" -> "start" of package.json, because I can't tell what does and that kind of wizardry makes me nervous.
According to the man pages for npm, what npm start does is that it reads the package.json, looks at the script under "scripts" -> "start" -> some_script, and then runs some_script. Sometimes, some_script is something like node foobar.js, which makes sense to me, since I can run that from the command line. NodeJS is executing foobar.js. However, in the case of the electron-api-demos, some_script is electron .
You can download and run electron-api-demos via
git clone https://github.com/electron/electron-api-demos
cd electron-api-demos/
npm install && npm start
In order to try to figure out what is running electron ., I've run it in the node shell, and I've tried running node main.js. I've even tried opening up the node shell and running
electron-api-demos#2.0.2 start $DIR/electron-api-demos
electron .
(which is exactly the output of npm start). None of them worked, because none of them started up the Electron application. At this point I'm very puzzled at how, exactly, the start script gets executed at all.
So I guess my question is: does there exist a command (that I can use on the command line) to start up this Electron application, without using npm? If not, what is npm calling to start up this Electron app?
I apologize if this question has been asked before, but I all the sources I found didn't seem to go into any further detail about what, exactly, is done when npm start is run and how it executes electron . . Thank you for your time!
Command line interfaces installed with npm are put in the node_modules/.bin/ directory. You can't just run them from the command line because that directory isn't in your PATH (unless you put it there, or you installed it globally).
So, if you want to run electron without npm start, you can run ./node_modules/.bin/electron .. Since this is a bit verbose, newer versions of npm provide the command npx to run things without the ./node_modules/.bin/ part, so npx electron . also works.
Since npm scripts often use the packages you've installed, they automatically add node_modules/.bin/ to the PATH before running your command. As a result, the start script can just reference electron directly.
npx can do some other cool things too – npm has a blog post about it.
When you run npm start , it by default run command corresponding "start" key of script property of package.json like
"script":{
"start": "ng serve",
"launch":"electron main.js" or "electron ." // main.js located in the same dir
"test": " ng test"
}
same when you run npm run launch it will trigger the command corresponding of the "launch" key of script property of package.json file. like run electron main.js command and your application will launched.
so if you want to run the your electron application directly like electron main.js then install the electron module globally using command npm install electron -g then simply run the electron main.js command and your application will start.

Can't link command to node script file, no such file or directory

I've got trouble when trying to link a commant to a javascript file with npm on a linux machine. Just assume some simple script and package.json:
package.json
"bin": {
"test": "./test.js"
},
test.js
#!/usr/bin/env node
console.log("test");
installation
$ sudo npm install
$ sudo npm link
Doing this on a windows machine causes no problems. Everything works fine. But on a linux / Raspbian system it keeps telling me:
: no such file or directory
I have already linked the binaries of node with:
$ sudo ln -s /usr/bin/nodejs /usr/bin/node
And to be sure, I've updated to the latest version of node, 7.10.0. But nothing helps. The message still appears. I have no idea whats wrong ...
After a long search, the problem seems to be the windows line endings. They needed to be converted to unix style endings. This was the only change I've made, and now it works. This problem belongs only to the shebang line, because the node interpreter itself has no problem with the original file ...
For this test I installed and used dos2unix, which convert files:
$ sudo apt install dos2unix
$ sudo dos2unix test.js
After doing this, the initial test command works like a charm. So I've change the file on the development version to unix style, so I don't need to run this anymore.

NPM Failing on Trying to Find Shell Script

Console output on npm run build failure:
'.' is not recognized as an internal or external command, operable
program or batch file.
And the relevant npm file:
"scripts": {
"postinstall": "jspm install && npm run build",
"start": "http-server",
"build": "./bin/build-code",
"build-home": "./bin/build-home -dw",
"build-common-deps": "./bin/build-common-deps -dw",
"build-navbar": "./bin/build-navbar -dw",
"build-root": "./bin/build-root -dw",
"build-angular1": "./bin/build-angular1 -dw",
"build-react": "./bin/build-react -w",
"build-preact": "./bin/build-preact -dw",
"build-vanilla": "./bin/build-vanillajs",
"build-angular2": "./bin/build-angular2 -dw"
}
Looks like it's not understanding the pathing to the ./bin/build-code script location. From what I understand, it looks for files from the package.json's location? So, if the app has a bin folder in the same dir as package.json, then this is the correct pathing to the build-code script, which is within the bin folder. What gives? Using PowerShell to run npm run build if it matters.
P.S. I tried with basic Command Prompt - no changes. Someone running the same build (both of us just pulled from repo) on Cygwin said they had to "Change dos endings to unix", which doesn't tell me much and doesn't seem to be the issue.
Looks to me like npm is invoking batch scripts. Batch files are run in CMD.exe (even when invoked from PowerShell), which doesn't recognize / as a path separator. That's where the error message comes from.
Replace the forward slashes with \ (or \\ if they require escaping).

Angular JS 2 can't be installed, why?

I have tried setting up Angular 2 to experiment with on my VPS. The needed steps are here:
https://angular.io/docs/js/latest/quickstart.html
The first command is successful:
$ npm install -g tsd#^0.6.0
But the second step causes an error:
tsd install angular2 es6-promise rx rx-lite
"/usr/bin/env: node: No such file or directory" is the error I get. I have tried installing this in my project folder as root, but that also didn't had success. Does someone know why I get this error? The command doesn't exist, but why not?
Is there an easier way to install this? I have seen "npm install angular2", but I don't know how that works, that's why I tried the steps
You may need to make a symbolic link to node:
ln -s /usr/bin/nodejs /usr/bin/node
(see https://stackoverflow.com/a/26320915/2033574)

Categories