Running npm command within Visual Studio Code - npm

Is there anyway of executing the npm command within Visual Studio Code (using f1 into >) on Windows(10) to install packages to my folder I'm working in?
I have npm installed and I keep having to install npm packages from cmd

Install
Ctrl+P, write ext install npm script runner
Restart VS Code
Use (two ways)
Ctrl+R Shift+R
Ctrl+P, write >npm, select run script, select the desired task
Update: Since version 1.3 Visual Studio Code has integrated terminal. To open it, use any of these methods:
Use the Ctrl+` keyboard shortcut.
Use the View | Toggle Integrated Terminal menu command.
From the Command Palette (Ctrl+Shift+P), use the View:Toggle Integrated Terminal command.

You can run npm commands directly in terminal (ctrl + `). Make sure that terminal has cmd.exe as the shell selected.
You can default cmd.exe as your shell by following these steps.
ctrl+Shift+p
Type > Select Default Shell + Enter
Select > Command Prompt ...cmd.exe
Restart VS Code.

There might be a chance that you have install node.js while your visual studio code was open.
Once node.js is install successfully, Simply close the VS Code and Start it again. It will work.
Thank you

On Win10 I had to run VSCode as administrator to npm commands work.

All you need to do is to add args to the integrated terminal within 'User Settings' window.
Follow this steps below to do that:
List item
Go to 'User Settings'
Search for setting named - "terminal.integrated.shellArgs.windows"
Edit this setting by copying it to the right side.
Add the following arg value = "/k nodevars.bat", e.g. "terminal.integrated.shellArgs.windows": ["/k nodevars.bat"],
Make sure that the setting named "terminal.integrated.shell.windows" is set to the value - "C:\Windows\system32\cmd.exe". If not then do that.
Open new Terminal.
Good luck

I installed npm after Visual studio code, closed all visual studio instances and opened again and it started working.

One reason might be if you install the node after starting the vs code,as vs code terminal integrated or external takes the path value which was at the time of starting the vs code and gives you error:
'node' is not recognized as an internal or external command,operable
program or batch file.
A simple restart of vs code will solve the issue.

Edit user setting file settings.json.
Settings > Search for settings.json > Edit in settings.json
or
Run > type %APPDATA%\Code\User\settings.json
Copy this code
{
"terminal.integrated.shell.windows": "C:\\Windows\\System32\\cmd.exe",
"terminal.integrated.shellArgs.windows": ["/k nodevars.bat"]
}
Restart VS Code

To install npm on VS Code:
Click Ctrl+P
Write ext install npm script runner
On the results list look for npm 'npm commands for VS Code'. This npm manages commands. Click Install, then Reload VS Code to save changes
Restart VS Code
On the Integrated Terminal, Run 'npm install'

Same thing was happening to me after I installed Node.js. Node and npm was recognized in PowerShell and Command Prompt but not in VS Code. I fixed it by adding the Node.js install path to the system's environment PATH variable. The node.js install path on my system was:
C:\Program Files\nodejs
Where I find the node.exe that is needed. The user's PATH variable already had the Node.js install path but for some reason VS Code needs the Node.js install path in the system's PATH variables.
Windows 10 instructions:
Windows key and type "environment"
Select "Edit the system environment variables"
Click button labelled "Environment Variables..."
In "System variables" section edit the "Path" variable
Add Node.js install path to the list (C:\Program Files\nodejs)
The other answers were great but this is another way to fix it that worked for me without needing to install stuff, run as admin, or change the default settings.

VSCode User Setup is a new installer which installs VSCode and its dependencies in directories which don't require system-level / administrator permissions to modify. This is because New VSCode runs with user privileges. Your Nodejs installation added npm's path as System variable which VSCode cannot read.
Adding NPM path to Path variable in the User variable, you will be able to run NPM from the integrated command line.

Make sure you exit out of the existing terminal window. It will try to recover it and even though you may have closed out of VS Code you want to close the terminal window as well

Try to install PowerShell extension provided by VS code.
After install click on PowerShell and It will start new PowerShell Console where you can run all script

One from this two can help:
A) After you installed NodeJS, and restarted VScode, but still not getting npm to work, then idelete the opened terminal in VSCode with 'recycle' icon and try to create a new instance of terminal. From that moment and onwards, NPM should be working
B) If that doesn't help, then open up the prompt (Ctrl+P) and type >Terminal>Create terminal (with profile) and create 'cmd/powershell' based terminal. Afterwards, npm should be working.

Well, anyone still ends up here, and couldn't resolve the problem, Here is how to fix it.
Check the default terminal in VS Code (ctrl+ ~). It should be cmd and not Powershell.
If it is Powershell, go to settings > features > Terminal › Integrated ›
Default Profile: Windows. And select Command Prompt.
Or in search settings type 'default profile', and select Command Prompt.
Install VS Code extension - npm script runner (npm support for VS Code by Microsoft).
Click on extensions marketplace (ctrl + shift + x).
Type> npm script runner > install
After these steps, npm should be working from VS Code terminal.

There is an extension available, npm Script runner. I have not tried it myself, though.

As an alternative to some of the answers suggested above, if you have powershell installed, you can invoke that directly as your terminal. That is edit the corresponding setting.json value as follows:
"terminal.integrated.shell.windows": "C:\\WINDOWS\\System32\\WindowsPowerShell\\v1.0\\powershell.exe"
I find this works well as the environment is correctly configured.

Open standard terminal ctrl+p and paste this command
npm i script-runner
Need to see this logs npm should be run outside of the node repl,
in your normal shell. (Press Control-D to exit.)
(To exit, press ^C again or type .exit)
C:\DW\Examples\Ang.Crud>npm i script-runner npm WARN saveError ENOENT:
no such file or directory, open 'C:\DW\Examples\Ang.Crud\package.json'
npm notice created a lockfile as package-lock.json. You should commit
this file. npm WARN enoent ENOENT: no such file or directory, open
'C:\DW\Examples\Ang.Crud\package.json' npm WARN Ang.Crud No
description npm WARN Ang.Crud No repository field. npm WARN Ang.Crud
No README data npm WARN Ang.Crud No license field.
script-runner#0.1.8 added 7 packages from 5 contributors and audited 7 packages in 2.955s found 0 vulnerabilities
Usage: npm
where is one of:
access, adduser, audit, bin, bugs, c, cache, ci, cit,
completion, config, create, ddp, dedupe, deprecate,
dist-tag, docs, doctor, edit, explore, get, help,
help-search, hook, i, init, install, install-test, it, link,
list, ln, login, logout, ls, outdated, owner, pack, ping,
prefix, profile, prune, publish, rb, rebuild, repo, restart,
root, run, run-script, s, se, search, set, shrinkwrap, star,
stars, start, stop, t, team, test, token, tst, un,
uninstall, unpublish, unstar, up, update, v, version, view,
whoami
npm -h quick help on npm -l display
full usage info npm help search for help on npm help
npm involved overview
Specify configs in the ini-formatted file:
C:\Users\fdc.npmrc or on the command line via: npm --key value Config info can be viewed via: npm help config
npm#6.4.1 C:\Program Files\nodejs\node_modules\npm

For me, this problem is fixed after installing the extension ES7 React/Redux/GraphQL/React-Native snippets. I am using windows 10 and the latest version of VS Code, and a little interpreter icon occurred on the lower right of the status bar.

Try this.
npm not works in Visual studio code
open vs code then Ctrl+P -> type - ext install npm script runner
you have to choose one and install it. once installed please close and open Visual studio code
and go to vs code terminal and type npm start and browser will start http://localhost:3000
Its working good. Thanks

I restarted my machine, after checking the path in environment variable. it worked for me.

In terminal run ->
npm install script-runner

You probably dont have your path variable set for npm on your machine. And typescript has nothing to do with this issue. VS Code is built on TypeScript for type checking when you're using JavaScript. But i'd suggest you to uninstall Node from your machine and re-install Node from here. Make sure you install the latest version of Node. Node comes with npm and it also sets the PATH_VARIABLE for terminal. In order to check if the path variable set or not , you can try this command node --version or npm --version.

Download Node.js from the link here
Install and then Then restart

You have to do the following 3 steps to fix your issues:
1.Download Node.js from here.
Install it and then add the path C:\Program Files\nodejs to your System variables.
Then restart your visual studio code editor.
Happy code

Related

Environment variables (zsh terminal) installed through npm on MacOS 12.4 Monterey - SOLVED

Background:
Dear Stackoverflow community. I recently switched from Windows to MacOS and am at a loss how to configure environment variables. Many of the previous questions asked here are about bash instead of zsh.
Goal:
I am trying to install an npm package globally. For instance:
npm install -g vercel
or any other package, and use it in my Visual Studio Code terminal (also zsh). If I want to deploy code I have to use 'npx vercel deploy' every time. I want to be able to use "vercel deploy" but instead I get:
zsh: command not found: vercel
What I tried so far:
Installing the package in zsh and visual studio code terminals (didn't work)
Setting the path equal on both VS and terminal: export PATH=/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
I learned that you need to add environment variabels to a .zshrc file. I don't seem to have a zshrc file. When I do:
sudo ~/.zshrc
Password: XXXXXX
sudo: /Users/vincent/.zshrc: command not found
I am getting another error.
I read in the zsh man file that you should use $HOME/.zshrc instead. Again I am getting the same errors:
vincent#Vincents-MacBook-Air-2 ~ % $HOME/.zshrc
zsh: permission denied: /Users/vincent/.zshrc
vincent#Vincents-MacBook-Air-2 ~ % sudo $HOME/.zshrc
sudo: /Users/vincent/.zshrc: command not found
Update 1:
#slebetman Thank you for your explanation. When I open the terminal I go to home via "cd $HOME" which puts me in in the home directory. However there is no way for me to create a .zshrc file in that directory. Neither via touch or vs code. I am getting the following error: "Unable to write file '/home/.zshrc' (Unknown (FileSystemError): Error: ENOTSUP: operation not supported on socket, open '/home/.zshrc')"
Update 2:
I did manage to find the .zshrc file in visual studio code under /etc. I hope this will work. I was able to overwrite the file with Sudo and add environment variables to it.
Add this to the file .zshrc file under /etc and force overwrite it:
export vercel=/Users/vincent/.npm-global/bin/vercel
Screenshots below for those who will try in the future:
Image with .zshrc file layout
Final remarks:
I don't understand how it is so difficult to add environment variables on Mac while everything else is so easy.
I have read many different questions on stackoverflow, and I can't seem to solve it. Also since I am a newbie I am not allowed to comment on there so I post here in the hope that anyone can help me :)
Best,
Vincent
Note that when you do:
npx vercel deploy
Npm will execute vercel for you without installing it. It does that by temporarily downloading vercel. If you want to run vercel directly without using npx then simply don't use npx. Install it instead:
npm install -g vercel
The -g flag installs the module globally and if the module has a CLI it will be available globally as well (note that depending on your setup you may need admin/sudo privileges to use the -g flag). Now you can run vercel by simply typing:
vercel deploy
This works in all operating systems supported by node.js and npm. That means you can even do this in Windows. In fact I use npm to distribute my tools in a simple cross-platform way so I don't have to support multiple package managers like chocolatey (Windows), homebrew (Mac OS), deb (Debian based distros), rpm (Redhat based distros) etc. (npm does not require your software to be written in node.js - I have published packages on npm written in tcl and bash).

Npm won't install quick.db, returns these errors

I am trying to install quick.db for my discord.js bot, and when I attempted installing by running these two commands:
npm -g --add-python-to-path install windows-build-tools node-gyp
and
npm i quick.db
It returns me this error
Here is the log
Does anyone know how to fix this?
I found the solution.
It is easy. Follow these steps:
1- Click on the windows-key of your keyboard to open the windows menu
2- Type cmd/PowerShell (It's totally your choice) and right-click on the first result
3- Click Run as Administrator
4- When the terminal is opened type npm install --global windows-build-tools --vs2015 in it and the installation will start. (Sometimes this can take longer)
5- When the installation is finished you can close the terminal.
6- Now you must go to the directory where you want to install Quick.db.
7- Hold Left-Shift and right-click on a blank space of the directory.
8- Click on "Open PowerShell window here".
9- If you see a file named "package.json" in your folder skip to 11
10- Type npm init -y in the terminal
11- Type npm i quick.db
I finally managed to fix it! All you have to do is click modify under Visual Studio Community in the installer, go to the dropdown list in "Desktop Development with C++" and check the box marked "VC++ 2015.3 v14.00 (v140) toolset for desktop" and install it!
I found this solution here!

Building Autodesk Forge RCDB on Windows 10 fails with node-gyp errors

While attempting to get the Forge Responsive Connected Database to build. I am unable to build this application on my Windows 10 machine. I am unable to succesffuly run “npm install”. I get the following errors as listed below. I ended up trying to use a pre-configured “canvas” and it still didn’t let the node app run.
https://github.com/Autodesk-Forge/forge-rcdb.nodejs
I have tried these solutions to no success:
https://github.com/npm/npm/issues/14543
https://github.com/nodejs/node-gyp#on-windows
https://github.com/Automattic/node-canvas/wiki/Installation---Windows
https://github.com/substack/dnode/issues/181
https://github.com/nodejs/node-gyp/issues/629#issuecomment-153196245
https://www.npmjs.com/package/canvas-prebuilt
> canvas#1.6.10 install D:\DEVELOPMENT\Autodesk\forge-rcdb.nodejs3\node_modules\canvas
> node-gyp rebuild
D:\DEVELOPMENT\Autodesk\forge-rcdb.nodejs3\node_modules\canvas>if not defined npm_config_node_gyp (node "C:\Users\steck\AppData\Roaming\nvm\v7.7.1\node_modules\npm\bin\node-gyp-bin\\..\..\node_modules\node-gyp\bin\node-gyp.js" rebuild ) else (node "node C:\Users\me\AppData\Roaming\npm\node_modules\node-gyp\bin\node-gyp.js" rebuild )
module.js:472
throw err;
^
Error: Cannot find module 'D:\DEVELOPMENT\Autodesk\forge-rcdb.nodejs3\node_modules\canvas\node C:\Users\me\AppData\Roaming\npm\node_modules\node-gyp\bin\node-gyp.js'
at Function.Module._resolveFilename (module.js:470:15)
at Function.Module._load (module.js:418:25)
at Module.runMain (module.js:605:10)
at run (bootstrap_node.js:425:7)
at startup (bootstrap_node.js:146:9)
at bootstrap_node.js:540:3
Here are the full installation and setup steps I tested on two different 2 different computers with Windows10 installed.
Pre-Installations:
Node.js - used v.6.x and v8.x
Node.js global packages:
npm-windows-upgrade(link):
This tool will move and install npm into Windows' spectial folder %appdata%.
Installation steps:
Open Powershell as Administrator and execute Set-ExecutionPolicy Unrestricted -Scope CurrentUser -Force.
Install tool by npm install --global --production npm-windows-upgrade
npm-windows-upgrade
Execute cmd npm-windows-upgrade --npm-version 5.6.0 in the Powershell (note. you can choose any version what you want, e.g. 6.0.1)
windows-build-tools(link):
This tool will install build tools required by the package node-gyp.
The installation will take some time to wait for the build tools to be downloaded and installed. If you might see a message Successfully installed Python 2.7 firstly, but you have to wait for the message Successfully installed Visual Studio Build Tools. shows up.
Open Powershell as Administrator and install tool by npm install --global --production windows-build-tools
node-gyp(link):
This tool is required by the package node-canvas while running npm install.
Open Powershell normally(say current user) and install tool by npm install -g node-gyp
Visual Studio 2017 (link):
This might be optional, I just installed it with VS C++ features in my cases.
Chocolatey (link):
This tool will make your life easier to install dependencies for package node-canvas on Windows.
Open Powershell as Administrator and execute Set-ExecutionPolicy Bypass -Scope Process -Force; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1')).
After installing Chocolatey, install node-canvas dependencies by choco install -y python2 gtk-runtime microsoft-build-tools libjpeg-turbo (You might see a warning message visuallstudiotool-vs2017 installation failed and you can ignore it. In my cases, it didn't affect the result)
GTK with cairo library (link):
This tool the required dependency for package node-canvas.
Installation steps:
Download precompiled X64 GTK package here.
Unzip all contents into C:\GTK.
Mongochef (link):
This tool will make your life easier to restore dummy database of the forge-rcdb.
MongoDB:
You can follow the instructions here to install MongoDB on your compture , or use MongoDB PaaS mlab instead.
Main Installations:
Download source codes of the forge-rcdb and unzip into somewhere you like. In my cases, I put it into my desktop.
Open Powershell normally(say current user) and change directory into the unzipped folder of the source code. For instance, execute cd Desktop\forge-rcdb-nodejs-master in the Powershell.
Run cmd npm install in the root of the source code folder. (You may see some error message while compiling the package node-canvas, but can see 'copied xxx.c', 'copied xxx.cpp', and so on in the same time, then you can ignore the errors. In my cases, it didn't be a problem to run forge-rcdb.)
Configurations:
Open config\development.config.js with your editor and change values of viewer section into:
viewer: {
viewer3D: 'https://developer.api.autodesk.com/derivativeservice/v2/viewers/viewer3D.js?v=4.0.1',
threeJS: 'https://developer.api.autodesk.com/derivativeservice/v2/viewers/three.js?v=4.0.1',
style: 'https://developer.api.autodesk.com/derivativeservice/v2/viewers/style.css?v=4.0.1'
}
Change the database section in the config\development.config.js with you MongoDB connection settings.
Restore dummy database data in the resources/db/dev or resources/db/prod via Mongochef's Import function.
Open Powershell normally(say current user) and set following environment variables:
$env:NODE_ENV="development"
$env:HOT_RELOADING="true"
$env:FORGE_DEV_CLIENT_ID="YOUR_CLINET_ID"
$env:FORGE_DEV_CLIENT_SECRET="YOUR_CLINET_SECRET"
Run the project
Change current directory of the Powershell into the source code folder of the forge-rcdb and type npm start
open http://localhost:3000 in your favorite browser.
Trouble Shooting
You might be not able to load any models in the browser. If you to test the functionality, please copy resources\models\prod\robot into resources\models\dev\robot. Afterward, navigate to http://localhost:3000/configurator in the web browser and mouse-click on Kinematics project, then you will see the model loaded on your screen.
If you see the response of http://localhost:3000/api/forge/user showing HTTP EORROR CODE 404 from the browser dev console, then you must have to check whether the CallBack URL of your Forge App used to host forge-rcdb in Forge Dev Protal is http://localhost:3000/api/forge/callback/oauth. (note. This config can be changed in config\development.config.js)
Afterward, you or your users must have to log into the website with Autodesk account to obtain 3 legged token from the Forge OAuth service to upload models in the gallery page.
If you get an empty array from the response of http://localhost:3000/api/models/configurator?limit=100&offset=0&search=, it means there is nothing in your MongoDB collection configurator.models. To fix this issue, you can import db content resources\db\dev\configurator.models.json into your MongoDB via Mongochef. (note. Please refer the step 3 of the configuration section)
If you cannot load any listed models from the web pages, it means there is not downloaded viewable bubbles in your resources\models\prod or resources\models\dev as well as your Forge OSS bucket. So, please check the urn value in your database whether you have those models in these positions previously mentioned.
If you found the main reason for item 3 of Trouble Shooting is that you didn't own models listed in the forge-rcdb, that's because these models are Autodesk's private models or uploaded by other users themselves before, that might not be shared to the public, unfortunately. To fix this issue, you have to change the example data of MongoDB Collections mentioned in step 3 of the configuration section to your own models.
Hope it helps.

Visual Studio Code (Windows) Not Detecting Global NPM Modules

I'm experiencing an issue where Visual Studio Code in Windows 10 is not able to detect globally installed NPM packages within the Integrated Terminal.
For example, if the windows command prompt I install #angular/cli via command npm install -g #angular/cli, within that command prompt I can execute #angular/cli commands such as ng --version without error. After that global npm module install, within Visual Studio Code, performing the same ng --version gives me the error:
The term 'ng' is not recognized as the name of a cmdlet, function,
script file, or operable program. Check the spelling of the name, or
if a path was included, verify that the path is correct and try again.
I can however within the integrated terminal successfully perform commands such as npm -v and node -v.
I have the following items in my system environment variables
C:\Program Files\nodejs\
%AppData%\npm
Are there Visual Studio Code user or system environment variables I'm missing to allow detection of global npm modules?
Thank you for any help you can provide.
Visual Studio Code must be "Run as Administrator" for working Terminal Commands.
I was able to resolve the issue and use global npm commands in Visual Studio Code by modifying my system environment path variables to position %AppData%\npm before %ProgramFiles%\nodejs\ per npm troubleshooting tips
no need to do anything else just follow below steps -
right click on my computer (this pc).
go to properties.
click on advanced system setting.
click on environmental variable.
select path variable from system variable.
click on edit button.
add %AppData%\npm and %ProgramFiles%\nodejs\ at top of the list.
8 click on ok. ok. ok
restart your system.
after restart open visual studio code as run as administrative mode.
Enjoy...............!!!!!
Run Visual studio code as an administrator. Then type ng command. It should work
Reboot your computer , this worked for me
If you have entered the environment variable and it is still not recognized and if you don't want to reboot your computer because you have so many things running then just reboot your Visual Studio Or Visual Studio Code and then Open Terminal (ctrl + `) and run
npm install
Now it will work fine.
You can resolve the issue by opening the VS Code Terminal and type
npm install -g #angular/cli
This should resolve the issue.
Run powershell or cmd prompt in admin mode.
Check if npm and node commands work (if not, then check environment variables as others have mentioned).
Update node using npm [https://stackoverflow.com/questions/18412129/how-can-i-update-
npm-on-windows]
Set-ExecutionPolicy Unrestricted -Scope CurrentUser -Force
npm install -g npm-windows-upgrade
npm-windows-upgrade
Run npm install -g #angular/cli
Run ng --version in Visual Studio Code to see if it's now working.
If you do not have access to Environmental variable (office machines).
You can try to run command like this:
npm run <your angular command>
it works as well, just need to add npm run before ng command
Example -> npm run ng g c shop/cart
I found that adding %AppData%\npm to your Path variable made it work for PowerShell and the Command Prompt, it still did not work for the integrated PowerShell in VS Code for me.
When I changed it from %AppData%\npm to the actual path C:\Users\<USER>\AppData\Roaming\npm where <USER> is your user directory, and reopened VS Code it started working.
Additionaly to all the good advice here, make sure that you have node.js installed on your machine.
To me the solution was adding the extensions for npm:

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

I'm getting:
'npm' is not recognized as an internal or external command, operable program or batch file.
when I issue:
npm install -g bower polyserve
Even though I have downloaded node.
I think you have not defined the path of nodejs in Environment Variables on the System Properties.
Add ;C:\Program Files\nodejs\ (path of your nodejs installed directory)
To the end of your Path variable on the "User variable" section of the Environment Variables on the System Properties.
Then reopen cmd prompt and type npm again.It should work now.Hope it helps.
You can do one more thing if still it does not work.Reboot your machine after installing node.
Set path for node.js in your environment variables.
To do this:
right click on MyComputer->properties->advance system setting-> Environment variables-> select & edit "Path" variable in System variables.
Add C:\Program Files\nodejs\; in path
Restart your command prompt
Type node --version in a new console window, the version number should appear as a response.
I would recommend using https://github.com/coreybutler/nvm-windows instead of directly installing a version of node, as it is common to switch between node versions for different projects. Following are the instructions to set it up.
Open PowerShell with 'Run as Administrator' option (Admin role is required for creating symlink)
Check if nvm is installed:
nvm version
If not, follow instructions on https://github.com/coreybutler/nvm-windows#node-version-manager-nvm-for-windows to install it.
If nvm was installed, check if nodejs is in system path:
$Env:Path
If nodejs (likely 'C:\Program Files\nodejs') is not in the path and 'nodejs' symlink file exists in the directory 'C:\Program Files', then add it to the path as described by Vikas Sharma.
If 'C:\Program Files\nodejs' is in the path and symlink file doesn't exists in the directory, list installed node versions using:
nvm ls
Then create the symlink using (needs Admin role):
nvm use <node version>
Check if npm is working now:
npm --version
I had the same issue. Environment variables were all in place (as suggested by the user above). I was using node 8.9.4. Changing node version to 8.16.0 worked for me.
If node is there but npm only not found (which is my case), then
if you have the downloaded node.js in your (windows) pc run the repair option it will do the rest.
Close the cmd window and reopen and test it by running the command
npm -v
Now it will show you the npm version.