Unable to load `fs-extra` using `require()`: encountering error message "Module fs-extra not found"

Having trouble utilizing installed npm packages like fs-extra in the js files provided by Truffle. The error message "can't find module 'fs-extra'" keeps popping up.

1) Attempted to import local js files using the require() method, but it was unsuccessful.

2) Running separate js files using node works without any issues.

3) The problem arises when trying to use require("fs-extra") inside a function declared within the APP object.

App = {
  web3Provider: null,
  contracts: {},
  init: async function () {
    return await App.initWeb3();
  },
  initWeb3: async function () {
    // Modern dapp browsers...
    if (window.ethereum) {
      App.web3Provider = window.ethereum;
      try {
        // Request account access
        await window.ethereum.enable();
      } catch (error) {
        // User denied account access...
        console.error("User denied account access")
      }
    }
    // Legacy dapp browsers...
    else if (window.web3) {
      App.web3Provider = window.web3.currentProvider;
    }
    // If no injected web3 instance is detected, fall back to Ganache
    else {
      App.web3Provider = new Web3.providers.HttpProvider('http://0.0.0.0:9283');
    }
    web3 = new Web3(App.web3Provider);

    return App.initContract();
  },

  initContract: function () {

    $.getJSON('UserCreation.json', function (data) {
      var CMArtifact = data;
      App.contracts.UserCreation = TruffleContract(CMArtifact);
      App.contracts.UserCreation.setProvider(App.web3Provider);

    });
    return App.bindEvents();
  },

  createUser: function (event) {
    event.preventDefault();
    var username = $("#sign-up-username").val();
    var title = $("#sign-up-title").val();
    var intro = $("#sign-up-intro").val();
   const utility=require('fs-extra');  // Unable to locate module
  }

}


$(function () {
  console.log("Initializing farmer")
  $(window).load(function () {
    App.init();
  });
});

Expected: Should be able to call methods from the fs-extra package

Actual : Error persists - can't find module "fs-extra"

Answer №1

Verify that you have correctly installed fs-extra by running npm ls fs-extra. If it is not installed properly, try running npm install fs-extra.

Answer №2

The 'fs-extra' module is specifically designed for use in server-side JavaScript (Node.js).

Keep in mind that if your code is running in a browser environment, the 'require' function will not be compatible.

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

What is the process for incorporating a standalone custom directive into a non-standalone component in Angular?

Implementing a custom directive in a non-standalone component I have developed a custom structural directive and I would like to use it across multiple components. Everything functions as expected when it is not standalone, but encountering an error when ...

How to retrieve items from a DynamoDB table where the fields array is not empty using JavaScript FilterExpression

Can anyone help me with filtering this sample dynamodb table to only return items where either the "old" or "new" array is not empty? In the example below, it should retrieve the items with id "2" and "3". Table: field name = item [ { id: "1", p ...

What is the best approach for creating a custom hook that is highly reusable in my code?

I have created a custom hook for fetching requests, but I am having trouble with defining the response type to make it reusable. import { useState, useEffect } from 'react' export function useFetch (url: string, options?: object) { const [re ...

Unlock the full potential of your database with ExpressJs and MySQL. Discover a superior option to Knex Interceptor for securing your data with

I have a dilemma with managing sensitive data in my MySQL tables. I need to encrypt these fields before insertion and decrypt them when retrieving the data. Here are three different approaches that I have considered: One option is to implement a DATABAS ...

The menu is about to receive some custom styling

I have come across a webpage where I need to make some modifications, but I am unable to locate the script responsible for applying the inline style. This issue only seems to be occurring on the mobile version with the dropdown menu. <div class="is-dri ...

"Populate a textarea with the selected options from a multiple select dropdown

I am currently working on a form that contains several select boxes, some of which allow for multiple selections. I have successfully managed to pass the text value from each select box to a textarea. However, I am facing an issue where I am only able to p ...

Angular2 - Creating PDF documents from HTML content with jspdf

For my current project, I am in need of generating a PDF of the page that the user is currently viewing. To accomplish this task, I have decided to utilize jspdf. Since I have HTML content that needs to be converted into a PDF format, I will make use of th ...

Retrieve the subdomain from within the passport FacebookTokenStrategy and GoogleStrategy

In the process of developing a node.js application with wildcard subdomains, I am creating separate parts of the app for each subdomain that will have distinct user authentication based on the subdomain. For instance, envisioning my app having subdomains ...

Guide on activating full screen mode in Firefox without user interaction

Is there a way to make a webpage automatically go into fullscreen mode upon loading? I attempted the following code: <html> <head> <script type="text/javascript> webkitEnterFullscreen(); </script> </head> <body onload="mo ...

The TipTap Editor does not properly register spaces

In our project, we are utilizing the TipTap rich text editor. However, we are encountering an issue where spaces are not being recognized correctly - a space is only created after every 2 clicks. Our framework of choice is Vue.JS. import { Editor, EditorC ...

Apologies, we are experiencing a server error. TypeError: Unable to access the 'map' property of an undefined

I'm struggling to grasp the concept of fetching external data using server-side rendering with the getServerSideProps method. In particular, I am facing difficulties with the search functionality and how to handle the returned data. As someone who is ...

Troubleshooting: AngularJS filter is not functioning properly in combination with jQuery

I have a collection of items stored in the variable $scope. I have connected these items to jQuery events and added an input field for filtering. Everything is functioning properly. However, when I enter text into the input field, the filtered out items r ...

Is it necessary to compile the React JavaScript project before uploading it to npm?

Hey there, I'm currently in the process of publishing a React JavaScript component on npm. I have a question regarding whether or not I need to build the project and deploy the build folder. Your input would be greatly appreciated. Thanks! ...

Send a REST Request and retrieve a Response without the need to refresh the page

I need to show the result of a REST request without having to refresh the page. Using RAVE for this purpose. Below is the URL for the POST request: JSON data: { "recipientaccount": "0690000034", "destbankcode": "044", "PBFPubKey": "FLWPUBK-4e ...

Struggling to align images side by side using HTML and CSS along with figcaption and buttons

Adding buttons below images on my webpage is proving to be a bit challenging. I tried using the figcaption tag, but it resulted in the images stacking on top of each other instead of side by side. **My HTML:** <section class="mostpurch"> ...

SonarQube seems to be overlooking TypeScript files with the .ts extension

I'm currently setting up SonarQube on Jenkins for an Angular 2 project, but I'm facing an issue where Sonar isn't recognizing the TypeScript files with the .ts extension. Here's the current configuration: sonar.projectKey=App Name- An ...

AngularJS is receiving an array of null inputs

I have a form where data from my DB is displayed through WebApi. One of the controls will contain an array of values. There is a scenario where the user can edit and save it using the PUT function. Controller : $scope.Put= function () { $scope.i ...

Error: Attempts to access the 'avatar' property of a null value result in a TypeError

I've been attempting to showcase both an avatar and the user name, but I keep encountering this error. Despite trying to declare a user variable to troubleshoot, it's not resolving the issue. Error: Cannot read property 'avatar' of n ...

What is the significance of having multiple route parameters in Express?

The RESTful API provided by cex.io offers a route that can return pairs of all currencies with a variable amount of parameters. In express, how can we achieve similar functionality? Consider the following pseudo code example... app.get('/pairs/:arg ...

What are some techniques for animating SVG images?

Looking to bring some life to an SVG using the jQuery "animate" function. The plan is to incorporate rotation or scaling effects. My initial attempt with this simple code hasn't yielded the desired results: $("#svg").animate({ transform: "sc ...