Original: http://www.sitepoint.com/beginners-guide-node-package-manager/
Installing node. js
Verify that your installation is successful.
$ node> Console.log (' node is running '); Node is running>. Help.break Sometimes you get stuck, this gets you out.clear Alias for. Break.exit exit the Repl.h ELP Show repl options.load load JS from a file to the REPL session.save Save all evaluated commands in this REPL session to a file>. exit
Node has been successfully installed, now see if NPM installs successfully and which version of NPM is installed.
$ npm-v1.4.28
(Mine is)
Node Packaged Modules
NPM installation packages is available in both local and global modes. Installing in local mode installs the package in the Node_modules directory under your current directory. Installation in global mode {prefix}/lib/node_modules/
is installed under a folder ( {prefix}
usually /usr/
or /usr/local
). This means that you may need to use sudo to install the global package.
Modify the installation directory for the global package
See what the NPM config output is.
$ NPM Config list
Now we know the global location.
$ npm config Get prefix/usr/local
This prefix is what we want to modify, in order to install the global package in our home directory, we create a new folder in the home directory
$ cd && mkdir. node_modules_global$ npm config set prefix= $HOME/.node_modules_global
After a simple modification, the catalog of the global package has been modified. Now create a. npmrc file in the home directory.
$ npm config get prefix/home/sitepoint/.node_modules_global$ cat. Npmrcprefix=/home/sitepoint/.node_modules_global
Because we have modified the global package installation location We need to reinstall NPM, this time we will install in a new location
$ NPM Install NPM--global[email protected]/home/sitepoint/.node_modules_global/lib/node_modules/npm
Finally, we need to add .node_modules_global/bin 到我们的$PATH环境变量
. After running the following command, remember to restart terminal.
Export path= "$HOME/.node_modules_global/bin: $PATH"
Run the following command to see where NPM is installed and which version of NPM is installed.
$ which npm/home/sitepoint/.node_modules_global/bin/npm$ npm-v2.6.0
(I will not modify the directory)
Install the package in global mode
Current location We only installed a package in global mode that's NPM itself. Now let's install UGLIFYJS in global mode. I use --global
flag and he has an abbreviation for it -g
.
$ NPM Install Uglify-js--global[email protected] Node_modules/uglify-js├──[email protected]├──[email Protected]├──[em AIL protected] ([email protected]) └──[email protected] ([email protected])
With the output you can see that the other 4 package is also installed – this is because UGLIFY-JS relies on these 4.
See which global package is installed
Use the NPM List command to see which global packages are installed.
$ NPM List--global├─┬[email protected]│├──[email protected]│├──[email protected] ......... ..... └─┬[email protected] ├──[email protected] ├─┬[email protected] │└──[email protected] ├─┬[email Prot Ected] │└──[email protected] └──[email protected]
The content of this output is very much, and they also output the dependent package that we installed. We can use--depth=0.
$ NPM list-g--depth=0├──[email Protected]└──[email protected]
We can use the UGLIFYJS we just installed to parse JS. The following command will be Minify example.js
to example.min.js
:
$ UGLIFYJS Example.js-o example.min.js
Install the package in local mode
Removing--global is the installation of the package in local mode. The package will be installed in your current directory under the Node_modules folder. Now create a project directory in the home directory
$ mkdir ~/project && cd ~/project$ npm install underscore[email protected] node_modules/underscore$ Lsnode_module S $ ls Node_modulesunderscore
Lists which local package was installed
$ npm List/home/sitepoint/project└──[email protected]
Uninstalling the Local package
NPM is a package manager, so we'll definitely be able to use it to uninstall packages:
$ npm Uninstall underscoreunbuild [email protected]$ npm list/home/sitepoint/project└── (empty)
Specifies that a version of the package is installed
Write the version after @ is good.
$ NPM Install [email protected][email protected] node_modules/underscore$ NPM List/home/sitepoint/project└──[email Protected
Update package
$ NPM Update underscore[email protected] node_modules/underscore$ NPM List/home/sitepoint/project└──[email protected]
Search Find Packages
This article uses the mkdir command multiple times. Is there a node package that can do the same thing? Let's find out.
$ NPM Search MKDIRNPM WARN Building The local index for the first time, please be patientmkdirp
Manage Cache
When NPM installs a package it automatically retains a copy so that the next time you want to install the package, it doesn't need to be downloaded online, just go and get the copy. These copy are slow to exist in the home directory under the. NPM directory.
$ ls ~/.npmnpm registry.npmjs.org underscore ...
The Packagek in this directory may have many older versions of the package, so sometimes we need to clean them up.
$ NPM Cache Clean
Management dependencies
It is unwise to install dependencies manually, and we can use Package.json to manage the package in the root directory of the project. You can use NPM init to create Package.json
$ npm Initthis utility would walk you through creating a Package.json file. Press ^c at no time to Quit.name: (Project) Demoversion: (1.0.0) Description:demo of Package.jsonentry point: (main.js) Test command:git Repository:keywords:author:Sitepointlicense: (ISC)
The contents of Package.json are as follows:
{ "name": "Demo", "version": "1.0.0", "description": "Demo Package.json", "main": "Main.js", " Dependencies ": { " mkdirp ":" ^0.5.0 ", " underscore ":" ^1.8.2 " }, " Devdependencies ": {}, " scripts ": { " test ":" Echo \ "Error:no test specified\" && exit 1 " }, " author ":" SitePoint ", " License ":" ISC "}
You can add private: true
some warnings to avoid running NPM installl. Create a new directory using Package.json to install our dependencies.
$ mkdir ~/demo && CD ~/demo$ CP ~/project/package.json ~/demo$ npm install$ npm list[email protected]/home/sitepo Int/demo├─┬[email protected]│└──[email Protected]└──[email protected]
Look, it's easy to install the package in another directory based on the previous Package.json file. When we install the new package, how do we get him to reflect in the Package.json? We can use--save.
$ npm Install request--save$ NPM list--depth=0[email protected]/home/sitepoint/demo├──[email protected]├──[email Prot Ected]└──[email protected]
package.json被更新了
:
"Dependencies": { "mkdirp": "^0.5.0", "request": "^2.53.0", "underscore": "^1.8.2"}
A Beginner ' s Guide to npm-the Node package Manager