Learn how to add and remove packages globally and as project dependencies using npm and Yarn.
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.
For those new to the party,
yarn are package managers for Node.js. They both leverage the package.json file for your projects and function quite similarly.
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.
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
[mocha](https://morioh.com/p/86d418cf65a6 "") or
[jest](https://morioh.com/p/0bacfcdc8dd2 ""). 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
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
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
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.
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
A Guide to Hire Node.js Developers who can help you create fast and efficient web applications. Also, know how much does it cost to hire Node.js Developers.
We are providing robust Node.JS Development Services with expert Node.js Developers. Get affordable Node.JS Web Development services from Skenix Infotech.
The practical implications of having Streams in Node.js are vast. Nodejs Streams are a great way to handle data chunks and uncomplicate development.
You may already be aware that Raygun uses Node.JS for our API nodes that receive your precious crash reporting data (we also do node.js crash reporting if you’re interested). We’ve peaked in the past at more than 110,000 requests per second coming...
🔥 Node.js Certification Training: https://www.edureka.co/nodejs-certification-training This Edureka video on 'Node.js Docker Tutorial' will help you in learn...