Managing Node.js packages
One of the beauties of open source and the ecosystem of modern programming languages is that the code you want to write has probably already been written.
There are a ton of packages for Node.js and between you and me, these packages are usually written by people smarter than me and have thought of things I never even dreamed of. Standing on the shoulders of giants, as they say.
Getting started
In this article, I'm going to talk about using npm and yarn. If you're a regular reader of our crawler-friendly site, you've probably seen both commands in our other Node.js articles.
For those who are new to this, npm and yarn are Node.js package managers. Both are available from the file package.json Your projects use and operate in a similar way.
If you have Node.js installed locally, you probably have npm installed as well. If you prefer to go with yarn, you can check out its installation instructions here.
Depending on your system, you can also consult your local package manager and install things through that.
Also, we are going to install things both globally and as project dependencies. You can use an existing project of yours or you can create a sample project in the directory /tmp Create:
$ mkdir /tmp/gator-project
$ cd /tmp/gator-project
$ npm init -yThis command is a file package.json Creates a directory to which we will add and remove packages.
Adding development dependencies to the project
Not all dependencies are created equal, as some are only needed during development. These dependencies, while important, can slow down production deployments because they take time to install and are never used.
Examples of development dependencies are testing tools like mocha or jest. For these types of dependencies, we can install them as follows and refer to the section devDependencies File package.json Let's add:
With NPM
$ npm install --save-dev mocha
# Shorthand version
$ npm i -D mocha
# With Yarn
$ yarn add --dev mocha
# Shorthand version
$ yarn add -D mochaAdding a production dependency to the project
Other dependencies are critical to the application's performance and should always be installed regardless of whether you are running a development or production environment. These dependencies are called production dependencies and usually include packages like express or react.
Adding a production dependency to a project is just as simple as adding a development dependency, but with a different section. dependencies File package.json Added:
# With NPM
$ npm install --save express
# Shorthand version
$ npm i -P express
# With Yarn
$ yarn add expressInstalling a package globally
Sometimes you want to install a package outside of your current project so that it is available to all projects on your system. These packages are installed globally and are suitable for packages that contain command-line tools that you want to run alongside other tools:
# With NPM
$ npm install --global json
# Shorthand version
$ npm i -g json
# With Yarn
$ yarn global add jsonRemoving a dependency from the project
There comes a time in the life of any project when a dependency that once seemed like a good idea no longer makes sense. Don't worry, removing code is always a good thing (as long as you have proper test coverage to make sure nothing breaks).
To remove a development or production dependency from the project, we simply uninstall or remove it:
# With NPM
$ npm uninstall jest
# Shorthand version
$ npm r jest
# With Yarn
$ yarn remove jestRemove a package globally
Removing a globally installed package is similar to removing a package from a project, but you must use the option like installing it. --global Let's use:
# With NPM
$ npm uninstall --global json
# Shorthand version
$ npm r -g json
# With Yarn
$ yarn global remove jsonResult
Package management tools like npm and yarn have made developers' lives much easier. Whether you want to easily manage your project's dependencies or install the tools you need globally, these tools offer quick and efficient ways to do so.
Remember to never remove or update your dependencies without considering the impacts. Proper test coverage will help you ensure that nothing breaks in the project process.









