NPM: Package Management Made Easier




Let’s talk Package Management!!

Imagine a world where you can run a single command line and have everything installed and ready to go. No need to download and set up libraries manually one by one. Sounds simple and resourceful, right?

Well, I’m here to tell you that is exactly what npm does for you!
But before we dive into how npm actually works, let’s go over some basic information about what it is.

What is ‘npm’?
NPM is a Node.js package manager. Some people say that it stands for “Node Package Manager,” but according to the creators, it’s just npm — plain and simple.

Created in 2010 by Isaac Z. Schlueter, npm is currently the largest software registry in the world, making thousands of packages available for easy reuse.

What does ‘npm’ do?

As you know, JavaScript is what we use to power web browsers, just like this one. All the user interactivity, like clicking buttons and searching in a search bar, is powered by JavaScript. Node.js allows us to run JavaScript on our computers, outside of the browser. And when you install Node.js, one of the tools you automatically get is the npm package manager.

Here are a few concepts npm helps you with:

  • Installing packages created by other developers
  • Manage project dependencies
  • Automate scripts for your project

Let’s discuss an important concept – Dependencies

NPM helps you install and manage libraries that developers have already created. You need these libraries for almost any project you create or repository you fork.

Some packages that you might be familiar with are:

  • Lodash/Underscore – provides helpful utility functions like:
    _.map, _.filter, and _.flatten
  • Mocha – provides the structure for writing/running tests
  • Chai – allows you to use certain statements that check if your code functions as expected.

For example,

it('should be able to add rooms to the DOM', () => {
      RoomsView.renderRoom('superLobby');
      expect($('#rooms select').children().length).to.equal(1);
    });
Enter fullscreen mode

Exit fullscreen mode

And when these packages are installed, they are kept in a file called
package.json.

The package.json file serves as a blueprint of what you need to run the project.
Here is an example of how some dependencies look in that file.

  "dependencies": {
    "underscore": "^1.13.7"
  },
  "devDependencies": {
    "@compodoc/live-server": "^1.2.3",
    "chai": "^4.5.0",
    "eslint": "^8.57.0",
    "jquery": "^3.7.1",
    "mocha": "^10.7.3",
    "nock": "^13.5.5",
    "sinon": "^19.0.2"
  }
Enter fullscreen mode

Exit fullscreen mode

But what are dependencies?

Dependencies are libraries that your code depends on to function correctly, and these libraries have already been created for you to reuse across multiple projects.

This saves you a lot of time when working on different projects because you don’t have to build them yourself — unless you want a very long challenge!

This will basically tell the developer, “Hey, this repo/project depends on these libraries!”

Installing Dependencies

Now all you have to do is type the magic words into your terminal.
npm install

That command tells ‘npm’ to look at that package.json file and download the listed dependencies. After downloading the dependencies, ‘npm’ creates a folder to store them in called node_modules.

The picture above shows just a snippet of what that folder looks like. Since some packages have their own dependencies, and those dependencies can have more dependencies inside them, this folder can become very large.

Now you see what I mean by “it saves you a lot of time”!

Updating Dependencies

Something important to remember with dependencies is that they also receive updates regularly. Which means you should always check for new versions to keep your project running and stable. Just make sure to read the release notes before updating any packages, so that nothing conflicts with the project you are working on.

You can check for outdated packages with this command.
npm outdated
And then update them with this command.
npm update

Scripts – Automating Commands

NPM isn’t only good for installing and managing packages — it also lets you create shortcuts for commands in that same package.json file.

For example,

  "scripts": {
    "start": "live-server --port=8081 --open=./ --watch=client",
    "test": "live-server --port=8080 --open=./TestRunner.html --watch=client"
  }
Enter fullscreen mode

Exit fullscreen mode

Instead of typing the full command to open the server every time, if you run npm run start, it should execute that exact line and start the live server for me. It’s super easy and convenient!

DevDependencies vs Dependencies

There is such a thing as DevDependencies, but they differ from the dependencies you just learned about.

  • Dependencies are required for your app to function
  • DevDependencies are packages used only when you are developing

DevDependencies are mainly used for testing, but they are not required for your project to function.

Conclusion
NPM adds a lot of functionality to your projects. It gives you access to thousands of libraries in such a short amount of time. So use that extra time to create something great! Thank you to all the teams that made these resources available for us to use!

Extra Resources:
https://nodejs.org/en/about
https://docs.npmjs.com/about-npm



Source link

Leave a Reply

Your email address will not be published. Required fields are marked *