🎓 A1Learner
🔍
0 of 50 lessons completed

Installing Packages

Installing a package adds its code to a project. npm fetches the package and saves its name in package.json.

Install a package

This command installs is-odd into the project. Run it in the project folder.

npm install is-odd

List the package

This command lists the packages in the project. The line shows is-odd version 3.0.1.

npm list

Example

const isOdd = require("is-odd");

console.log(isOdd(3));
console.log(isOdd(4));

Key points

  • Run npm install is-odd to install the is-odd package.
  • is-odd is a tiny package that tests whether a number is odd.
  • An odd number is a number that does not divide evenly by two, like 3.
  • npm saves the package name in package.json under dependencies.
  • Dependencies are the packages a project needs to run.
  • npm puts the package code in a folder named node_modules.
  • Use require to bring an installed package into code.

In this example, require brings the is-odd package into the code. The first print asks whether 3 is odd and shows true. The second print asks whether 4 is odd and shows false.

Note

Install once per project, then require the package in any file of that project.

Try it Yourself

Edit the code, then press Run.

Running is simulated

Exercise

Which command installs the is-odd package?