DEV Community

loading...

NodeJS: How to Get External Packages / npm

miku86 profile image miku86 Updated on ・2 min read

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

npm Homepage
npm install Docs
npm on Wikipedia


Questions

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

Discussion (3)

pic
Editor guide
Collapse
aminnairi profile image
Amin

Great article, thanks!

Did you know that you could install GitHub repositories as packages as well?

$ npm install --save-dev i3bar/core
$ # installs from https://github.com/i3bar/core

How cool is that?

Collapse
miku86 profile image
miku86 Author

Hey Amin,

thanks for your tip, I added a link for the npm install docs,
so everyone can have a look at all the npm install possibilities.

Do you see any advantages in installing from github?

Collapse
aminnairi profile image
Amin

When crafting an NPM package and you want to test that you did not forget anything by simulating an installation. This is quite a handy feature. Plus, there will be, I'm sure, more third party package hosting in a near future that will be compatible with the NPM CLI.

Another advantage is that you are not forced to publish your package on the NPM package registry if you don't want to but still be able to open-source the package. Sometimes it can be quite tedious to think about checking and publishing your next versions of your packages when you could just instead merge requests and publish a git tag.