Using npm in Visual Studio 2015

3.5k Views Asked by At

When using npm in Visual Studio with NTVS, what are the options that are in effect? How do I apply --save or -g?

Will it corrupt the Visual Studio project in anyway if I were to run npm outside of Visual Studio on the command line directly?

1

There are 1 best solutions below

0
On

I can't say anything about ntvs, because I've never used it. But when we are talking about standard Package Manager Console Window:

  1. npm install [packageName] - installs packages locally (analogue to --save option);
  2. npm uses package.json file of the selected project;
  3. to understand weather using npm outside of Visual Studio will corrupt your project or not, you should know this:

there may be several installations of npm (and node.js) on your machine. Node.js installer stores its packages in "*C:\Program Files\nodejs*" path by default and sets up the system PATH variable with this value. When you install packages via -g option, they are stored in "%APPDATA%/npm" path.

So, when you use npm outside of Visual Studio, system console will apply to the version of npm/nodejs which is stored in the PATH variable.

To make sure that you use the same version of npm in Visual Studio, open Tools/Options => Projects and Solutions/External Web Tools option. Visual Studio will use locations of external tools by the order, as they are located in the list.

You can add PATH variable and put it in the first place to use the same version as you system.console does:

enter image description here at this picture VS will use PATH variable, while "$(VSINSTALLDIR)\Web\External" and "c:\Program files\nodejs" will be ignored (although, I have redundancy here, because, my system PATH variable contains "c:\Program files\nodejs" as well).

You can also check version of each npm installed on your machine. Just run "npm -v" in VS package manager console, and run it in your system console.

I have the same version for them because of my preferences:

node -v
v7.8.0

But when I run this command opening cmd at $(VSINSTALLDIR)\Web\External path, I get an older version: enter image description here

hope this helps =)