VS Code how to specify npm run shell per project - bash

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

Related

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

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 :)

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.)

'tns' is not recognized as an internal or external command, operable program or batch file. NativeScript

I am new to NativeScript and am currently going through the installation steps described here (http://docs.nativescript.org/angular/tutorial/ng-chapter-1) and I just completed executing the PowerShell script
#powershell -NoProfile -ExecutionPolicy Bypass -Command "iex ((new-object net.webclient).DownloadString('https://raw.githubusercontent.com/NativeScript/nativescript-cli/production/setup/native-script.ps1'))"
and then I run
tns doctor
and I get
C:\>tns doctor 'tns' is not recognized as an internal or external command, operable program or batch file.
Use this code
npm install --global nativescript
I had the same isssue and installing nativescript solved it.
If you are on Windows you need to add the the npm global directory to your path.
Control Panels -> System
Advanced System Settings (Link on Left side)
Environmental Variables (Button)
Scroll down for Path, and then click Edit (Button)
Append to the end: ;%USERPROFILE%\AppData\Roaming\npm
Make sure you get the semicolon.
This should work on Windows 7. I can get you the steps for other versions of windows; but hopefully this gets you going.
Go to C:\Users\\username\AppData\Roaming\npm\node_modules (replace username appropriately) and check if Nativescript is installed or not. If not create directory and install nativescript and copy nativescript dir from node_modules and paste inside C:\Users\username\AppData\Roaming\npm\node_modules
create C:\Users\username\AppData\Roaming\npm\tns.cmd file and copy below content
#IF EXIST "%~dp0\node.exe" (
"%~dp0\node.exe" "%~dp0\node_modules\nativescript\bin\tns" %*
) ELSE (
#SETLOCAL
#SET PATHEXT=%PATHEXT:;.JS;=;%
node "%~dp0\node_modules\nativescript\bin\tns" %*
)
I was trying to create a mobile app with angular using nativescript and Install angular module npm install --global #nativescript/schematics
followed all instruction as mention in https://blog.angular.io/apps-that-work-natively-on-the-web-and-mobile-9b26852495e7
after everything, I faced the same error.
'tns' is not recognized as an internal or external command, operable program or batch file.
But it got resolved by installing nativescript module globally.
npm install --global nativescript
or
npm install -g nativescript
After that, I was able to use tns properly.
Hope this will help you to resolve your issue.

'npm' is not recognized as internal or external command, operable program or batch file

I am completely new to nodejs. I am trying to get nodejs to work on my Windows 2008 box in order to install Karma which I would use for TDDing my AngularJs code. I have done the following steps so far
Install using Chocolatey ==> npm is not recognised
Install using 64-bit nodejs installer from nodejs.org ==> npm is not recognised
At this stage, running where npm gives me c:\User\<Username>\AppData\Roaming\npm which has nothing in it
I figure out that nodejs is installed in C:\Program Files\nodejs. Opening a command prompt in this directory makes npm work fine.
So I added C:\Program Files\nodejs to PATH only to get the same error again that npm is not recognized
One of the github issues on nodejs repository says that I need to restart the machine and it would fix. But that has not helped so far
I do see a Node.js icon in my Start -> Programms mennu which takes me to nodejs console but not sure what to do with that.
Have I missed any important step in the process?
Edit
I figured out that if I open "Nodejs command prompt" from program files, then npm is recognized. How do I make it work on a normal command prompt?
Edit
After node I started facing a similar problem with another application. I posted this question on superuser and as rightly pointed out by the accepted answer, I had an additional quote in my PATH which was causing issues with all the paths added after the quote. I have a feeling that some Chocolatey install adds this troubling quote but I am just not sure which one.
Just add:
;C:\Program Files\nodejs\
To the end of your Path variable on the "User variable" section of the Environment Variables on the System Properties.
After that, reopen your command prompt and type
npm
This should work.
Don't forget to reboot your computer after installing node! That one got me.
If you are using VS Code, close VS code and open again.
I tried closing Terminal and then opening new Terminal but it didn't work.
Re-Starting VS Code works!
To elaborate on Breno's answer... For Windows 7 these steps worked for me:
Open the Control Panel (Click the Start button, then click Control Panel)
Click User Accounts
Click Change my environment variables
Select PATH and click the Edit... button
At the end of the Variable value, add ;C:\Program Files\nodejs
Click Ok on the "Edit User Variable" window, then click Ok on the "Environment Variables" window
Start a command prompt window (Start button, then type cmd into the search and hit enter)
At the prompt (C:\>) type npm and hit enter; you should now see some help text (Usage: npm <command> etc.) rather than "npm is not recognized..."
Now you can start using npm!
For Windows users: A picture tells a thousand words
Reference: npm not recognized as internal or external command (Note: this is my own blog)
Just Download and Install Node.js from here https://nodejs.org/en/
If you run the downloaded file and install it, they will automatically configure for your system
You don't need any other configurations anymore, now you can use the npm command anywhere
If the Nodejs is successfully installed and still displays the message like this:
'npm' is not recognized as an internal or external command, operable program or batch file.
Follow the steps below for Windows users:
Go to My Computer Properties
Click Advanced System Setting from the Left bar of a window.
Now you have a System Properties window. Click Advanced
Then, Click Environment Variable button
Now you have Environment variable window: From System Variable, Select Path
Click Edit
At the end of the Variable value, add ;C:\Program Files\nodejs\
Note: If you have installed nodejs on other drives then please act accordingly.
Click Ok all the open dialogue box
Very important Note: "Close your Command Prompt And Restart Again"
(It's very important because if you didn't restart your command prompt then changes will not be reflected.)
Now you can use the npm command anywhere
Don't forget to run cmd as admin.
I understand this question is really old and we have many answers, unfortunately, my scenario was different, thus a different solution.
If you started using nvm to manage the node versions after you install the node in your machine, most likely you will be facing the same issue.
I installed a version 10.15.3 and then I had to use another version as there is some tight dependency with one of my projects. Then I decided to install the nvm, while installing it asked, whether I need nvm to manage the version already installed on my machine which is 10.15.3. And yeah, I said Yes hoping that it will take care of that, unfortunately, it didn't.
I was always getting this error no matter I tried many other things mentioned in the other answers here, including setting the path. In the end, to fix that, these are the things I tried.
Uninstall the node version installed, for example, nvm uninstall 10.15.3
Make sure no other node versions are there, nvm list
Then, install the version needed, for example, nvm install 10.15.3. This should give you an output as preceding.
Downloading node.js version 10.15.3 (64-bit)...
Complete
Creating C:\Users\SibeeshVenu\AppData\Roaming\nvm\temp
Downloading npm version 6.4.1... Complete
Installing npm v6.4.1...
Installation complete. If you want to use this version, type
nvm use 10.15.3
PS C:\Users\SibeeshVenu> nvm use 10.15.3
Now using node v10.15.3 (64-bit)
Please note that from the above command, the npm is also installed.
Now use the version you need, nvm use 10.15.3 and type npm
I had the same problem described by Ashu, but in addition to that, the PATH entry for nodejs was terminated by a backslash:
C:\Program Files\nodejs\
I also had to remove that final backslash in order to have it work.
I faced the exact same issue and notice that after installing node.js there was a new path entry in the user variable section for PATH with value --> c:\User\\AppData\Roaming\npm. Also the Path entry in the system variable is appended with --> C:\Program Files\nodejs. Now since user variable has preference over system you have two options to fix this. Either delete the path from user variable or correct the right path (C:\Program Files\nodejs). Restart CMD and it should work.
Set aside all the tips, just run the following line in cmd
> SET PATH=C:\Program Files\Nodejs;%PATH%
If you're getting this error through a service account like Visual Studio TFS Build controller service or any other background service, make sure you restart the service after installing npm as the new PATH environment settings will not be picked up by those already running processes. I was getting same error through my build service but I had npm installed and running in the console.
You might have already received a response but this might help others since I experienced the same issue recently and this is what I did:
Added a path for Powershell. For me, the path was C:\Windows\System32\WindowsPowerShell\v1.0
Then, I opened the command prompt with administrative privileges and ran
#powershell -NoProfile -ExecutionPolicy unrestricted -Command "iex ((new-object net.webclient).DownloadString('https://chocolatey.org/install.ps1'))" && SET PATH=%PATH%;%ALLUSERSPROFILE%\chocolatey\bin
Next, choco install nodejs
Restart and everything worked fine. Try opening the command prompt without admin privileges and run npm -v
Cheers.
Had the same problem on Windows 8.1 64 bit.
Turns out i get that problem if I start cmd by typing it in the path bar at the top of a folder window or when i shift right click in a folder window and then open command prompt from the list.
When I run cmd using Run or Just from the cmd.exe executable it works.
I installed nodejs following this AngularJS tutorial. the npm command did work when I open a new cmd window but not in the current one.
So the fix was to close and open a new cmd window.
If the package is successfully installed and still shows the message "'npm' is not recognized as an internal or external command, operable program or batch file."
Click windows start button.
Look for "ALL APPS", you will see Node.js and Node.js Command prompt there.
You can run the Node.js Command prompt as administrator and soon as its run it will show the message "Your environment has been set up for using Node.js 6.3.0 (x64) and npm."
and then it works from there...
I ran into this issue as well. It turns out Windows doesn't enjoy single quotes on the command line. The culprit was one of my npm scripts. I changed the single quotes to escaped double quotes:
'npm -s run sass-build'
to
\"npm -s run sass-build\"
I'm updating this thread with a new answer because I've found the solution to my miserable situation after not less than a week ...
For those still experiencing the error even though they have their path value set properly, check your pathext variable to have the value (default value in windows 7 +) : .COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC
Mine was to set only to : .BAT and changing it solved the problem. I wonder why nobody brought this up ...
Hope this helps!
If everything looks fine. I would advice to check this for PATHEXT .CMD must be added.
I don't know why most of user suggesting ; / in the command.
I solved this by removing ; and /
Before
;C:\Program Files\nodejs\
After Solution
C:\Program Files\nodejs
You need to save them into system PATH variables
I ran into this problem the other day on my Windows 7 machine. Problem wasn't my path, but I had to use escaped forward slashes instead of backslashes like this:
"scripts": {
"script": ".\\bin\\script.sh"
}
Step 1: install NodeJs
step 2: Set environment path
Step 3: Restart PC once
I installed Node.js and while trying to install Ionic and cordova using this piece of code:
npm install -g cordova ionic
I faced the above error. I added 'C:\Program Files\nodejs' to my Environment Variable 'PATH'. But still was unable to get over this issue. Turned out that my PATH variable was longer than 2048 characters and so I was unable to add the Nodejs path to it. I had to remove the path of another program and add the Nodejs path.
Close and reopen the cmd prompt and try to install Ionic again.
This worked for me.
for me adding path to PATH didn't do the trick.
Run c:\Program Files\nodejs\nodevars.bat instead, it will do the job for you
Well in my case doing testing via Mocha i tried everything just to realize i have to remove single quotes around my test case script tag inside package.json.
I am running mocha test case on all *.test.js files as can see below:
package.json
Before:
"scripts": {
"test": "mocha server/**/*.test.js",
"test-watch": "nodemon --exec 'npm run test'"
}
After(removing single quotes - npm run test):
"scripts": {
"test": "mocha server/**/*.test.js",
"test-watch": "nodemon --exec npm run test"
}
Worked for me, just in case someone else also gets stuck on this.
Check npm config by command:
npm config list
It needs properties: "prefix", global "prefix" and "node bin location".
; userconfig C:\Users\username\.npmrc
cache = "C:\\ProgramData\\npm-cache"
msvs_version = "2015"
prefix = "C:\\ProgramData\\npm"
python = "C:\\Python27\\"
registry = "http://registry.com/api/npm/npm-packages/"
; globalconfig C:\ProgramData\npm\etc\npmrc
cache = "C:\\ProgramData\\npm-cache"
prefix = "C:\\ProgramData\\npm"
; node bin location = C:\Program Files\nodejs\node.exe
; cwd = C:\WINDOWS\system32
In this case it needs to add these paths to the end of environment variable PATH:
;C:\Program Files\nodejs;C:\ProgramData\npm;
If you used ms build tools to install node the path is here:
C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\MSBuild\Microsoft\VisualStudio\NodeJs
You don't need to "install" Node.js. Just download the package from https://nodejs.org/en/download/releases/ and extract the contents somewhere. Then, in a command prompt, you can navigate there using cd. Then you can run npm from that location. While you will have to do the cd C:\Users\YourAcct\Documents\node-v16.13.0-win-x64 or similar command before you can run it, it's a way to get it onto your computer and run npm without having to be an admin or editing environmental variables.
The issue is with system policy. I have tried the following comments in powers heel then it is started working
$> Set-ExecutionPolicy Unrestricted -Scope CurrentUser -Force
$> npm install -g npm-windows-upgrade
$> npm-windows-upgrade
I got this idea from the following link
After adding environment varible path restart your machine maybe this will work. it work for me. i was using vm.

Resources