NodeJS: How to Get External Packages / npm

Intro

So we installed NodeJS on our machine.

Because there is already a lot of code out there in the internet, we can use other peoples' code so we do not have to reinvent the wheel to solve problems, that are already solved.

In order to easily get solutions from other people, we can use a tool called npm.

This will be a short intro to npm with the most used commands. If you want to learn some advanced concepts, read the npm documentation.


What is npm?

  • npm is a package manager for JavaScript packages
  • npm is the world’s largest software registry
  • npm comes with the NodeJS installer, so if you already installed NodeJS, you already have npm on your machine
  • you download packages with the command line interface
  • you get information about packages through the npm website

How To Install npm?

If you already installed NodeJS, you already have npm on your machine.

To check, if npm is installed: npm --version


How to Get External Packages?

Most of the time, you want to use packages project-wise.

  1. Initialize a npm project in an existing folder: npm init. This will create a package.json file, where all your settings will be saved.
  2. Answer all questions like package name, version etc. You can read about the correct answers here
  3. Go to npm and search for a package, e.g. "text color".
  4. You will get a list of matching packages, click on the link to the package and read the documentation, e.g. the package "chalk".
  5. Most of the time, the documentation will give you an appropriate command to install the package, e.g. npm install chalk. If you want to see all the possibilities how to npm install a package, read the npm install docs.
  6. The package gets downloaded, you have to wait for some time.
  7. Read the package documentation to learn how to use the package.

Additional Commands

If you want to learn additional commands, e.g. uninstall a package, read the documentation


Further Reading


Questions

  • What beginner's problem would you have liked to have solved here as well?
  • Are the instructions clear, understandable and comprehensible?