Error: The module 'fs' could not be located after running Rollup

Having encountered this issue, I understand that it has been a common question on the internet. I apologize for potentially duplicating the query. Despite trying various solutions found online, none have proven to be effective.

The Problem: The problem arises when importing a package created with Rollup.js for React using Typescript. The error occurs when attempting to import and use any component from the package:

Compiled with problems:
×
ERROR in ./node_modules/clone-stats/index.js 1:11-30
Module not found: Error: Can't resolve 'fs' in 'C:\Users\User\Documents\test\node_modules\clone-stats'

ERROR in ./node_modules/css/lib/stringify/source-map-support.js 9:9-22
Module not found: Error: Can't resolve 'fs' in 'C:\Users\User\Documents\test\node_modules\css\lib\stringify'
... (more errors listed)

Below is my package.json:

{ 
    // Package JSON data here
}

Below is my rollup.config.js:

{ 
    // Rollup configuration details here
}

Below is my config-overrides.js used with React-App-Rewired:

{
    // Webpack configuration overrides here
}

Below is my tsconfig.json:

{
    // TypeScript compiler configuration here
}

While everything functions correctly when utilizing components from my local directory, issues arise in two scenarios after packaging with Rollup:

  1. Testing the npm linked package and importing a component before running npm start
  2. Deploying the package to npm, installing it, and encountering errors upon importing a component before running npm start

In both situations, the same error persists. Any assistance or insights would be highly valued.

Answer №1

The issue has been resolved successfully. It was identified that there was a problem with the config-overrides.js file, where none of the configuration settings were being applied, especially the fallback options. By updating the export statement to export a function instead of an object literal and correctly applying the options to the config settings, the problems were fixed.

const MiniCssExtractPlugin = require("mini-css-extract-plugin");
module.exports = function override(config, env) {
    config.plugins = [new MiniCssExtractPlugin()];
    config.module = {
        rules: [
            {
                loader: "ts-loader",
                test: /\.(js|jsx|ts|tsx)$/,
                exclude: /node_modules/,
            },
            {
                test: /\.css$/i,
                use: [MiniCssExtractPlugin.loader, "css-loader"],
            },
            {
                test: /\.(woff|woff2|eot|ttf|otf)$/i,
                type: "asset/resource",
            },
        ],
    };
    config.resolve = {
        extensions: [".js", ".jsx", ".ts", ".tsx", ".css"],
        fallback: {
            assert: false,
            buffer: false,
            fs: false,
            tls: false,
            net: false,
            path: false,
            zlib: false,
            http: false,
            https: false,
            stream: false,
            crypto: false,
        },
    };
    config.externals = {
        react: {
            root: "React",
            commonjs2: "react",
            commonjs: "react",
            amd: "react",
        },
        "react-dom": {
            root: "ReactDOM",
            commonjs2: "react-dom",
            commonjs: "react-dom",
            amd: "react-dom",
        },
    };
    // config.node = {
    //  fs: "empty",
    // };
    config.target = "web";
    return config;
};

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

Access denied in zsh while executing command outside current directory

I have noticed a curious behavior when running commands in my project directory. The package.json file is located at: /home/user/dev/project/package.json When I navigate to this location using the command: cd /home/user/dev/project and then run the follo ...

What is the best method to display a tooltip for a disabled radio button within a set of radio buttons?

Is there a way to disable a specific radio button based on a condition and display a tooltip only for that disabled button? https://i.stack.imgur.com/niZK1.png import {Tooltip} from '@mui/material'; <Tooltip titl ...

Using React to update the state of an array of objects

I'm faced with a challenge in changing the properties of an object within an array of objects at a specific index using a function: const handleEdit= (index) =>{ if(itemList[index].edit==true){ const copied=[...itemList]; const item2 = {...ite ...

What steps can be taken to resolve the issue with installing an npm package if it fails at the installation script for [email protected]?

While working on setting up a npm package for a project, I encountered an error during the installation process. The ngx-loader is required for this project. prebuild-install WARN install No prebuilt binaries found (target=11.14.0 runtime=node arch=x64 pl ...

Tips for integrating the react-financial-charts library into your React and JavaScript project

While exploring the react-financial-charts library, I discovered that it is written in TypeScript (TS). Despite my lack of expertise in TypeScript, I am interested in using this library in my React+JS project due to its active contributions. However, I hav ...

Disabling the scrollbar within angular elements

Trying to remove the two scrollbars from this code, but so far unsuccessful. Attempted using overflow:hidden without success filet.component.html <mat-drawer-container class="example-container" autosize> <button type="button&qu ...

Encountered an issue while trying to install npm grunt-libsass

Attempting to set up grunt-libsass by running this command: npm install grunt-libsass --save-dev Resulting in the following error message: npm WARN engine <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="7710050219035a1b1e1504 ...

Publishing scoped packages to Nexus using npm is unsuccessful

I'm encountering an issue when trying to publish scoped npm packages to a private nexus repository. The package in question is named @spike/core, and here's the error message that I received: npm notice Publishing to https://nexus.pitsfs.work/re ...

Unable to install grunt-combine-media-queries on a system running Ubuntu version 14.04.1

I'm in need of assistance with installing grunt-combine-media-queries: npm install grunt-combine-media-queries --save npm WARN optional Skipping failed optional dependency /chokidar/fsevents: npm WARN notsup Not compatible with your operating system ...

What is the TypeScript syntax for indicating multiple generic types for a variable?

Currently working on transitioning one of my projects from JavaScript to TypeScript, however I've hit a roadblock when it comes to type annotation. I have an interface called Serializer and a class that merges these interfaces as shown below: interfa ...

Guidelines for transmitting form information to a web API using Angular

I am currently working on an Angular 6 project where I have a form and a table that retrieves data from a web API. I want to know if it's possible to send the form data to that web API. Here is the code snippet that I have so far: HTML Form: &l ...

Incorporate axios within getStaticProps while utilizing Next.js

I am working on a new project where I am utilizing axios to handle request data. However, I am facing an issue when using axios in the getStaticProps function which results in an error on my index.js page. Here is a snippet of my index.js code: import ...

You cannot use objects as valid children in React layout components

I encountered an issue with my layout: Unhandled Runtime Error Error: Objects are not valid as a React child (found: [object Promise]). If you meant to render a collection of children, use an array instead. The code in my DashboardLayout file (dashboardLa ...

Locating and casting array elements correctly with union types and generics: a guide

My type declarations are as follows: types.ts: type ItemKind = 'A' | 'B'; type ValidItem<TItemKind extends ItemKind = ItemKind> = { readonly type: TItemKind; readonly id: number; }; type EmptyItem<TItemKind extends ...

Encountered an error while running npm run dev on a Laravel Vue 3 project: opensslErrorStack: [ 'error:03000086:digital envelope routines::initialization error' ],

I am facing an issue in my Laravel 9 Vue 3 project. When I run php artisan serve and then npm run dev, I encounter the following error: opensslErrorStack: [ 'error:03000086:digital envelope routines::initialization error' ], library: 'di ...

Every time I restart VSCode, I have to re-run the .zsh_profile command in order for the NVM packages to work properly

Many others have encountered a similar issue, but I'm struggling to resolve it. Every time I open VSCode, I find myself needing to run these commands in the terminal for npx, npm, and nvm to work: export NVM_DIR="$HOME/.nvm" [ -s "$NVM_DIR/nvm.sh" ] ...

How to efficiently fetch Redux state through reducers with an action-based approach

Utilizing Redux actions to manage a list of contacts, I am facing an issue where I am unable to retrieve the actual state contact. Despite setting the contact in the action, all I receive is the contact set within the action itself. Can someone guide me on ...

Angular 17 isn't notifying child component of signal changes

In the statistics module, I have a signal that specifies the type of charts to display and this signal is updated through a radio button group. The signal: typeSignal = signal<string>('OIA') The radio buttons for setting the : <div clas ...

Trouble installing Angular: ENOENT Error causing issues

Currently in the process of setting up angular. I have been diligently following the provided installation instructions, but when executing npm install -g @angular/cli, I encounter the error shown in the screenshot below. Here are the versions of node and ...

What causes functions operating on mapped objects with computed keys to not correctly infer types?

If you are seeking a way to convert the keys of one object, represented as string literals, into slightly modified keys for another expected object in Typescript using template string literals, then I can help. In my version 4.9.5 implementation, I also ma ...