git bash does not recognize "environment path" when using package.json scripts - bash

I am trying to yarn start with git bash
"start": "node scripts/start.js",
It always works when using PowerShell or CMD.
But it does not work with git bash.
But when I tried to node scripts/start.js instead yarn start with bash it works!
I tested git bash
yarn -v, node -v, npm -v,
every command works well.
But not work with scripts...
This is the error message
'node' is not recognized as an internal or external command
And i tried to
"startStart": "yarn start",
And this time bash gives me this error message
'yarn ' is not recognized as an internal or external command
I checked my env PATH but all is fine.
--- ENV
VS_CODE
OS : window 10
node : 13.5
npm : 6.13.4
I installed git-bash with git
And all install config is default-standard
Add
I think Git-Bash can find the path when it is alone
I think we should focus on that it can't find path only when it try to trigger package.json scripts
About .profile I didn't know what it is and I never created it.
If it is not default - exist I don't have it.

without to relate to windows specific, npm executes scripts commands (specified in package.json) under the default shell, but it does not perform a login to the shell.
for instance, a bash login (bash --login) in order to use your custom system environment variable.
you can change this by using .npmrc file and set the script-shell. see this answer for the solution.
i hope this is what you suffer from :)

Related

VS Code how to specify npm run shell per project

I'm contributing to a project hosted on GitHub (https://github.com/esphome/dashboard) which is using bash scripts to develop and build.
I'm using Windows 10, so my default shell in VS Code was Command Prompt, I've changed it to Bash, by setting the default profile to Git Bash (and I verified that when I open a new terminal it is bash), but when I run an npm task I'm getting this error:
* Executing task: npm run develop
> esphome-dashboard#0.0.1 develop
> script/develop
'script' is not recognized as an internal or external command,
operable program or batch file.
* The terminal process "C:\WINDOWS\System32\cmd.exe /d /c npm run develop" terminated with exit code: 1.
* Terminal will be reused by tasks, press any key to close it.
I can globally set npm config according to this answer by calling:
npm config set script-shell "C:\\Program Files\\git\\bin\\bash.exe"
but this is a global change and I must remember to revert it by calling:
npm config delete script-shell
Is there a way to set this per project, so that when I open this project it will use bash and when I open others they will use the default (cmd)?
You can put following line :
script-shell=C:\Program Files\git\bin\bash.exe
in your projects-root/.npmrc, and remove it from ~/.npmrc

Git bash can not run any ng command (Angular CLI)

I'm working on git bash cmd. When I try run ng command e.g. ng serve or ng gc new-component I see this output:
C:\Users\user\AppData\Roaming\npm/node_modules/node/bin/node: line 1: This: command not found
In the middle of output I see problem with left and right slash. How can I fix it?
I suppose, it isn't problem with environment paths, because all ng commands are working in Windows cmd or PowerShell.
Please see comments from #steveswork at:
https://github.com/nodejs/node/issues/29287
You have to delete C:/Users/user/AppData/Roaming/npm and C:/Users/user/AppData/Roaming/npm-cache and
reinstall global npm modules.
(This typically happens after an upgrade or downgrade of node)
Good Luck.

HOME environment variable changes under npm on Windows

I’ve noticed a quirk when running npm scripts via Git Bash on Windows 10. Namely, the value of my HOME variable changes for commands called via npm, as opposed to direct from the shell:
$ env |grep ^HOME=
HOME=/h/
$ npm run env |grep ^HOME=
HOME=C:\Users\jake
This is creating some headaches, because I have scripts which depend on Git and ssh configuration, which are expected to live under $HOME. Does anyone know why this occurs, and/or how to change it?
For now, I’m working around it by maintaining the configs under both HOME paths, but feel like it would be cleaner to use the same path for either context.

On Windows, how can I stop npm from running processes in new windows?

When I run npm commands on my Windows 10 machine, new processes seem to spawn in new windows.
On my (Mac-using) colleagues machines, I see that these commands run in the same terminal window from which they were called. This is the behaviour I'd expect and is much more useful - it's really hard, for instance, to see my test results in the split second between them finishing and the terminal window being closed!
As an example, our packages.json has a script to run jest:
scripts": {
"test": "jest"
}
When I run npm run test, the following gets written to the current terminal:
$ npm run test
> #headuplabs/mobile#0.0.1 test C:\Users\TomWright\Documents\Repos\HeadUp.Mobile
> jest
But the actual execution of jest then gets started in a newly created window:
I'm using "git bash" as my default integrated terminal in VS Code, but I'm able to replicate this using cmd.exe and outside VS Code (i.e. by running git bash or cmd standalone).
I presume that my npm installation is improperly configured, but my Google search haven't yielded any answers.
Does anyone know what I'd need to do to make npm processes run in the same terminal window?
As mentioned in "how to set shell for npm run-scripts in windows", you might want configure the shell in npm itself:
npm config set script-shell "C:\\Program Files\\git\\bin\\bash.exe"
That would avoid the default setting, which can trigger a new shell window.

Running npm script on windows starting with a period

I have a package.json file with the following script defined:
"scripts": {
"test": "./node_modules/selenium-cucumber-js/index.js"
}
When I run npm test on linux or mac this script runs as expected. On Windows however I get an error:
/node_modules/selenium-cucumber-js/index.js
'.' is not recognized as an internal or external command,
operable program or batch file.
npm ERR! Test failed. See above for more details.
However if I run the command ./node_modules/selenium-cucumber-js/index.js directly from a cmd prompt it works correctly. The same issue also occurs if I try to run any other script through npm that starts with a ".". I haven't been able to find any other thread talking about this as an issue.
I am running npm versions 5.6.0 on Windows 10 Home.
Does anyone know how I can get this working?
Since npm 5.1
npm config set script-shell "C:\\Program Files (x86)\\git\\bin\\bash.exe"
or (64bit installation)
npm config set script-shell "C:\\Program Files\\git\\bin\\bash.exe"
Note that you need to have git for windows installed.
You can revert it by running:
npm config delete script-shell
Everything defined under scripts gets executed in the default system shell and on Windows ./node_modules/selenium-cucumber-js/index.js is definitely not a valid command (or rather a path). It just happens that the same file has a +x argument and a shebang pointing to a Node.js (or another JS interpreter) binary so that it gets executed on Linux without intervention.
I'm quite certain you'll get the same error if you were to execute the same command in the default Windows shell (cmd.exe) but you may get away with it in some ports/emulations of *nix shells (i.e. Cygwin, MSYS, bash.exe etc.) which may give you a false sense of everything working correctly outside of the npm chain.
If you want to make sure your script gets executed by Node.js while using relative paths and keeping cross-platform compatibility, call it explicitly as:
"scripts": {
"test": "node ./node_modules/selenium-cucumber-js/index.js"
}
This will also take care of things like not having a proper x flag or shebang in the script you're executing and since Node.js is perfectly comfortable with using *nix paths on Windows it won't complain either.
Whenever you write a shell script, you need to specify which shell it is written for. For inline package.json scripts, you should do this in an .npmrc alongside your package.json, like this:
script-shell = bash
If you don't specify a shell, npm is free to pick any old shell. On Windows, it picks cmd.exe, which is almost never what you want.
(Note: I don't recommend using npm config set for this. It sets local user configuration. The correct shell should be stored in version control alongside your scripts.)

Resources