bugl
bugl
HomeLearnPatternsPathsSearch
HomeLearnPatternsPathsSearch

Loading lesson path

Learn/Node.js/Module Basics
Node.js•Module Basics

Node.js NPM

Flash cards

Review the key moves

1/4
Core idea

What is the main idea behind Node.js NPM?

Lesson checks

Practice each idea before moving on

Short Mimo-style checks built from this lesson's code, terms, and sequence.

1Quick choice

Which statement best captures the main point of this lesson?

2Fill blank

Complete the missing token from the example code.

___ uc = require('upper-case');
3Order

Put the learning moves in the order that makes the concept easiest to apply.

The NPM program is installed on your computer when you install Node.
NPM is a package manager for Node.
Uninstalling a Package

What is NPM?

NPM is a package manager for Node.js packages, or modules if you like.

www.npmjs.com hosts thousands of free packages to download and use.

The NPM program is installed on your computer when you install Node.js

If you installed Node.js, NPM is already ready to run on your computer!

What is a Package?

A package in Node.js contains all the files you need for a module.

Modules are JavaScript libraries you can include in your project.

Using a Package

Once the package is installed, it is ready to use.

Include the "upper-case" package the same way you include any other module:

let uc = require('upper-case');

Create a Node.js file that will convert the output "Hello World!" into upper-case letters:

Example

let http = require('http');
let uc = require('upper-case');
http.createServer(function (req, res) {
 res.writeHead(200, {'Content-Type': 'text/html'});
 res.write(uc.upperCase("Hello World!"));
 res.end();
}).listen(8080);

Save the code above in a file called "demo_uppercase.js", and initiate the file:

Initiate demo_uppercase:

C:\Users\
Your Name
>node demo_uppercase.js

If you have followed the same steps on your computer, you will see the same result as the example: [http://localhost:8080

Global Packages

Packages can be installed globally, making them available as command-line tools anywhere on your system.

Global packages are typically used for CLI tools and utilities.

Install a package globally

npm install -g package-name

Example: Install the http-server package globally

npm install -g http-server

After installation, you can run the package from any directory:

http-server

Note

On some systems, you might need administrator/root privileges to install packages globally.

On Unix-like systems, use sudo before the command.

Updating Packages

To keep your packages up to date, you can update them using the following commands:

Update a specific package

npm update package-name

Update all packages in your project

npm update

Check for outdated packages

npm outdated

Tip

To update npm itself, run: npm install -g npm@latest

Uninstalling a Package

To remove a package that you no longer need, you can use the uninstall command:

Remove a package

npm uninstall package-name

Remove a global package

npm uninstall -g package-name

Remove a package and its dependencies

npm uninstall --save package-name

Note

The --save flag updates your package.json file to remove the dependency.

For older versions of npm, you might need to use --save-dev for development dependencies.

Previous

Node.js ES Modules

Next

Node.js package.json