7️⃣

2.7 Npm and Npx

What is Npm?

Now that we know how to use node and a few of its uses, let’s explore an essential tool that every web developer needs to have in their tool kit; npm and npx. For starters, what is npm and npx?. To put it simply, npm or node package manager is a package manager for importing and exporting node packages, which are an essential component for every web development project. Why? It is simply because npm is what allows you to install many open source packages and software, such as ReactJS, VueJS, bootstrap, all of which are javascript frameworks, and many more packages. In fact, npm is the largest software registry in the world! You can view a comprehensive list of packages from https://www.npmjs.com.

Functions of Npm

There are lots of things, from downloading and using other’s packages, to sharing your own packages to the rest of the world, from making your (top secret projects) private and only shareable to people you choose (a.k.a for businesses), to managing multiple versions projects of code.
When it comes to the individual developer, there are lots of things you can do! The main things being
  1. Install && Manage dependencies:
    1. To install an npm package you would simple type (though this will change depending on the package, so make sure to consult their documentation):
      1. npm install <package>
    2. npm can install all the dependencies of a project / package (dependencies are any modules that are required by the npm package/project you install, to run: for example, in a random number generation game, the randomizer module is a dependency)
  1. You can use it to share your own projects and packages to the world with the following steps:
    1. Sign up at https://www.npmjs.com
    2. Check if you are logged in with the cmd: ‘npm whoami’ command.
      1. If not, then log in first using the cmd: ‘npm login’ command
    3. After that, simply navigate to your project, using the command cd <file path> and type into the terminal: npm publish
 
Alongside Npm comes a useful prepackaged tool called Npx, which is the second portion of this chapter.

What is Npx?

Npx is basically an Npm package runner that helps you install and run any sort of node based executable that you would normally install using Npm.

Functions of Npx

Npx is an extremely handy tool as it allows you to run dependencies and packages without installing them.
Here’s a quick overview of the advantages of this:
Usually, packages and dependencies are installed either locally, for that project alone, or globally, for all user projects. However, sometimes, people don’t want to install packages using either of those methods, or sometimes people might only want to experiment with new packages and not want to install them into a project (and if the package isn’t to their liking, uninstalling it).
This is where Npx shines. Instead of having to install those packages, Npx will simply run them without having to fully install it, thereby saving disk space, and allows you to experiment all you want.
⚠️
Npx scripts change according to the package, so to use it you need to consult the package

Conclusion

Overall, Node.js is an incredibly versatile javascript runtime environment. It is useful to become familiar with its functions, how to use its events and methods, and the extensible functionalities such as frameworks. Though nowadays it’s main use is in Npm and Npx, overall it is an incredible tool to have for any web developer.

Previous Section

6️⃣
2.6 Writing Node with Frameworks
 
⚖️
Copyright © 2021 Code 4 Tomorrow. All rights reserved. The code in this course is licensed under the MIT License. If you would like to use content from any of our courses, you must obtain our explicit written permission and provide credit. Please contact classes@code4tomorrow.org for inquiries.