What is npm? A basic introduction for beginners

npm is the Node Package Manager that is included and helps every development associated with Node. For years, Node has been widely used by developers to share tools, install various modules, and manage their dependencies. That’s why it’s critical that the people you work with understand what npm is. Below we will explain more about how it works and how to install npm.

How does the Node Package Manager work?

This tool works in two ways:

  • As a widely used repository for publishing open source Node.js projects. Which means that it is an online platform where anyone can post and share tools written in JavaScript.
  • As a command line tool that helps to interact with online platforms such as browsers and servers. This helps with installing and uninstalling packages, version management, and dependency management needed to run a project.

To use it, you must install node.jssince they are developed in a grouped way.

The Node package manager command line utility allows node.js works correctly.

To use packages, your project must contain a file called package.json. Inside that file, you’ll find project-specific metadata.

The metadata shows some aspects of the project in the following order:

  • The name of the project
  • The initial version
  • Description
  • the entry point
  • test commands
  • the repository
  • Keywords
  • License
  • dependencies
  • development dependencies

The metadata helps identify the project and acts as a baseline for users to learn about it.

Here is an example of how you can identify a project through its metadata:

{ “name”: “-npm”, “version”: “1.0.0”, “description”: “npm beginners guide”, “main”: “beginners-npm.js”, “scripts”: { ” test”: “echo \”Error: no test specified\” && exit 1″ }, “keywords”: , “author”: “International “, “license”: “MIT”, “dependencies”: { “express” : “^4.16.4” } }

  • The name is -npm
  • version is 1.0.0
  • This is one npm guide for beginners.
  • The entry point or main file of the project is beginners-npm.js
  • The keywords or tags to find the project in the repository are npm example Y essential
  • The author of the project is International.
  • This project is licensed under MIT
  • Dependencies or other modules that this module uses are express 4.16.4

How to install npm modules and start projects?

First, you have to make sure that node.js Y npm have been installed. Do it by running a few simple commands.

See also  Payment gateway: What is it, how does it work, the best gateways compared

To see if node.js is installed, open the Terminal or a command line tool and type node -v. This should display a version number if it has already been installed:

$ node -v v0.10.9

To see if the Node package manager has been installed, type npm -v. This should display the version number:

$ npm -v 1.2.25

If it is not installed, download Node del and follow the installer instructions.

npm is famous for its one line installer:

$ curl https://npmjs.org/install.sh | sh

Once installed, you can update it as new versions come out regularly. To update it, simply download the installer from the site and run it again. The newer version will automatically replace the latest one.

However, you can also update it using this command:

$ npm update -g npm

Starting a project with npm

If you already have Node and npm, and want to start building your project, run the command npm init. This will trigger the initialization of your project.

For example, let’s create a directory called test-npm and cd into it. Now let’s run our first npm command:

$ npm init

This command works as a tool to create the file package.json of a project. Once you run the npm init steps, a file will be generated package.json and it will be saved in the current directory.

As an aid, npm’s init explains what it does when you run it:

mymacs-MacBook-Pro: test-npm mymac$ npm init This utility will walk you through creating a package.json file. It only covers the most common items, and tries to guess sane defaults. See `npm help json` for definitive documentation on these fields and exactly what they do. Use `npm install –save` afterwards to install a package and save it as a dependency in the package.json file. Press ^C at any time to quit. name: (test-npm)

See also  blog archives

Respond to prompts from npm init.

name: (test-npm)-npm version: (1.0.0)1.0.0 description: npm beginners guide entry point: (index.js) beginners-npm.js test command: git repository: keywords: npm example , basic author: Internationallicense: (ISC) MIT

Press “Enter” to accept. Then npm init will give you a preview of the package.json what is to be created.

It will look like this:

{ “name”: “-npm”, “version”: “1.0.0”, “description”: “npm beginners guide”, “main”: “beginners-npm.js”, “scripts”: { ” test”: “echo \”Error: no test specified\” && exit 1″ }, “keywords”: , “author”: “International “, “license”: “MIT”, “dependencies”: { “express” : “^4.16.4” } } Is this OK? (yes) yes mymac-MacBook-Pro: test-npm mymac$

Write “And it is» and press «Enter» to confirm, so you will save the package.json. You can make changes later, either by editing the file directly or by running npm init again.

Installing npm modules

A package in node.js contains all the files you need to install a module. Modules are JavaScript libraries that you can include in your project.

Installing modules is one of the most basic things you should learn to do when getting started with the Node package manager. This is the command to install a module in the current directory:

$ npm install $ npm i

In the above command, replace with the name of the module you want to install.

For example, if you want to install Express, the most widely used and popular node.js web framework, you can run the following command:

$ npm install express npm notice created a lockfile as package-lock.json. You should commit this file. npm WARN -npm@1.0.0 No repository field. + express@4.16.4 added 48 packages from 36 contributors and audited 121 packages in 2.798s found 0 vulnerabilities mymac-MacBook-Pro: test npm mymac$

See also  WordPress Hosting | Safe, Fast and Best Price

The above command will install the express module to /node_modules in the current directory.

Whenever you install a module from the Node package manager, it will be installed in the folder node_modules.

This is what it will look like after installing a module in your project:

{ “name”: “-npm”, “version”: “1.0.0”, “description”: “npm beginners guide”, “main”: “beginners-npm.js”, “scripts”: { ” test”: “echo \”Error: no test specified\” && exit 1″ }, “keywords”: , “author”: “International “, “license”: “MIT”, “dependencies”: { “express” : “^4.16.4” } } mymac-MacBok-Pro: test-npm mymac$

conclusion

As you can see, in addition to working as an online database for various packages node.jsa primary goal of the Node package manager is automated dependency management for packages included in the Node package manager. package.json with its command line interface.

As a summary, we leave you some basic commands that you should know to:

$ curl https://npmjs.org/install.sh | sh $ npm -v $ npm init $ npm install $ npm i

Now yes! We have reached the end. Now you know what npm is, how to install it and how to use it. If you plan to work with JavaScript, the Node package manager is an indispensable tool for your workflow.

Deyi is a digital marketing enthusiast, with a background in web design, content creation, copywriting, and SEO. She is part of ‘s SEO & Localization team. In her free time, she likes to develop projects, read a book or watch a good movie.

Loading Facebook Comments ...
Loading Disqus Comments ...