install all optional packages from binary
What Are NPM's Optional Dependencies and When Should We Use Them?
Speed up your builds and reduce used deejay space
Many JavaScript projects rely on a lot of tertiary-party packages. Some of them are required (east.g. a component library like Bootstrap), while others are only useful for developing (e.g. tooling like ESLint). Many packages depend on other libraries that are ofttimes referenced in famous memes well-nigh the node_modules
folder being style also big sometimes. After all, some projects include dependencies that are barely used (eastward.g. for ane custom NPM script), but they are notwithstanding installed every time.
Not but can bigger pro j ects eat a lot of deejay space, but as a consequence, the installation times tin be longer. Some popular packages like Puppeteer or Cypress may download large binaries in lodge to piece of work. As long equally you are not resetting your evolution environs every day and you have plenty disk space, this may non exist a trouble at all.
However, on a continuous integration tool like GitLab CI or GitHub Actions, this could exist a more pressing problem:
- If you lot have many developers and automations (e.g. Dependabot for creating pull requests for dependency updates) pushing code, you could exceed your usage quotas. Many CI/CD tools have usage quotas (e.g. 2,000 minutes per month), and if you exceed them, you need to pay for additional usage or wait until the next month.
- Installing the dependencies of a projection is typically the prerequisite for doing a bunch of tasks like building, linting, and testing your application. The longer information technology takes, the longer you need to wait to find out almost potential issues in your code.
To install or update a dependency, yous utilise the npm install
command, while you lot ideally employ npm ci
(available since NPM v6) to install all dependencies of your project. I have previously written nigh the difference betwixt npm ci
and npm install
.
What Kinds of Dependencies Be?
JavaScript projects have a package.json
where you take divers the packages used in your project. There are different kinds of dependencies:
-
dependencies
are e'er installed. Typically, something would break if a dependency was missing. Examples: UI libraries similar Bootstrap or frameworks like React. NPM recommends to non put test harnesses or transpilers in yourdependencies
object. -
devDependencies
are typically packages that are used in development only non mandatory for the actual functionality of your project. Examples: linters like ESLint and testing frameworks similar Jest. -
peerDependencies
are packages that are typically not installed automatically. In some cases, you desire to express the compatibility of your package with some other package without including it yourself. NPM will warn you lot when you have missingpeerDependencies
. Example: the react-router package depends on React being installed. -
bundledDependencies
are packages that will be bundled when publishing the package. - Finally,
optionalDependencies
are dependencies that don't necessarily need to be installed. If a dependency can be used, but you would like npm to proceed if it cannot be constitute or fails to install, so yous may put it in theoptionalDependencies
object.
Let's assume you accept a web application, continuous integration and deployment, and a suite of end-to-terminate tests using Cypress. Have a expect at the following CI pipeline. Information technology consists of four different parallel jobs (edifice, linting, unit testing, and cease-to-end testing) that all demand to install the projection dependencies equally the first footstep. Yet, simply one of them really needs Cypress, yet we're e'er installing all of the packages for each job.
Since Cypress is not required to run the awarding, we tin install it as a dev dependency. This is already a good stride, but we can take information technology fifty-fifty further. By installing Cypress as an optional dependency, we declare information technology to be an optional dependency that does not necessarily need to exist installed.
Notation that it is still your programme'due south responsibleness to handle the lack of optional dependencies. Therefore, y'all demand to evaluate whether a dependency is really optional and you might need to check at runtime whether an optional dependency is actually installed or non. Y'all should also spread this knowledge to your coworkers. Otherwise, less experienced developers might not understand why certain dependencies are missing when they ready the projection or a new dependency is introduced.
How to Use Optional Dependencies to Speed Up Installation
- Execute
npm install someDependency --save-optional
to install a packet as an optional dependency. The installed package will exist put intooptionalDependencies
. - When you lot want to avert installing optional dependencies, y'all can execute
npm ci --no-optional
(eastward.m. on CI tools like GitLab CI). - Use
npm ci
to install all dependencies, including optional dependencies (due east.g. on your development environment).
I measured the difference on my local machine (an iMac 4K) as well as on GitLab CI. The project is a medium-sized Angular projection using Cypress for finish-to-end tests. The merely thing I did is change Cypress from being a dev dependency to an optional dependency. You can see that both the time and the amount of packages installed have decreased considerably when executing npm ci --no-optional
compared to when using npm ci
:
- Earlier: Added one,637 packages in ~67-72 seconds using
npm ci
as well as around ~550 MB cache that is stored outside of any projection (on a Mac:/Users/myusername/Library/Caches/Cypress
). - Afterward: Added 1,514 packages in ~27-38 seconds using
npm ci --no-optional
without any cache created by Cypress since this packet was not installed at all.
Conclusion
Cheers for reading this short article. As you can meet, NPM allows usa to specify how a dependency is existence used in Node projects. Installing a dependency every bit optional can be a skillful choice to speed upwards the installation process of Node projects. But this is not a silver bullet either since not many dependencies can really be optional.
How are you using optional dependencies? Allow me know in the comments.
Source: https://betterprogramming.pub/what-are-npms-optional-dependencies-and-when-should-we-use-them-796a6a964e73
Posted by: fowleraccultoo85.blogspot.com
0 Response to "install all optional packages from binary"
Post a Comment