📜 ⬆️ ⬇️

Cheat Sheet for NPM Package Manager


npm is the node.js batch manager. With it, you can manage modules and dependencies.
A small cheat sheet of all my favorite npm commands:


Npm installation


curl https://npmjs.org/install.sh | sh 


Npm update


There are several ways to update npm. I prefer:
 curl https://npmjs.org/install.sh | sh 

or
 npm install npm -g 


Search for packages in npm


 npm search hook.io 

Tip: You can also use search.npmjs.org
The second hint: To search you need to know the name of the desired package (everything looks for any word in the package name, and in its description, can it be translated incorrectly?)
')

View package information


 npm view hook.io 


Local package installation


For demonstration, take the http-server package.
http-server is a package that has been wrapped around the node's core http.Server class. This module makes it a good example, since its API provides both the CLI binary and a requitable 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 set the http-server in our working directory.
You will see a new folder in node_modules. Now you can not pay attention to it.

Install 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 is this magic? (author well done)
The http-server is not the base node.js module. This package we just installed 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 where you are currently working. It is right. This may seem a bit confusing if you have previously worked with previous package management systems.
For example:
 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.<anonymous> (/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.<anonymous> (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 to 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 the http-server must be set globally and be accessible to all applications.
Now we can call it require ('http-server') in any of our applications.

In addition, since the http-server package has its own executable file, this file will also be installed as an executable http-server and is available in commands.
Now you can just 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 version of the package


 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 in npm. Fortunately, source codes for most npm modules are 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 the http-server is linked locally.

Link 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 to the npm repository.
 cd http-server/ npm link 

On our local version, the http-server is created "connected" for our local machine. (the link is created as a “copy-paste”, from the beginning you need to go to the right directory and make a “copy”, then go to the right directory and make a “paste”. So now we have learned how to do a “copy”, and below will be about " paste "this module)

Link local packages for multiple applications


As we saw earlier, npm installs packages into the local default directory. So the npm link (link) works almost the same.
 mkdir newapp/ cd newapp/ npm link http-server 

We indicate that we have now created a connection from the http-server to our new application newapp. If we didn’t run the npm link http-server, we would get an error about the missing module. (and here is our “paste” about what I wrote above, now you should understand the logic of creating links)

Unlinking 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 are canceling our “copy” for this package)

Creating a new package


 mkdir mypackage/ cd mypackage/ npm init 

(I just want to note from myself that creating a package is not such an easy task in one command, you can read more in another article )

Add new user


 npm adduser 


Publish the package to the npm repository


 cd mypackage/ npm publish 


Remove package from npm repository


 npm unpublish http-server 


Managing access rights to packages in the npm repository


You can set other users' access rights 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 the package.json format and all the intricacies of working with npm, see Charlie Robbin's article: blog.nodejitsu.com/package-dependencies-done-right

Source: https://habr.com/ru/post/133363/


All Articles