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

Tips for ensuring that the DOM is fully rendered before executing any code

Before proceeding to the next functions, it is necessary to wait for the DOM to finish rendering. The flow or lifecycle of this process is outlined below: Adding an item to the Array: this.someFormArray.push((this.createForm({ name: 'Name& ...

Unable to deploy Bower on Azure successfully

I have chosen Azure to deploy my Node.js web application, and I've connected Github as the deployment repository. Following Jay Harris's instructions on this webpage, I have set up deploy.sh for deployment. My web application relies on bower, bu ...

The video rendered with fluent-ffmpeg appears to have an elongated image

I am working on combining an mp3 audio file with a jpg image file to create a new mp4 video. Although I have a functional fluent-ffmpeg command that accomplishes this task, there is an issue where the image gets stretched in the final output video, especia ...

The error message "TypeError: Unable to access the 'getFullWidth' property of an undefined value when using TSLint and TypeScript" was

I have been using Dan Wahlin's tutorials and online examples to set up Gulp and Typescript, but I am facing an issue with the tslint() function. The problem occurs in my code as follows: node_modules\tslint\lib\language\walker&bso ...

Who is the intended audience for the "engines" field in an npm package - consumers or developers?

As the creator of an npm library, I have included the current LTS versions of Node.js and npm in the package manifest under the engines field. This ensures that all contributors use the same versions I utilized for development: Node.js <a href="/cdn-cgi ...

Is it possible to pass a prop from a parent container to children without knowing their identities?

I am currently working on a collapsible container component that can be reused for different sections of a form to display fields or a summary. Each container would include a Form object passed as a child. Here is the basic structure of my code: function C ...

Utilizing PrimeNG menu items to bind commands to a base class function

I'm struggling to connect a parent class function with my Angular 2 PrimeNG menu options. HTML <p-menu #menu popup="popup" [model]="exportItems"></p-menu> <button type="button" class="fa fa-download" title="Export As" (click)="menu.to ...

Exploring the Differences between Angular's Http Module and the Fetch API

While I grasp the process Angular uses for HTTP requests, I find myself leaning towards utilizing the Fetch API instead. It eliminates the need to subscribe and unsubscribe just for a single request, making it more straightforward. When I integrated it int ...

Can you guide me on incorporating a date input with ngModel?

I have a date input field set up as follows: <input [(ngModel)]="value" type="text" class="form-control"> Can someone explain how I can display and submit the value correctly? The user's input should be formatted as dd/MM/yyyy, while t ...

Inefficiency in POST method prevents data transmission to MongoDB

I've developed a MERN application and now I'm testing the backend using the REST client vscode extension. This is how it looks: `POST http://localhost:4000/signup Content-Type: application/json { "email": "<a href="/cdn-cgi ...

Error: The page "..." contains an invalid "default" export. The type "..." is not recognized in Next.js

Currently, I have a functional component set up for the Signup page. My goal is to define props within this component so that I can pass the necessary values to it from another component. This is my current approach: export default function SignupPage({mod ...

Can you tell me the significance of the constant variable isType = <T>(type: string) => (obj: unknown): obj is T => toString.call(obj) === `[object ${type}]`?

const isType = <T>(type: string) => (obj: unknown): obj is T => toString.call(obj) === `[object ${type}]` This function is all about determining types, but the arrows used in the syntax are confusing to me. I'm not sure what each arrow s ...

Leverage the package.json script without relying on any yarn/npm commands

Can scripts commands be executed within the package.json file without needing to include yarn or npm? For example: "scripts": { "get": "node index.js" } Currently, in order to run this script I have to use yarn get [ar ...

nvm has successfully installed npm, but unfortunately, IntelliJ is not yet aware of its presence

I utilized NVM to install NPM. Oddly, when I attempt to use npm in the Intellij terminal, it claims that NPM is not installed. However, if I utilize the Ubuntu terminal, it functions properly. Here are the steps I have taken: I have attempted to set my ...

ts-jest should replace the character '@' with the '/src' folder in the map configuration

I have set up a node project using TypeScript and configured various paths in my tsconfig.json file, such as: "paths": { /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl' ...

Interface key error caused by the TypeScript template literal

Version 4.4.3 of Typescript Demo Playground Example -- interface IDocument { [added_: `added_${string}`]: number[] | undefined; } const id = 'id'; const document: IDocument = { [`added_${id}`]: [1970] } My Attempts: I made sure that id in ...

Encountering issues with compiling files in react app using webpack, failing to compile as anticipated

When it comes to compiling, I prefer using webpack with TypeScript files. In my webpack.config.js file: module.exports = async (env, options) => { const dev = options.mode === "development"; const config = { //Webpack configuration pr ...

An error was encountered while attempting to proxy the request [HPM]

After cloning a GitHub repository, I ran npm i in both the root directory and client directories. I then created a development configuration file. However, when attempting to run npm run dev, nodemon consistently crashes with a warning about compiled cod ...

The error "Property 'push' does not exist on type '() => void'" occurs with Angular2 and Typescript arrays

What is the method to initialize an empty array in TypeScript? array: any[]; //To add an item to the array when there is a change updateArray(){ this.array.push('item'); } Error TS2339: Property 'push' does not exist on type &a ...

Mapping the preselected values of multiple input fields into an array in Angular 6: A step-by-step guide

When submitting a form with input fields, I need to capture the selected values into an array format like {"userid":1,"newstatus":[1],"mygroup":[1,2,3]}. I attempted using ngmodel but encountered issues. Below is the code snippet: home.component.html & ...