Skip to content

Node package manager

Image 01

Node Package Manager (npm) is an online repository https://www.npmjs.com/ for the publishing of open-source Node.js projects. This registry is the world's largest Software Registry. The registry contains over 800,000 code packages. npm is free to use and you can download all npm public software packages without any registration or logon. The Node.js community creates useful modules and publishes them as packages in this repository.

Image 02

Node Package Manager is also a command line tool that installs, updates or uninstalls Node.js packages in your application. npm includes a CLI (Command Line Client) that can be used to download and install software. Modules will be installed your project's node_modules folder.

Read more: https://www.npmjs.com/

3rd party modules

Modules from npm are self contained, which means they define a package.json file which declares the entry point to the module, the main file. This is the file which is then required from a module installed from npm. If the module does not declare main file, then the normal module resolution rules apply.

Below is an example of what actually gets required when we require the Express module in our application.

1
const express = require("express")

Package.json

One important use for npm is dependency management. package.json file lists all the packages your project depends on and specifies the version number of a installed package. This file will be gererated when you are creating your project with npm init command. It is a JSON file that lives in the root directory of your project.

Your project's package.json is the place to configure and describe how to interact and run your application. This file enables npm to start your project, run scripts, install dependencies, publish to the npm registry and many other useful tasks.

Example

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
{
  "name": "timereservation",
  "version": "1.0.0",
  "description": "Time Reservation backend application",
  "main": "index.js",
  "scripts": {
    "dev": "nodemon index.js",
    "start": "node index.js",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "Pasi Manninen",
  "license": "MIT",
  "dependencies": {
    "bcrypt": "^5.0.0",
    "cors": "^2.8.5",
    "dotenv": "^8.2.0",
    "express": "^4.18.2",
    "express-async-errors": "^3.1.1",
    "jsonwebtoken": "^8.5.1",
    "mongoose": "^5.11.11",
    "mongoose-unique-validator": "^2.0.3"
  },
  "devDependencies": {
    "nodemon": "^2.0.7"
  }
}

Read more:

Note

At least two fields must be present in the definition file: name and version.

Install all dependencies

If your project has already a package.json, command npm install will install all the dependencies your project needs. You usually need this command when you are cloning Node.js project from the Git, because all Node.js modules will be ignored to push Git repository.

1
npm install

Modules will be installed to node_modules folder in your application.

Install a single module

You can use a following command to install some 3th party module locally (to your project)

1
npm install <module name>

For example, the following command will install Express and also adds dependency entry into the package.json.

1
npm install express

The above command will install the express module into node_modules in the current directory. And it will include express dependency in your package.json file.

1
2
3
4
5
...
"dependencies": {
    "express": "^4.18.2"
  }
...

Express can be used for example in Node.js application to listen client requests:

1
2
3
4
5
6
7
const express = require('express') 
const app = express()
const port = 3000

app.listen(port, () => {
  console.log('Example app listening on port 3000')
})

Update modules

Use a below commands to update a new version of modules to your project.

All modules

1
npm update

or just one modules

1
npm update <module-name>

Uninstall modules

Sometimes you might want to uninstall installed modules. You can use the following command to remove a local modules from your project.

1
npm uninstall <module-name>

Git and Node modules

You should create a .gitignore file in your project directory. This will tell git to ignore certain files. Now you want to ignore all the possible installed node modules.

1
node_modules

If you're using a Visual Studio Code you may also want to add the .vscode folder to the list ignored files and folders.

1
2
node_modules
.vscode

Creating an own JavaScript module

JavaScript allows structure large programs by creating code modules that hold related functions and properties that can be exported in multiple other files that need these properties and functions. You can create your own modules and easily include them in your applications. Developer need to use exports keyword to make those functions and properties available outside the module file. This keeps larger projects organized and code cleaner.

Create and export own message module:

message.js
1
2
3
4
5
const print = () => {
  console.log('This is a demo message!')
}

module.exports = { print }

And use it in application:

index.js
1
2
const message = require("./message")
message.print()

Running the above program (with the command node index.js) would print out This is a demo message!.

Remember that you don't need to export everything in a module. You can have declarations that are only available for use inside the module. Now id in below.

user.js
1
2
3
4
const id = '1234'
const name = 'Kirsi Kernel'
const email = 'kirsi@kernel.com'
module.exports = { name, email }
run.js
1
2
3
4
5
const user = require('./user')

console.log(user.id)
console.log(user.name)
console.log(user.email)

Will display in console:

1
2
3
undefined
Kirsi Kernel
kirsi@kernel.com

own modules

Remember use relative path (./modulename) in your own modules to avoid name clashing with 3rd party or built-in modules.

Read more

Goals of this topic

Understand

  • Node package manager - npm.
  • The concept of modules.