简体   繁体   English

反应:找不到模块:无法解析“ xmlhttprequest”

[英]React: Module not found: Can't resolve 'xmlhttprequest'

I'm getting an error when trying to build my React project: 尝试构建我的React项目时出现错误:

./node_modules/ethers/utils/web.js Module not found: Can't resolve 'xmlhttprequest' in '/mnt/c/Users/.../node_modules/ethers/utils'

In package.json: "dependencies": { "@material-ui/core": "^3.8.1", "@material-ui/icons": "^3.0.1", "axios": "^0.18.0", "immutability-helper": "^2.9.0", "moment": "^2.22.2", "react": "^16.7.0", "react-dom": "^16.7.0", "react-router-dom": "^4.3.1", "react-scripts": "^2.1.2" }, 在package.json中: "dependencies": { "@material-ui/core": "^3.8.1", "@material-ui/icons": "^3.0.1", "axios": "^0.18.0", "immutability-helper": "^2.9.0", "moment": "^2.22.2", "react": "^16.7.0", "react-dom": "^16.7.0", "react-router-dom": "^4.3.1", "react-scripts": "^2.1.2" },

What should I do? 我该怎么办?

I got my project working again by not importing 'truffle-contract'. 通过不导入“松露合同”,我的项目得以重新工作。 There's a React Truffle example here: https://github.com/truffle-box/react-box which doesn't import truffle-contract. 这里有一个React Truffle的示例: https : //github.com/truffle-box/react-box ,它不会导入松露合约。 I made my project more like that one. 我使我的项目更像那个项目。 I then had to change my code to use the contract interface provided by web3: https://web3js.readthedocs.io/en/1.0/web3-eth-contract.html 然后,我不得不更改代码以使用由web3提供的协定接口: https ://web3js.readthedocs.io/en/1.0/web3-eth-contract.html

To overcome this problem you need to install a different version of ethers as explained in this issue: 要解决此问题,您需要按照此问题中的说明安装不同版本的ethers:

https://github.com/trufflesuite/truffle/issues/1614#issuecomment-452158490 https://github.com/trufflesuite/truffle/issues/1614#issuecomment-452158490

Exact comment with solution: 带有解决方案的确切评论:

ethers (v4.0.0-beta.1) gets installed as a dependency of web3-eth-abi (v1.0.0-beta.37) Which leads to the xmlhttprequest complaint and basically makes truffle-contract unusable. 安装ethers(v4.0.0-beta.1)作为web3-eth-abi(v1.0.0-beta.37)的依赖项,这导致xmlhttprequest投诉,并且基本上使松露合同无法使用。 I was able to get around it for now by manually upping ethers to v4.0.20. 我现在可以通过手动将ethers升级到v4.0.20来解决它。

$ npm install ethers@4.0.20 $ npm安装ethers@4.0.20

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

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