Node.js standard packages and modules are installed through standard packaging application – called as Node Package Manager(NPM). NPM is the standard for packaging the node.js components and modules. In the packages, the “package.json” is included to write the dependencies in Node.js and the startig file in node package.
There are two ways of installing node packages – Local and Global.
For Global Packages, the node packages will be loaded and installed in shared folders and those can be used in several node.js applications which are developed in different workspaces. In shared folder one version of node.js packge will be installed.
In local mode, node.js keeps same node packages in different application folders within a directory called – ‘node_modules’ where the different local node modules will be installed. NPM works with local mode by default and to work with global mode, the NPM command should be installed with “-g” option.
Some important NPM commands we have put here below –
A> npm ls -it will show all the packages and versions and show those in terminal console.
B> A variation of “npm ls” is to tag with options- like (list all installed packages) –
- npm ls installed
- npm ls stable – to show all the node packages and modules which are declared as stable.
- npm ls installed stable – to combine the above filters
C> Search for the installed packages with a particular string within.
npm ls sample
Here npm will search and show packages and modules with “sample” word contained.
D> To get query with version numbers –
npm ls @2.0
E> To install latest version of a node package –
npm install package_name
Example - npm install validation
It will install the node validation module within local node module repository from the Internet.
F> To install specific version of package –
npm install package@version
This will be specially required in case of any particular function invocation, which may not be present in the node present stable version.
G> To install some module within global context the command will be –
npm install -g package_name
Example - npm install -g express
It will install the express.js package with node.js global node in local development environment.
H> To remove a perticular package
npm rm package_name
– it will delete all the specific packages installed from node.js repository. To remove file from Global repository,
we need to use “-g” switch.
Example – npm rm -g validation
I> To veiw information for some package, we need to use
npm view validation
J> To view information for some specific versioned package –
npm view [email protected]
That is all for now. We will write more on other node.js features in our later articles.