NPM Package Manager Cheat Sheet
- Transfer
npm is a node.js package manager . Use it to manage modules and dependencies.
A little cheat sheet for all my favorite npm commands:
- Npm installation
- Npm update
- Search packages in npm
- View Package Information
- Install packages locally
- Installing the package in our application
- Understanding the difference between global and local installation
- Global Package Installation
- Removing a locally installed package
- Removing a globally installed package
- Installing a specific package version
- Installing a module with Github
- Associate any packages locally
- Local Package Associations for Multiple Applications
- Unlink application packages
- Unlinking a package in the system
- Create a new package
- Add new user
- Publish package to npm repository
- Removing a package from the npm repository
- Manage package permissions in the npm repository
Npm installation
curl https://npmjs.org/install.sh | sh
Npm update
There are several ways to upgrade npm. I prefer:
curl https://npmjs.org/install.sh | sh
or
npm install npm -g
Search packages in npm
npm search hook.io
Hint: You can also use search.npmjs.org
Second hint: To search, you need to know the name of the package you need (everything looks fine for any word in the package name or its description, maybe it translated incorrectly?)
View Package Information
npm view hook.io
Install packages locally
For demonstration, let's take the http-server package.
http-server is a package we've written which provides an easy to use wrapper around node's core http.Server class. This module makes for a good example, since it's API provides both a CLI binary and a requirable node.js module.
http-server - the package we wrote provides a simpler interface to use the basic http.Server module from node.js. This module is a good example of using the API for both the binary CLI and the node.js. plug-in.
npm install http-server
So we will install http-server in our working directory.
You will see a new folder in node_modules. Now you can not pay attention to it.
Installing the package in our application
mkdir mynewapp/
cd mynewapp
npm install http-server
touch test.js
test.js
var HTTPServer = require('http-server');
var httpServer = new HTTPServer({
root: './public'
});
httpServer.start();
run the script
node test.js
Notice how we do: require ('http-server')? What kind of magic is this? (author well done)
http-server is not a base module of node.js. We just installed this package from npm. Node.js and npm interact and automatically connect our local modules from the node_modules directory.
Understanding the difference between global and local installation
By default, npm will install all packages in the local directory in which you are currently working. It is right. This may seem a bit confusing if you have worked with previous package management systems before.
For instance:
mkdir anotherapp/
cd anotherapp/
touch test.js
test.js
var HTTPServer = require('http-server');
now run our script
node test.js
we get this error:
node.js:134
throw e; // process.nextTick error, or 'error' event on first tick
^
Error: Cannot find module 'http-server'
at Function._resolveFilename (module.js:326:11)
at Function._load (module.js:271:25)
at require (module.js:355:19)
at Object. (/Users/maraksquires/dev/nodeapps/anotherapp/test.js:1:80)
at Module._compile (module.js:411:26)
at Object..js (module.js:417:10)
at Module.load (module.js:343:31)
at Function._load (module.js:302:12)
at Array. (module.js:430:10)
at EventEmitter._tickCallback (node.js:126:26)
This is quite logical, we installed the http-server locally in "/ mynewapp /" and not in "/ anotherapp /".
There are two solutions in this situation:
a) Install the package again, but locally in our new application
cd anotherapp/
npm install http-server
b) Install the package globally
npm install http-server -g
Global Package Installation
If you want the package to be available to all applications, you need to install it globally:
npm install http-server -g
The -g flag means that http-server should be installed globally and be accessible to all applications.
Now we can call it require ('http-server') in any of our application.
In addition, since the http-server package has its own executable file, this file will also be installed as the http-server executable and is available in the commands.
Now you can simply run the command:
http-server
Removing a locally installed package
npm uninstall http-server
Removing a globally installed package
npm uninstall http-server -g
Installing a specific package version
npm install http-server@0.3.0
Installing a module with Github
Important. In some cases, there will be patches, forks, or branches that you want to use, but which have not yet been published to npm. Fortunately, source code for most npm modules is also available at www.github.com
git clone git://github.com/nodeapps/http-server.git
cd http-server/
npm link
Now our cloned version of http-server is connected locally.
Associate any packages locally
If you have a separate directory containing the npm package, then you can create a local link for it. This is useful in situations where we do not want to publish our package in the npm repository.
cd http-server/
npm link
On our local version, the http-server is created “connected” for our local machine. (the connection is created as “copy-paste”, from the beginning you need to go to the desired directory and make “copy”, then go to the desired directory and make “paste.” So now we have learned how to make “copy”, and below it will be about paste ”of this module)
Local Package Associations for Multiple Applications
As we saw earlier, npm installs packages in the default local directory. So npm link (link) works almost the same.
mkdir newapp/
cd newapp/
npm link http-server
We indicate that we have now created a link from the http-server to our new app newapp. If we had not performed npm link http-server, we would have received an error about the missing module. (and here is our “paste” as I wrote above, now you should understand the logic of creating links)
Unlink application packages
cd newapp/
npm unlink http-server
(here we just cancel our "paste" for this application)
Unlinking a package in the system
cd http-server/
npm unlink
(here we cancel our “copy” for this package)
Create a new package
mkdir mypackage/
cd mypackage/
npm init
(I just want to note on my own that creating a package is not such a simple task in one command, you can read more in another article )
Add new user
npm adduser
Publish package to npm repository
cd mypackage/
npm publish
Removing a package from the npm repository
npm unpublish http-server
Manage package permissions in the npm repository
You can set the access rights of other users to the published package:
npm owner add marak http-server
npm owner rm marak http-server
npm owner ls http-server
For more information about package.json format and all the intricacies of working with npm, you can read Charlie Robbin's article: blog.nodejitsu.com/package-dependencies-done-right