npm – a quick walk
As a part of learning angular, I would suggest everyone learn and understand the basic concept of npm
Maybe by finding answers for few of questions listed below
- What is npm?
- What is the role of npm?
- How to use it?
- Why we are going to use it?
So, what is npm?
npm – node package manager
Let’s split the sentence and understand the meaning present in the abbreviation.
Modules of code grouped up into a component which can be used in various project or application.
Managing the dependencies with the various operation like install, uninstall, update and so on.
Now, we can frame our own definition for npm from above split up an explanation. So using npm, we can manage (install, uninstall, organize) the node.js packages in our project.
npm has CLI terminal and website to search for packages.
What is the role of npm?
npm does N number of roles in various projects. I’m just mentioning the various steps which we are going to use in our upcoming posts in the end user developer point of view
- install packages
- uninstall packages
- version maintenance of packages across the team
- maintaining the dependencies
How to use it?
Simple, install Node.js
npm is distributed with Node.js- which means that when we download and install Node.js, we automatically get npm installed on our computer.
Using following command in terminal, we can check whether our toolset get installed successfully,
// to check node.js version node -v // to check npm version npm -v
What is this packages.json? this is one of the important things we should get familiar if we are going for use npm in our projects.
- packages.json file is like the registry of packages in our project.
- it lists the packages that our project depends on.
- allows us to specify the versions of a package that our project can use using semantic versioning rules.
How to create packages.json file?
Open terminal with your target folder location.
Type npm init and Enter, now you’ll be caught up with few questions. After you have answered most of the question, your file will get created.
name and version fields are mandatory fields.
If you are afraid of questions, we have another quick option.
Enter command npm init –yes
Using –yes argument option, the npm will create packages.json file with default values.
Install a package
npm install (in the package directory, no arguments) – Install the dependencies in the local node_modules folder.
In global mode (ie, with
--global appended to the command), it installs the current package context (ie, the current working directory) as a global package.
You can find detailed details of npm install options in this link
A package can be downloaded with the command:
npm install <package_name>
--save-dev install flags
The easier (and more awesome) way to add dependencies to your
package.json is to do from the command line, flagging the
npm install command with either
--save-dev, depending on how you’d like to use that dependency.
To add an entry to your
npm install <package_name> --save
To add an entry to your
npm install <package_name> --save-dev
That’s it. Few lines have been copied from official documentation. :p
Let’s conclude, using npm we can manage number of packages in our projects easily.