avatarHussain Arif

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

2827

Abstract

7372">To get version of npm</p><div id="d95c"><pre><span class="hljs-built_in">npm</span> -v</pre></div><p id="ab0e">To install exact version of a package eg. <i>express</i>:</p><div id="bb34"><pre><span class="hljs-attribute">npm</span> install express@<span class="hljs-number">4</span>.<span class="hljs-number">2</span>.<span class="hljs-number">0</span></pre></div><p id="3fa0">To install latest version(this is useful when a package requires an update):</p><div id="509f"><pre><span class="hljs-built_in">npm</span> install express@latest</pre></div><p id="6165">To <b>explicitly</b> save into your <b>package.json</b> file(-S or — save ) or into your devDependencies( <b>— save-dev</b> or <b>-D</b>):</p><div id="7d5f"><pre>npm <span class="hljs-keyword">install</span> express --save npm <span class="hljs-keyword">install</span> express -S npm <span class="hljs-keyword">install</span> mocha -D npm <span class="hljs-keyword">install</span> mocha --save-dev</pre></div><blockquote id="cc27"><p>Note: In the newer versions of npm, your packages are installed within your package.json file by default, so you it’s not necessary to explicitly use the <b>-S</b> command in your project</p></blockquote><p id="0418">Sometimes, programmers avoid npm’s versioning system , or they want to use the exact version of a package. To do so, use <b>— exact</b> or <b>-E</b>:</p><div id="0915"><pre>npm <span class="hljs-keyword">install</span> express --exact npm <span class="hljs-keyword">install</span> express -E</pre></div><p id="70fd">For more information about npm versioning, see<a href="https://nodesource.com/blog/semver-tilde-and-caret/"> here</a></p><p id="a970">You can even install more than one package and include multiple flags within one command:</p><div id="6acc"><pre>npm i react react-dom <span class="hljs-keyword">babel </span><span class="hljs-keyword">babel-core </span>-ED</pre></div><p id="afab">To update npm to its latest version, type</p><div id="0128"><pre><span class="hljs-built_in">npm</span> i -g <span class="hljs-built_in">npm</span>@latest</pre></div><h1 id="436b">Listing and removing modules</h1><p id="35c3">To <b>list only locally</b> installed modules you’re using in your project:</p><div id="21d2"><pre>npm <span class="hljs-built_in">ls</span> </pre></div><p id="cf3c">To <b>list only</b> <b>globally</b> installed modules:</p><div id="b07a"><pre>npm <span class="hljs-built_in">ls</span> -g</pre></div><p id="bee3">To <b>remove</b> npm module (eg.<i> express</i>)</p><div id="14ea"><pre>npm <span class="hljs-built_in">rm</span> express</pre></div><p id="ef7c">To <b>remove globally</b> installed module(eg <i>mysql</i>)</p><div id="0f54"><pre>npm <span class="hljs-built_in">rm</span> mysql -g</pre></div><h1 id="1352">NPM configuration</h1><p id="313f">To list settings:</p><div id="

Options

eea6"><pre>npm <span class="hljs-built_in">config</span> ls <span class="hljs-keyword">or </span>npm <span class="hljs-built_in">config</span> list</pre></div><p id="01b7">for global configs:</p><div id="ac42"><pre>npm <span class="hljs-built_in">config</span> <span class="hljs-comment">--global list</span> <span class="hljs-keyword">or</span> npm <span class="hljs-built_in">config</span> -g ls</pre></div><p id="23c2">For all npm config commands:</p><div id="4055"><pre>npm config -h</pre></div><p id="2c13">To change a config ,</p><div id="5d08"><pre>npm config set <key> <value>:</pre></div><p id="07a7"><b>Example:</b></p><div id="35f6"><pre>npm<span class="hljs-built_in"> config </span><span class="hljs-built_in">set</span> registry <span class="hljs-string">"https://registry.npmjs.org"</span></pre></div><p id="0917">To read individual config values:</p><div id="16ad"><pre>npm<span class="hljs-built_in"> config </span><span class="hljs-built_in">get</span> <key> </pre></div><div id="bb02"><pre>npm<span class="hljs-built_in"> config </span><span class="hljs-built_in">get</span> registry</pre></div><p id="0427">To remove config values</p><div id="79bf"><pre>npm config <span class="hljs-keyword">delete</span> email</pre></div><h1 id="72d0">Changing your project’s version</h1><p id="56d5">Let’s take a sample version number : <b>1.2.3</b></p><p id="3b11">The right-most number(<b>3</b>) is your <b>patch version(fixing bugs)</b>. To increment it,</p><div id="701f"><pre>npm <span class="hljs-keyword">version</span> <span class="hljs-keyword">patch</span></pre></div><p id="8164">The middle number(<b>2</b>) is <b>minor </b>version<b>(small changes).</b></p><div id="ed3c"><pre>npm <span class="hljs-built_in">version</span> minor</pre></div><p id="88ba">The left most number<b>(3) </b>is your <b>major version(big changes)</b></p><div id="6c31"><pre>npm <span class="hljs-built_in">version</span> major</pre></div><h2 id="0b49">What is package.json file?</h2><p id="4096">Its a project manifest file, which has metadata about the project such as descriptions,dependencies/packages you have used, license , locations , scripts to build, launch and run.</p><h2 id="b779">When to install packages globally and when to install them locally?</h2><p id="492e">Use the <i>-g</i> flag to install command line tools.</p><p id="a124">Anything you plan to <i>require() </i>must be LOCAL.</p><h2 id="3862">Where can I find the packages I’ve installed for my project?</h2><p id="12a6">In the ‘<b>node_modules</b>’ folder of your project</p><p id="0bf9">Note: Always run npm commands in the <b>project root folder </b>i.e where <b>node_modules</b> folder is located.</p><p id="fdeb">That’s it for today. Thanks for reading, and have a great day!</p><p id="9f65">Stay inside, Stay safe</p></article></body>

Source

The Basics of NPM

What is NPM?

N ode

P ackage

Manager

NPM is a package manager for the Node.JS framework, meaning , a programmer can download third party modules(see here)so that he can use those modules in his project. These modules are built by the programming community.

Common examples can be:

express — makes the process of writing web apps easier morgan — logs HTTP requests, and gives the developer concise insight on how his app is being used

Using NPM through the Command Line

First, create a directory for your project with your JavaScript file in it.

Then, within the same directory, run the command

npm init -y

You are now ready to install packages through NPM!

To install Locally:

npm i package-name
or 
npm install package-name

Thus, to install express, you can go

npm i express
or
npm install express

Note:i is just an alias to install. They both can be used interchangeably

Most packages are installed locally if your projects will use the module i.e you will use require() to include this module in your project.

The packages are saved within the ‘node_modules’ folder of your project

To install Globally:

You will include the -g flag within your commands

npm install -global module-name
or
npm i -g module-name

Most command line tools are installed globally

The packages are saved within your global directory. In many cases, this will be your C:/Users/<user name> /npm directory.

Additional Commands

To see all main commands:

npm help

To get version of npm

npm -v

To install exact version of a package eg. express:

npm install express@4.2.0

To install latest version(this is useful when a package requires an update):

npm install express@latest

To explicitly save into your package.json file(-S or — save ) or into your devDependencies( — save-dev or -D):

npm install express --save
npm install express -S
npm install mocha -D
npm install mocha --save-dev

Note: In the newer versions of npm, your packages are installed within your package.json file by default, so you it’s not necessary to explicitly use the -S command in your project

Sometimes, programmers avoid npm’s versioning system , or they want to use the exact version of a package. To do so, use — exact or -E:

npm install express --exact
npm install express -E

For more information about npm versioning, see here

You can even install more than one package and include multiple flags within one command:

npm  i react react-dom babel babel-core -ED

To update npm to its latest version, type

npm i -g npm@latest

Listing and removing modules

To list only locally installed modules you’re using in your project:

npm ls 

To list only globally installed modules:

npm ls -g

To remove npm module (eg. express)

npm rm express

To remove globally installed module(eg mysql)

npm rm mysql -g

NPM configuration

To list settings:

npm config ls
or
npm config list

for global configs:

npm config --global list
or 
npm config -g ls

For all npm config commands:

npm config -h

To change a config ,

npm config set <key> <value>:

Example:

npm config set registry "https://registry.npmjs.org"

To read individual config values:

npm config get <key> 
npm config get registry

To remove config values

npm config delete email

Changing your project’s version

Let’s take a sample version number : 1.2.3

The right-most number(3) is your patch version(fixing bugs). To increment it,

npm version patch

The middle number(2) is minor version(small changes).

npm version minor

The left most number(3) is your major version(big changes)

npm version major

What is package.json file?

Its a project manifest file, which has metadata about the project such as descriptions,dependencies/packages you have used, license , locations , scripts to build, launch and run.

When to install packages globally and when to install them locally?

Use the -g flag to install command line tools.

Anything you plan to require() must be LOCAL.

Where can I find the packages I’ve installed for my project?

In the ‘node_modules’ folder of your project

Note: Always run npm commands in the project root folder i.e where node_modules folder is located.

That’s it for today. Thanks for reading, and have a great day!

Stay inside, Stay safe

Node
Nodejs
JavaScript
Programming
Coding
Recommended from ReadMedium