Managing dependencies using Rollup and implementing a bundle strategy with Rollup

I am currently developing an NPM package and using rollup for bundling and npm publishing. I am seeking guidance on whether a dependency should be bundled by rollup or not. Below is my detailed analysis:

  1. Every dependency used in the 'src' folder should be added to either 'deps' or 'peerDeps'

  2. If you opt to include it only in 'peerDeps' and not 'deps', for local build purposes, it is advisable to add it to 'devDeps'. Otherwise, you may need third-party tools to install dependencies in 'peerDeps', or consider upgrading to npm@7 (which I have not done yet). I understand that this part might be controversial, but let's proceed as this topic is not solely about 'peerDeps'.

  3. Rollup will bundle every dependency used in 'src', unless excluded in the 'external' configuration. By default, it disregards whether the dependency is listed in 'deps', 'peerDeps', or 'devDeps'.

However, this approach seems unexpected. For any dependencies listed in 'deps', projects utilizing this package would install the dependency separately, allowing webpack to handle and process it without requiring rollup to include it in the bundle file. In that case, should we instruct rollup to treat all 3rd party dependencies as external? Perhaps by implementing this strategy:

// https://github.com/remix-run/react-router/blob/v5.2.0/packages/react-router/rollup.config.js
function isBareModuleId(id) {
  return (
    !id.startsWith(".") && !id.includes(path.join(process.cwd(), "modules"))
  );
}

Alternatively, we could utilize rollup-plugin-peer-deps-external:

peerDepsExternal({
  includeDependencies: true,  // FIXME: 'deprecated', no idea why?
}),

I am seeking insight into the best practice here and welcome any feedback or corrections to my thought process. Any advice is appreciated.

Thank you.

Answer №1

In my opinion, not only should 'peerDependencies' be marked as 'external' for rollup to bundle properly, but also 'dependencies'. One option is to utilize the rollup-plugin-peer-deps-external plugin with this configuration:

peerDepsExternal({
  includeDependencies: true
}),

Alternatively, you can follow the approach used by react-router:

function isBareModuleId(id) {
  return (
    !id.startsWith(".") && !id.includes(path.join(process.cwd(), "modules"))
  );
}

This method will set 'external' for all dependencies located in /node_modules/ directory.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Node.js encountered an error: Module "express" not found

I just created my first node.js application, but I'm having trouble finding the express library: C:\ChatServer\Server>node server.js module.js:340 throw err; ^ Error: Cannot find module 'express' at Function. ...

Is your Angular tree component not asynchronously loading child nodes?

When I am using npm install --save angular-tree-component, the child nodes are not loaded when I click on the parent node. Instead, it shows a "loading..." message. Here is the code I have written: options: ITreeOptions = { getChildren: this.getChil ...

During Docker build, the npm package was not found

I am a beginner when it comes to Docker, node, etc. and have spent several hours searching for a solution without success. My Dockerfile is straightforward: # Added to fix building on ARM64 - causing error on AWS FROM --platform=linux/amd64 python:3.7-alpi ...

Encountering problems during the installation of Ionic - Error code 'EPROTO'

After attempting to install Ionic on my system, I encountered the following error message: npm ERR! code EPROTO npm ERR! errno EPROTO npm ERR! request to https://registry.npmjs.org/async-limiter/-/async-limiter-1.0.0.tgz failed, reason: write EPROTO 0:er ...

What is the reason behind angular projects not adopting SemVer 2.0 for their pre-release tags?

Many Angular-related projects follow a pre-release versioning pattern that looks something like this: For example, angular-cli: 1.0.0-beta.22, 1.0.0-beta.22-1, 1.0.0-beta.24 And for @angular/material: 2.0.0-alpha.9 2.0.0-alpha.9-1, 2.0.0-alpha.9-2, 2. ...

What is the best way to manage optional peer dependency types while releasing a TypeScript package?

I'm trying to figure out the best way to handle optional peer dependencies when publishing a TypeScript package on npm. My package provides a function that can accept input from either one of two peer dependencies. How should I define these optional p ...

What is the reason for node-sass being rebuilt after upgrading from npm6 to npm7?

My application utilizes sass opposed to node-sass. Within my package-lock.json file, there is no mention of node-sass. I have been successfully using npm 6 for years without any issues. However, upon attempting to run npm install with npm 7, I encounter ...

What steps can be taken to confirm that the library or package I am responsible for maintaining remains compatible with previous versions?

As the caretaker of a library or package distributed through various package managers like NuGet, Maven, or NPM, I have unfortunately encountered instances where updates to my package inadvertently disrupted backwards compatibility. For instance, there wa ...

Inquiries regarding ReactJS, NodeJS, and the NPM platform

Here are the key points I have gathered: ReactJS is a frontend framework that compiles into html/js/css files for modern browsers to recognize, leading to direct requests from the browser to the web host server for these static files; NodeJS functions as ...

The installation process of NPM within a Docker container encounters difficulties, whereas it successfully executes on the host machine with

I am currently deploying node.js services to a corporate system using docker containers. The Dockerfiles for these services are quite simple, except for the fact that I need to set some proxy environment variables: FROM node:4.2.3 ADD . /src WORKDIR /sr ...

Steps to include a registry in npm installation for a scoped package

I am attempting to use npm install with a registry and scope. An example would be @test:registry=url After trying various methods, such as: npm install --registry @test url npm install --registry@test url I have not been successful in making it work. ...

package.json for small command-line tool

Currently, I am in the process of developing a command-line utility. However, when running npm install (without -g), the executable is not being linked. I anticipated that npm install would install the executable locally. The package.json file appears as ...

Check for the presence of an executable file in the system path using Node.js

Query How can I easily determine if a system executable is accessible on the system path using Node.js? For instance, if a user has Python installed at /usr/bin/python and the $PATH includes /usr/bin, how can I identify this in Node.js? Also, how can I id ...

There seems to be a problem with the bundle.js file caused by Uglify

I've just finished a project and now I'm ready to start building it. Utilizing a boilerplate project, I still find myself struggling to comprehend all the npm/webpack intricacies happening behind the scenes. Whenever I try to run "npm start", I k ...

Troubles arising while using ng serve in Angular 2

I'm currently facing an issue during the installation process of an existing Angular application. When trying to run the application using the ng serve command, I encounter the following error message: The "@angular/compiler-cli" package was not prope ...

Having trouble setting up Ionic on a mac: encountered an unexpected end of file issue

Having trouble with the installation of ionic. I successfully installed Cordova, but I keep encountering the error message below. Bankims-MacBook-Pro:Documents bankimdebnath$ sudo npm install -g cordova ionic tar.unpack untar error /tmp/npm-776-1c23f39a ...

What is the method to add a package with `yarn` without affecting other packages in the

I have created a set of packages with zero dependencies and stored them on my personal Gitlab. I am looking for a way to install these packages individually using the command yarn add GIT_URL_TO_PACKAGE --module-folder=my_module. The issue I am facing is t ...

Having trouble accessing WPA3 encrypted Wi-Fi networks with the node-wifi npm package

Struggling to connect to a WiFi network secured with WPA3 encryption using the 'node-wifi' package. Unfortunately, it seems that 'node-wifi' does not support WPA3 encryption. For more details about 'node-wifi', check out its n ...

Optimal techniques for deploying in the Azure DevOps NPM Registry for production and beta environments

Situation In my current project, I am looking to deploy an NPM package to a private Azure DevOps NPM Registry. This package has two versions - production and beta. However, while attempting to publish the package, I encountered an issue with Azure DevOps. ...

I can see the cookie in Postman, but it doesn't appear in the browser

I've encountered an issue with my Nodejs Project and pug template. When I log in using Postman, the Cookie is sent successfully. However, when I try to login using Chrome or any other browser, the cookie does not show up. const signToken = (id) => ...