Signup/Sign In

NodeJs Npm Introduction

Using npm successfully is a pillar of modern web creation, whether it's solely for Node.js, as a package manager or construct tool for the front-end, or as part of workflows in other languages and platforms.

For those that are completely new to Node.js, npm, and the related environment, this tutorial is a simple and informative guide to understanding npm.

Let's dive straight into this tutorial!

What is NPM?

It can be stated as the default package manager for JavaScript's runtime Node.js is NPM, or "Node Package Manager". It consists of two following parts:

  • a command-line interface (CLI) for publishing and installing packages.
  • a web-based archive for JavaScript packages

For a more visual description, when npm is installed, dependencies are provided to JavaScript developers in the following format:

dependencies are provided to JavaScript developers

and the procedure for releasing a kit for your JS will be as follows:

procedure for releasing a kit for your JS

package.json

A package.json file is a manifest of the project that contains information about the programs and applications it relies on upon, details about its particular source control, and basic documentation including the project's name, description, and author.

When npm init is used to start a JavaScript/Node.js file, package.json is created, with the following basic metadata provided by developers:

  • name: the name of your JavaScript project or library
  • version: the current state of your project This area is often overlooked in application development because there is no clear need for versioning open-source libraries.
  • description: the project's description
  • license: the project's license

npm scripts

The script property in package.json can be used to run command-line utilities that are configured in the project's local context. The scripts section of an npm project, for example, might look like this:

{
  "scripts": {
    "build": "tsc",
    "format": "prettier --write **/*.ts",
    "format-check": "prettier --check **/*.ts",
    "lint": "eslint src/**/*.ts",
    "pack": "ncc build",
    "test": "jest",
    "all": "npm run build && npm run format && npm run lint && npm run pack && npm test"
  }
}

Not actually built as global executables, but rather as local to your project within node modules/.bin/ with eslint, prettier, NCC, and jest.

dependencies vs devDependencies

These are key-value objects that have the names of npm libraries as the key and their semantic-formatted variants as the value. Any value of both dependencies and devDependencies is the version set that's appropriate to update, and every key is the name of a package.

This is an example of a TypeScript Action template from Github:

{
  "dependencies": {
    "@actions/core": "^1.2.3",
    "@actions/github": "^2.1.1"
  },
  "devDependencies": {
    "@types/jest": "^25.1.4",
    "@types/node": "^13.9.0",
    "@typescript-eslint/parser": "^2.22.0",
    "@zeit/ncc": "^0.21.1",
    "eslint": "^6.8.0",
    "eslint-plugin-github": "^3.4.1",
    "eslint-plugin-jest": "^23.8.2",
    "jest": "^25.1.0",
    "jest-circus": "^25.1.0",
    "js-yaml": "^3.13.1",
    "prettier": "^1.19.1",
    "ts-jest": "^25.2.1",
    "typescript": "^3.8.3"
  }
}

The —save and —save-dev flags on the npm update command are used to install these dependencies. They're intended for use in both manufacturing and development/test environments.

Install Modules with npm install

When you're first getting started with npm, one of the most basic things you can understand is how to install modules from npm. npm update can, by default, install the most recent version of a package with the version symbol. As you read more about downloading modules, you'll notice some differences, so here's the gist of what you need to do to load a standalone module into the current directory:

npm install <module>

Replace <module> with the name of the module you want to load in the above command.

npm install express

Since npm has made downloading JavaScript packages so easy, this command is often misused.

Install Modules Globally on your System

The flags to install a module globally on your device are the final and most common flags for npm install that you can use. Global modules can be incredibly useful; there is a plethora of software, services, and other items that you can add globally for both creation and general use. If you want to install a module from npm globally, just use the —global flag while executing the install button.

npm install <module> --global 

Conclusion

That's everything we need to know about npm to get started. If you want to take npm to the next level and start deploying Node.js applications and npm modules to development, check out the guide for more awesome and insightful tutorials. In meantime enhance your recently developed npm abilities.



About the author: