【发布时间】:2021-12-21 14:31:49
【问题描述】:
我正在构建一个使用 web3 与以太坊智能合约交互的网页。每当我将 web3 导入页面时,我都会在 Netlify 构建中遇到错误:
9:54:39 PM: ModuleNotFoundError: Module not found: Error: Can't resolve 'electron' in '/opt/build/repo/node_modules/swarm-js/node_modules/got'
9:54:39 PM: > Build error occurred
9:54:39 PM: Error: > Build failed because of webpack errors
我可以通过将 web3 导入添加到页面来重现此错误而不是简单地得到它:
import web3 from '../ethereum/web3'
上面的代码只是从另一个文件中导出一个实例化的 web3 实例:
import Web3 from "web3";
let web3;
if (typeof window !== "undefined" && typeof window.ethereum !== "undefined") {
// We are in the browser and metamask is running.
window.ethereum.request({ method: "eth_requestAccounts" });
web3 = new Web3(window.ethereum);
} else {
// We are on the server *OR* the user is not running metamask
const provider = new Web3.providers.HttpProvider(
"https://mainnet.infura.io/v3/{INFURA_KEY}"
);
web3 = new Web3(provider);
}
export default web3;
我认为这可能与 web3 只能在浏览器中运行和 NextJS 执行一些 s-s-r 有关,但我不能完全解决这个问题。这是我的完整页面代码:
import Head from 'next/head'
import Header from '../components/Header'
import Menu from '../components/Menu'
import Footer from '../components/Footer'
import web3 from '../ethereum/web3'
const compiledFaucet = require('../ethereum/contracts/Faucet.json')
export default function Faucet() {
const getICHCHandler = () => {
console.log('dripping ICHC from faucet')
}
return (
I Can Has Cheezburger Token
{/* Global site tag (gtag.js) - Google Analytics */}
ICHC Token Faucet
)
}
export async function getServerSideProps(ctx) {
const faucetContract = new web3.eth.Contract(
compiledFaucet.abi,
"0x4099E633A607F6ED211e2c82565003d6F755e75e"
)
return { faucetContract }
}
感谢任何提示或建议。
-
你不能在 netlify 上使用 electron,那是一个后端库。
-
谢谢。 web3 使用电子吗?我没有明确地在项目中的任何地方导入它。
-
不,electron 是用来打包桌面应用的。
-
哦,好吧,这就是这个错误的奇怪之处,因为我没有在任何地方使用 Electron。
-
从 package.json 中取出并尝试在本地运行它(npm start)。如果它在本地运行,它应该建立在 netlify 上。