繁体   English   中英

require('fs-extra') 未加载:抛出错误“无法找到模块 fs-extra”

[英]require('fs-extra') not loading: throwing error "Cant find module fs-extra"

尝试将一些已安装的 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”

npm ls fs-extra来检查你是否正确安装了它。 然后尝试npm install fs-extra

require('fs-extra') 只能在服务器端 javascript (nodejs) 中工作。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM