尝试使用一些已安装的npm包,如fs-extra到Truffle.But给出的下面的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包中的方法
Actual :找不到模块"fs-extra“
发布于 2019-06-18 04:34:57
npm ls fs-extra检查您是否正确安装了它。然后尝试npm install fs-extra。
发布于 2020-12-29 19:12:47
require('fs-extra')只能在服务器端javascript (nodejs)中工作。
如果您代码在浏览器上运行,则所需的代码将无法工作
https://stackoverflow.com/questions/56636737
复制相似问题