npm v9: -- does not pass arguments to script - npm

All of a sudden, if I run
npm run dev -- --host
the dev script is invoked without the --host argument. Seems like -- has stopped working.
Is it a feature in npm v9? I hope not, and couldn't find anything about this in the changelog...
Node 18.12.0, npm 9.5.0.
If I switch to Node 14 (with nvm for Windows), it still works.

Related

On Mac M1, can't run scripts in package.json: - sh: <dependency>: command not found

I just got a Mac Mini M1 for personal use, and I'm trying to run a preexisting React app. I installed nodejs and npm successfully, and running npm install does add the node_modules folder correctly as far as I can tell; but whenever I run npm start or npm run <script>, I get an error. It seems that npm can't access any of the project's dependencies. I've tried this using the rosetta terminal as well with the same results.
For an example, I initialized a new React project with npx create-react-app test_app, then cded into it and ran npm start. I got:
test_repo#0.1.0 start
> react-scripts start
sh: react-scripts: command not found
How do I get these commands to run properly and launch the app?
Here's what I'm using for node and npm:
➜ test_repo npm -v
7.6.0
➜ test_repo node -v
v15.11.0
I found a (very) hacky solution for now. I'm no expert with npm, but what I discovered is that npm scripts refer to dependencies indirectly - for example, having a command that says
"test": "jest"
tells npm to look in node_modules/.bin for a file called jest and to run that.
the issue is something to do with npm understanding this. But it's possible to get around that by putting the address of each dependency in the script, for example:
"test" "node_modules/.bin/jest"
I was able to get things to build this way. If someone comes along with a better answer, please show me up :P

I deleted npm but its still there

I want to delete npm from my system and start with a clean slate. So I run sudo apt remove npm.
Now, I called npm -v and it returned 6.13.7.
What dose that mean? is it possible that I have two or more installations of npm on my system? which one is considered "global"?
If I run whereis npm I get /usr/local/bin/npm and ls over there tells me its a soft link that points to /usr/local/lib/node_modules/npm
Should I simply rm * the folder and the link?
OK, took my risk and rm'ed the library and link. I did the trick. Now calling whereis npm returns an empty list and npm -v returns an error message.
So it probably was a path problem caused by improper installation process for node and npm in the first place.

Visual Studio Code - NPM not found when using NPM SCRIPTS

If I open the integrated terminal and run npm -v it works.
But, if I running it directly from NPM SCRIPTS it doesn't work.
MORE DETAIL AS REQUESTED IN COMMENT
// Edit by Fogmeister
I also have this problem, here is some more detail...
I have a test script defined in package.json...
When I run this script from the root folder command line using npm run test it works...
This script appears in the list of NPM Scripts in VS Code...
When I run from the NPM Scripts explorer it fails...
Even though it says it is running from the same folder.
Try doing, use dnf or apt-get
sudo apt-get/dnf install npm
The npm file should be in /usr/local/bin/npm. If it's not there, install node.js again with the package on their website.

VS Code terminal fails to use npm version from nvm

I'm using nvm on my Terminal and successfully installed node 10.2.1, which also installed npm 6.1.0. However, when I go to my VS Code editor, it gives me warnings in the integrated terminal for:
npm WARN npm npm does not support Node.js v10.2.1
npm WARN npm You should probably upgrade to a newer version of node as we
npm WARN npm can't make any promises that npm will work with this version.
npm WARN npm Supported releases of Node.js are the latest release of 4,6, 7, 8, 9.
Turns out, it is actually using npm 5.5.1 (npm -v).
I check to see what's up with that and tried to dig further and eventually used: which npm on both integrated terminal and Mac's CLI.
Mac's Terminal shows:
/Users/Aiz/.nvm/versions/node/v10.2.1/bin/npm
VS Code's Terminal shows:
/usr/local/bin/npm. Which is interesting, because if you do which node in this terminal, it results in the appropriate /Users/Aiz/.nvm/versions/node/v10.2.1/bin/node.
I'm not sure how to get my VS Code terminal to point to the appropriate npm install through nvm. Not sure if it helps, but I checked npm get prefix and npm -g bin to find /Users/Aiz/.nvm/versions/node/v10.2.1. The only difference I'm finding is where each terminal is using npm from.
I ended up looking through VS Code issues on GitHub and came across something relevant to NVM and node issues. It fixed my issue since the underlying cause was the same.
Essentially what happened is that I had a global install of node before that I removed prior to using NVM but hadn't removed my global install of npm. This was causing conflicts in VS Code's terminal (not Mac's terminal). In order to fix this, you essentially have to find the symlink for which npm and remove the node_modules and npm associated recursively.
Here's the link you'll need: https://github.com/Microsoft/vscode-docs/blob/master/docs/editor/integrated-terminal.md#why-is-nvm-complaining-about-a-prefix-option-when-the-integrated-terminal-is-launched.
Don't forget to restart your editor after.
Another solution is to implement this. https://medium.com/#kinduff/automatic-version-switch-for-nvm-ff9e00ae67f3
Basically, nvm will check for a .nvmrc and switch or default each time you go to a new directory in shell.
It does have a dependency on zsh.
The fix for me on Ubuntu:
A.) sudo apt-get remove nodejs npm
B.) Removed lines in my .bashrc that added the npm package directory to the path.
C.) Restart VS Code.
I doubt step A is necessary. But B and C certainly are.

What is the difference between yarn run and npm start?

Is yarn run intended to be the equivalent of npm start?
It seems yarn run start is the equivalent of npm start, which runs the script inside the start field of the script field in package.json
Few things to understand:
npm: run command is mandatory to execute user defined scripts.
yarn: run command is not mandatory to execute user defined scripts.
start command is not a user defined script name, so you may not need to specify run command to execute it.
So, all the below commands work similar!
npm start
npm run start
yarn start
yarn run start
If you have a user defined script named 'app':
npm app (Does not work!)
npm run app (Works!)
yarn app (Works!)
yarn run app (Works!)
Note: By default start runs node server.js in case not explicitly defined.
npm start is a shortcut for npm run start
Now in terms of running scripts from package.json, all these are equivalent:
npm run start
npm start
yarn run start
yarn start
npm run myscript
npm myscript this is an error
yarn run myscript
yarn myscript
This is because run is not mandatory command for yarn, but it is for npm.
Bonus
npr start - OK
npr myscript - OK
Put this file somewhere in PATH, eg. %localappdata%\Programs\Git\cmd
npr.cmd
npm run %*
yarn run is similar to npm run, they can be used to run the scripts in package.json.
For npm, you can leave out run when run the npm lifecycle scripts (test, start, restart, and stop), but these scripts maybe have extra effect. For example, npm start will run node server.js if the "scripts" object does not define a "start" property in package.json. see the doc npm run. You can't run script with other name leaving out run.
For yarn, you can leave out run for all the scripts in package.json, but if your script name is same as yarn built-in cli commands, the built-in cli commands will have preference over your scripts. doc yarn run.
So the best way to run scripts in package.json: never leave out run.