
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 -yYou are now ready to install packages through NPM!
To install Locally:
npm i package-name
or
npm install package-nameThus, to install express, you can go
npm i express
or
npm install expressNote: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-nameMost 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 helpTo get version of npm
npm -vTo install exact version of a package eg. express:
npm install express@4.2.0To install latest version(this is useful when a package requires an update):
npm install express@latestTo 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-devNote: 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 -EFor 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 -EDTo update npm to its latest version, type
npm i -g npm@latestListing 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 -gTo remove npm module (eg. express)
npm rm expressTo remove globally installed module(eg mysql)
npm rm mysql -gNPM configuration
To list settings:
npm config ls
or
npm config listfor global configs:
npm config --global list
or
npm config -g lsFor 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 registryTo remove config values
npm config delete emailChanging 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 patchThe middle number(2) is minor version(small changes).
npm version minorThe left most number(3) is your major version(big changes)
npm version majorWhat 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