我在ReactJS中有一个小脚本:
import './App.css';
import { useState, useEffect } from 'react';
//Import de la librairie ethers (comme web3.js)
import { ethers } from 'ethers';
const { MerkleTree } = require('merkletreejs')
const keccak256 = require('keccak256');
const tokens = require('./tokens.json')
function App() {
function isWhitelisted() {
let tab = [];
tokens.map(token => {
tab.push(token.address);
})
const leaves = tab.map(v => keccak256(v));
console.log(leaves);
const tree = new MerkleTree(leaves, keccak256, { sort: true });
const leaf = keccak256("0x7D8a1f6A5efc16E88FeF87E7745EAc2F5Cbc88D7")
const proof = tree.getHexProof(leaf);
}
return (
<div className="App">
<button onClick={isWhitelisted}>TEST</button>
</div>
);
}
export default App;
遗憾的是,在执行脚本时出现了以下错误:
keccak.js:41 Uncaught ReferenceError: Buffer is not defined
at Keccak.update (keccak.js:41)
at keccak256 (keccak256.js:11)
at App.js:16
at Array.map (<anonymous>)
at isWhitelisted (App.js:16)
at HTMLUnknownElement.callCallback (react-dom.development.js:3945)
at Object.invokeGuardedCallbackDev (react-dom.development.js:3994)
at invokeGuardedCallback (react-dom.development.js:4056)
at invokeGuardedCallbackAndCatchFirstError (react-dom.development.js:4070)
at executeDispatch (react-dom.development.js:8243)
所以,错误就在这里:
const leaves = tab.map(v => keccak256(v));
但我很遗憾地找不到解决办法,你们能帮我吗?太棒了!
发布于 2021-12-30 11:35:58
好的,所以我已经找到了错误,当在依赖项中更改时
"react-scripts": "5.0.0",
至
"react-scripts": "4.0.3",
我真的不明白为什么..。如果有人知道..。
发布于 2022-05-25 01:56:44
但是降级反应-从5.0.0到4.0.3的脚本(更别提我不喜欢首先降级的想法了)导致了其他问题:无支持“
通过添加“-openssl-遗留-提供者”来轻松地处理
"scripts": {
"start": "react-scripts --openssl-legacy-provider start",
"build": "react-scripts --openssl-legacy-provider build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
但是当您构建时,接下来的问题是:
$ npm run build
> xxx@0.1.0 build
> react-scripts --openssl-legacy-provider build
Creating an optimized production build...
Failed to compile.
./node_modules/framer-motion/dist/es/components/AnimatePresence/index.mjs
Can't import the named export 'Children' from non EcmaScript module (only default export is available)
最后,我通过将框架-运动降级到4.1.7通过了:
{
"name": "darksecret",
"version": "0.1.0",
"private": true,
"dependencies": {
"@chakra-ui/react": "^2.0.0",
"@emotion/react": "^11.9.0",
"@emotion/styled": "^11.8.1",
"@openzeppelin/contracts": "^4.6.0",
"@testing-library/jest-dom": "^5.16.4",
"@testing-library/react": "^13.2.0",
"@testing-library/user-event": "^13.5.0",
"Buffer": "^0.0.0",
"keccak256": "1.0.6",
"merkletreejs": "0.2.31",
"react": "^18.1.0",
"react-dom": "^18.1.0",
"react-scripts": "4.0.3",
"web-vitals": "^2.1.4",
"framer-motion" : "4.1.7"
},
"scripts": {
"start": "react-scripts --openssl-legacy-provider start",
"build": "react-scripts --openssl-legacy-provider build",
"test": "react-scripts test",
"eject": "react-scripts eject"
}
所以,这就是为什么我最终会跌到谷底。但正如我所说,我不喜欢从一开始就降低评级。有更好的方法吗?我是新来的反应。
(哇,只是浪费了几个小时)
我没有提到在我将React应用程序从linux移到窗口之后,我仍然面临另一个错误"node.exe:坏选项:--openssl--遗留-提供者“
此外,当部署到Digitalocean时:
[2022-05-26 01:29:36] Running custom build command: npm run build
[2022-05-26 01:29:37]
[2022-05-26 01:29:37] > felinesoulmate_web@0.1.0 build
[2022-05-26 01:29:37] > react-scripts --openssl-legacy-provider build
[2022-05-26 01:29:37]
[2022-05-26 01:29:37] /layers/heroku_nodejs-engine/nodejs/bin/node: bad option: --openssl-legacy-provider
[2022-05-26 01:29:37] building: exit status 9
[2022-05-26 01:29:37] ERROR: failed to build: exit status 1
[2022-05-26 01:29:51]
[2022-05-26 01:29:51] For documentation on the buildpacks used to build your app, please see:
[2022-05-26 01:29:51] Node.js: https://do.co/apps-buildpack-node
[2022-05-26 01:29:51]
[2022-05-26 01:29:51] ! Build failed (145)
天哪,我真的不喜欢反应
发布于 2022-05-27 13:03:28
您不需要降级react脚本,只需执行以下操作:npm install --save buffer
(如果它不能工作),在您的react脚本中添加这一行,您应该会很好:window.Buffer = window.Buffer || require("buffer").Buffer;
https://stackoverflow.com/questions/70530052
复制相似问题