npm
Looking to avoid not-invented-here syndrome? npm allows you to install and manage packages for use in your Node.js apps (among other things). We'll assume you're familiar with basic npm functionality, otherwise check outnpmjs.org to learn more.
npm in the command prompt
The easiest way to open a command prompt in the correct location is to right-click yourNode.js project in Solution Explorer, then click Open Command Prompt Here... on the context menu. You'll be able to run npm commands in your project directory assuming npm is in your path.
.npm in the Node.js Interactive Window
New in NTVS 1.0 Beta 2
If you're addicted to the npm command line interface, you'll feel right at home with the.npm
command in the Node.js Interactive Window
Tools→
Node.js Tools→
Node.js Interactive Window. It does exactly what you would expect - run npm commands from the Interactive Window. For instance...
> .npm install azure -g
By default, npm will execute in your project's home directory. If you have multiple projects in your solution (or want to execute in a different path,) you'll need to specify the name (or path) of the project in brackets:
> .npm [MyProjectNameOrPath] install azure@4.2.3 -g
Known Issue: the .npm
command is currently unavailable for folders that do not have a package.json file. To initiate commands like npm init (which creates a package.json file,) simply right click your project's Solution Explorer node,
and open up the command prompt.
Browsing/Installing new npm packages in the GUI
Refreshed in NTVS 1.0 Beta 2
If you don't know the exact command, want to browse through available packages in the npm registry, or simply prefer agooey graphical user interface, right click on the Solution Explorer npm node, and select
Install New npm Packages...
The first time you open the dialog, it'll start downloading the entire npm catalog. It's a 43MB+ download, so if you're impatient, you can close the dialog and wait for the download to complete (current status prints to Output Window).
Once the catalog finishes loading, search for a package, specify options, and install. For instance, if you'd like to install azure, approximately version 0.8, and list it in your devDependencies in package.json:
- Dependency type: Development
- Add to package.json: Yes (if you uncheck this, it will be installed as a standard dependency instead because there is no concept of devDependencies without package.json. Similarly, this flag will be ignored if you select the
global
dependency type) - Other npm arguments:
@~0.8
The corresponding npm command is:
npm install azure@~0.8 --save-dev
If the status indicator in the lower left hand corner indicates an error and/or if you wish to view more details about the install, you can click on the link to open the npm install Output Window.
Note that npm will continue running in the background when you close the dialog (npm output will be printed to the Output Window.)
Managing Installed Packages in Solution Explorer
Edits to package.json and package install status are reflected in the npm Solution Explorer node.
For reference, the corresponding package.json specifies its dependencies as follows:
"dependencies": {
"express": "3.4.4",
"jade": "*"
},
"devDependencies": {
"mocha": "^1.21.0"
},
"optionalDependencies": {
"chess": "^0.2.1"
}
Package status:
Installed and listed in package.json
Extraneous (installed but not in package.json)
Missing (not installed, but listed as a dependency in package.json)
Dependency type: standard packages appear directly under the npm node, whereas your other packages (global
,devDependencies
, optionalDependencies
) appear under their corresponding subnodes. bundledDependencies are labeled "[bundled]"
Expand package nodes to browse their dependencies.
Of course, that's not all you can do... Right click on a package node or the npm node to take action:
- Install missing package(s) if they're listed in package.json
- Update package(s) to the latest version
Uninstall a package uninstalls package and removes from package.json (only available on package nodes)
Note that these options are unavailable for sub-modules.
npm log in Output Window
If you've already installed some packages you'll have seen that we capture and display thenpm output for the current command. However, you can also view thenpm output for your entire Visual Studio session (note that npm output from Interactive Window or commpand prompt is not reflected.) To see this:
Open View→ Output
In the Show output from dropdown, select Npm.
You should see something similar to the following:
Happy package-managing!
Comments, compliments, complaints? We'd love to hear your feedback - please file anissue, comment below, or post on theforums.
This page was generated using a tool. Changes will be lost when the html is regenerated. Source file: Npm.md