One of the beautiful things about both Open Source and the ecosystems of modern programming languages is that there’s a good chance the code you’re about to write has already been written.
There are a plethora of packages out there for Node.js and between you and me, they are usually written by folks smarter than myself that have thought through of a bunch of stuff I wouldn’t have even dreamed of. Standing on the shoulders of giants, as they say.
For this article, I’m going to discuss using both
yarn. If you’re an avid reader of our reptilian friendly site, you have probably seen both commands mentioned in our other Node.js articles.
I’m not here to start a flame war about which is better, but I do favor
npm due to it’s ubiquity.
Fight me, loljk.
If you already have Node.js installed locally, you probably have
npm installed. If you’d prefer to follow along using
yarn, you can check out their installation instructions here.
Depending on your system, you could also consult with your friendly neighborhood package manager and install things that way.
Also, we’re going to be installing things globally as well as to a project as a dependency. You could very well use an existing project of yours, or you could create a dummy project out in your
/tmp directory as such:
$ mkdir /tmp/gator-project $ cd /tmp/gator-project $ npm init -y
This creates a
package.json file that we will be adding and removing packages from.
— Sorry to interrupt this program! 📺
If you're interested in learning Node in a comprehensive and structured way, I highly recommend you try Wes Bos' Learn Node course. Learning from a premium course like that is a serious investment in yourself.
Plus, this is an affiliate link, so if you purchase the course you help Alligator.io continue to exist at the same time! 🙏
Adding a Development Dependency to a Project
Not all dependencies are created equal, as some are only required while doing development. These dependencies, while important, can slow down production deployments since they take time to install and the code will never be touched.
Examples of development dependencies would be testing utilities like
jest. For those types of dependencies, we can install them as such, and have them added to the
devDependencies section of our
# With NPM $ npm install --save-dev mocha # Shorthand version $ npm i -D mocha # With Yarn $ yarn add --dev mocha # Shorthand version $ yarn add -D mocha
Adding a Production Dependency to a Project
Other dependencies are mission critical to the application and should always be installed regardless if it’s a development environment or not. We call these production dependencies and tend to includes packages like
Adding a production dependency to a project is just as easy as adding a development one, but it will be added to the
dependencies section of our
# With NPM $ npm install --save express # Shorthand version $ npm i -P express # With Yarn $ yarn add express
Installing a Package Globally
Sometimes you want to install a package outside of your current project, so it’s available to all of the projects on your system. These are installed globally and are great for packages that also include command-line utilities that you want to run alongside your other command-line utilities:
# With NPM $ npm install --global json # Shorthand version $ npm i -g json # With Yarn $ yarn global add json
Removing a Dependency From a Project
In every project’s life, there comes a time when a dependency that once seemed like a good idea, no longer serves any purpose. Don’t be too sad, deleting code is always a good thing (assuming you have proper test cover to ensure you didn’t break anything).
To remove either a development or production dependency from a project, we simply uninstall or remove it:
# With NPM $ npm uninstall jest # Shorthand version $ npm r jest # With Yarn $ yarn remove jest
This will remove things from
node_modules as well as drop the dependency from our
package.json. Depending on your version of either command, you may also see updates to your lock file.
Uninstalling a Package Globally
Removing a globally installed package is the same as removing one from a project, but we need to pass in the global argument as we did when installing it:
# With NPM $ npm uninstall --global json # Shorthand version $ npm r -g json # With Yarn $ yarn global remove json