Skip to content

Files

Latest commit

 

History

History

learn npm

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
Learn npm
npm

Welcome to Learn NPM

Table of Contains

  1. About npm
  2. Getting Started
  3. Conclusion
  4. Author
  5. Contributions

About npm

npm (short for Node Package Manager) is a package manager for the JavaScript programming language. With npm, developers can easily install and manage hundreds of thousands of packages from the npm registry, which is a public repository of open-source JavaScript packages. npm supports CLI (Command Line Interfacce) through which developers can perform various tasks such as installing packages, updating packages, removing packages, searching for packages, publishing packages, and managing dependencies.

Overall, npm is an essential tool for modern JavaScript development, making it easy to manage dependencies, speed up development, and improve the quality of code.

Several Most Common npm Packages

react: A popular JavaScript library for building user interfaces. react-router: Declarative routing for React. nodemon: Automatically restart node application when file changes are detected. express: Fast, unopinionated, minimalist web framework for Node.js. typescript: TypeScript is a language for application scale JavaScript development. mongoose: A library for working with MongoDB databases in Node.js. axios: Promise based HTTP client for the browser and node.js

Getting Started

All the basic stuffs will be covered regarding npm and package.json file. All you need to do just install node in your system, and you are ready to go.

Basic npm Commands

In here, I discussed regarding npm help documentations shown in the terminal. If you get ever stuck in the middle of a work, it is really annoying. Help documentation might come to rescue.

Syntax
npm <command>
Most common <command> name:
Command Alias Description
init Create a package.json file
install i install all the dependencies in your project
install <foo> i <foo> add the <foo> dependency to your project
update Update packages
uninstall Remove a package
<command> --help <command> -h quick help on <command>
help <term> search for help on <term> (in a browser)
--help -h display help documentation
--version -v show version

Step-1: Command Warm-Ups:

1. How can you check your npm version?
npm -v

or,

npm --version
2. Print npm help documentation.
npm -h

or,

npm --help
3. Let you want to install something. But you need some documentation help. How can you get it?
npm i -h

or,

npm install --help
4. Okay, let's say, you need more details help documentation. What about in a browser based help documentation!! How can you get it?
npm help install

Step-2: Making package.json File:

5. How can you create a package.json file by npm?
npm init

or, to skip questions,

npm init -y

This creates a package.json file in working directory. Open it and surf. It probably appears like this.

Step-3: npm Install Process:

Flag Meaning
-g install globally
--save-dev install as a dev-dependency
--save (default) install as a dependency
6. Suppose, you want to add `express` in your package.json under `dependencies`. How can you do that?
npm i express

Note: > 4.18.2 means: major version . minor version . patch version

Version Meaning
4.18.2 dependency is not allowed to update to any version except this one.
^4.18.2 dependency is allowed to update to any minor or patch version, as long as the major version remains the same.
~4.18.2 dependency is allowed to update to any patch version, but not to any minor or major version.
* dependency is allowed to update to any version.
latest dependency must be latest version.
dependencies
7. If you want to install jest as a dev dependency, what should you need to do?
npm i jest --save-dev
dev-dependency
8. Is it possible to install multiple package at the same time?
npm install <package-1> <package-2> <package-3> .........
9. How can we install `nodemon` globally?
npm i nodemon -g
10. What is the meaning of npm install?

It installs all the dependencies and dev-dependencies in package.json

Step-4: npm Update and Uninstall:

Syntax:

To update a package: npm update <package>
To uninstall a package: npm uninstall <package>

Package.json Script Field

  "scripts": {
    "start": "node index.js",
    "dev": "nodemon dev.js"
  },

To access this, type npm start or npm run dev in the terminal.

Official Docs About Scripts

What About npx

npx stands for Node Package eXecute. From version 5.2+, npx is used to execute node packages without the need to install them. With npx, you can run a package by simply specifying its name and version, and npx will download and execute it for you. This is particularly useful when you need to run a one-time command or script, or when you want to try out a package without installing it permanently.

Example:
npx create-react-app my-app

Conclusion

At this point, a basic understanding and working process should be achieved. Use this artical as a cheat sheet.

Author

  • Mirza Monirul Alam
  • Full Stack Web Developer.

Contributions

Any contributions will be appreciated. THANK YOU