DEV Community

Cover image for Getting to Know Node.js (Part III)
Oscar Luna
Oscar Luna

Posted on • Edited on

Getting to Know Node.js (Part III)

Today's the day, you guys! Today we will finally dive into npm! So last time I prefaced Node's modular system and how we can use it to call upon other files or built-in modules for use in our code. Well node gives us access to much more than built-in modules. In fact, Node gives us access to an online repository with millions of JavaScript modules written and published by other users. This repository is known as the Node Package Manager (npm). And without further ado, let's get into it!

npm

On our command line, if we run npm install, Node will do a couple of things. First, it creates a directory called node_modules that will store the packages we want to install. This folder gets stored in the directory we run npm install from. Prior to running npm install we can run npm init which creates a package.json file that, initially, contains information about our project (ie. name, author, project version, scripts). Node will ask a series of questions to obtain this information, which can either be answered one by one, or can all be given the answer 'yes' by using the --yes tag. Values left blank can be filled in later.

$npm init --yes

--

//generated package.json file

{
  "name": "current_directory_name",
  "description": "",
  "version": "0.1.0",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "repository": {
    "type": "git",
    "url": ""
  },
  "keywords": [],
  "author":"",
  "license": "ISC",
  "bugs": {
    "url": ""
  },
  "homepage": ""
}

Enter fullscreen mode Exit fullscreen mode

note: NPM contains a public registry for all members to download and use, which is
completely free. NPM also offers signing up as an npm Enterprise admin, but that >feature will be retired in June 2021

As we install dependencies to our project, a dependencies object gets stored containing the dependency names and their version numbers. If there isn't a package.json file in our directory when we run npm install, then Node will generate one containing all the aforementioned data.

This file also makes your project reusable to other users.

NPM requires each dependency and our project to contain their own version numbers. This is known as semantic versioning. Dependencies update over time, and this number system helps identify when changes to a package can break our project or not. Dependency versions are marked as three numbers, separated by decimal points (ie. React 16.13.1). Updates to functionality are indicated by increasing the second number by 1. These changes won't necessarily break your project. However, when a change will break compatibility with other dependencies in your project, then the change is marked by increasing the first number by 1. Dependency A updating from version 1.2.1 to 1.3.1 won't break your project, but Dependency A version 2.0.0 can. Incrementing the third number indicates bug fixes that are reverse compatible. As an example, bug fixes introduced in version 2.0.1 of a dependency are compatible with version 2.0.0.

Packages also contain scopes that match the user or organization name. These scopes are indicated with an @ in the beginning and a / and the end of the name space. for example, packages published by the organization Material-UI are scoped as @material-ui/package_name. If a package is not scoped, then we can use the public registry namespace, like styled-components.

The registry contains libraries and frameworks like React, Express, Next, MongoDB, etc., which we can call in our projects as needed.

//index.js
const React = require('react');
const ReactDOM = require('react-dom');

const rootNode = document.getElementById('root');
ReactDOM.render(<App />, rootNode);
Enter fullscreen mode Exit fullscreen mode

Apart from downloading packages from the repository we can also use npm to publish packages. This can be done either privately (to other developers if creating a package as a team) or publicly. To be able to download or publish from the npm registry we also need to create an account at https://npmjs.com.

Well this has been fun! I'll leave this here so you can go explore that npm registry, and next time we'll talk about the HTTP server, and how to create them. Until next time!


Works Cited

  1. Haverbeke, Martin "Node.js", Eloquent JavaScript - A Modern Introduction to Programming, 3rd Edition, 2019, Chapter 20, No Starch Press, Inc.
  2. https://docs.npmjs.com

Top comments (0)