【问题标题】:require('fs-extra') not loading: throwing error "Cant find module fs-extra"需要('fs-extra')未加载:抛出错误“找不到模块 fs-extra”
【发布时间】:2019-06-17 18:11:51
【问题描述】:

尝试将一些已安装的 npm 包(如 fs-extra)用于 Truffle 提供的以下 js 文件中。但它显示“找不到模块“fs-extra”。

1) 尝试使用 require() 方法导入本地 js 文件,但也失败了。

2) 尝试使用 node 运行单独的 js 文件,效果很好。

3) 当我尝试在 APP 对象中声明的函数中使用 require("fs-extra") 时出现问题。

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) {  //<VK>Satish to add his contract file here
      // Get the necessary contract artifact file and instantiate it with truffle-contract
      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');  // Failing to find module
  }

}


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

预期:应该能够调用 fs-extra 包中的方法

实际:找不到模块“fs-extra”

【问题讨论】:

  • 嗯,你安装fs-extra了吗?
  • 当然可以。已经安装了它,当我在新的 JS 文件中添加 require("fs-extra") 时。它工作得很好。但是当我在创建用户函数中使用它时它不起作用。

标签: javascript typescript requirejs truffle


【解决方案1】:

npm ls fs-extra 检查您是否已正确安装。那就试试npm install fs-extra

【讨论】:

  • 是的,它已安装,但不确定 Truffle 是否在 app.js 中正确选择它。
【解决方案2】:

require('fs-extra') 仅适用于服务器端 javascript (nodejs)。

如果您的代码在浏览器上运行,则 require 将不起作用

【讨论】:

    猜你喜欢
    • 2021-08-09
    • 1970-01-01
    • 1970-01-01
    • 2021-04-11
    • 2017-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多