webpack错误 - 无法解决' crypto',' http'和' https'在vue.js中

发布于 2025-02-01 13:44:01 字数 2762 浏览 0 评论 0原文

问题背景

我正在尝试将钱包连接添加到我们的项目(vue.js),在此处: https://docs.walletconnect.com/quick-start/dapps/web3-provider ,我使用此命令来安装它。

在我的JS文件中导入它,

import Web3 from 'web3'
import WalletConnectProvider from '@walletconnect/web3-provider'

它显示了这些错误:

ERROR  Failed to compile with 7 errors                                                                               
These dependencies were not found:

* crypto in ./node_modules/eth-lib/lib/bytes.js, ./node_modules/web3-eth-accounts/lib/index.js and 1 other
* http in ./node_modules/xhr2-cookies/dist/xml-http-request.js
* https in ./node_modules/xhr2-cookies/dist/xml-http-request.js

To install them, you can run: npm install --save crypto http https

尝试

我安装这些软件包。

npm i crypto-browserify
npm i https-browserify
npm i stream-http

然后编辑我的vue.config.js文件:

resolve: {
  fallback: {
    crypto: require.resolve('crypto-browserify'),
    http: require.resolve('stream-http'),
    https: require.resolve('https-browserify'),
  },
},

并且错误变为:

WebpackOptionsValidationError: Invalid configuration object. Webpack has been initialised using a configuration object that does not match the API schema.
configuration.resolve has an unknown property 'fallback'. 

These properties are valid:
   object { alias?, aliasFields?, cachePredicate?, cacheWithContext?, concord?, descriptionFiles?, enforceExtension?, enforceModuleExtension?, extensions?, fileSystem?, mainFields?, mainFiles?, moduleExtensions?, modules?, plugins?, resolver?, symlinks?, unsafeCache?, useSyncFileSystemCalls? }

这是我的package.json.json文件看起来像:

{
  "dependencies": {
    "@walletconnect/web3-provider": "^1.7.8",
    "crypto-browserify": "^3.12.0",
    "https-browserify": "^1.0.0",
    "os": "^0.1.2",
    "stream": "^0.0.2",
    "stream-http": "^3.2.0",
    "vue": "^2.6.10",
    "web3": "^1.7.3",
  },
  "devDependencies": {
    "@vue/cli-service": "^3.7.0",
    "webpack": "^4.39.3",
    "webpack-bundle-analyzer": "^3.6.0",
    "webpack-dev-server": "^3.11.2",
    "webpack-merge": "^4.2.1",
    "webpack-node-externals": "^1.7.2",
    "webpack-plugin-hash-output": "^3.2.1",
    "webpack-spritesmith": "^1.0.1",
  }
}

而我的节点版本是v10.24.1,版本仍然无法暂时升级。

v10.24.1

从上面可以看出,我已经从错误中安装了建议的软件包(crypto-browserifystream-httphttps-browserify)并将它们包括在vue.config.js中。

我该如何解决?

Question Background

I'm trying to add Wallet Connect to our project(Vue.js), form here: https://docs.walletconnect.com/quick-start/dapps/web3-provider, I used this command to install it.

import it in my js file

import Web3 from 'web3'
import WalletConnectProvider from '@walletconnect/web3-provider'

it shows those errors:

ERROR  Failed to compile with 7 errors                                                                               
These dependencies were not found:

* crypto in ./node_modules/eth-lib/lib/bytes.js, ./node_modules/web3-eth-accounts/lib/index.js and 1 other
* http in ./node_modules/xhr2-cookies/dist/xml-http-request.js
* https in ./node_modules/xhr2-cookies/dist/xml-http-request.js

To install them, you can run: npm install --save crypto http https

Try

I install those packages.

npm i crypto-browserify
npm i https-browserify
npm i stream-http

Then edit my vue.config.js files:

resolve: {
  fallback: {
    crypto: require.resolve('crypto-browserify'),
    http: require.resolve('stream-http'),
    https: require.resolve('https-browserify'),
  },
},

and the errors becomes:

WebpackOptionsValidationError: Invalid configuration object. Webpack has been initialised using a configuration object that does not match the API schema.
configuration.resolve has an unknown property 'fallback'. 

These properties are valid:
   object { alias?, aliasFields?, cachePredicate?, cacheWithContext?, concord?, descriptionFiles?, enforceExtension?, enforceModuleExtension?, extensions?, fileSystem?, mainFields?, mainFiles?, moduleExtensions?, modules?, plugins?, resolver?, symlinks?, unsafeCache?, useSyncFileSystemCalls? }

This is my package.json file look like:

{
  "dependencies": {
    "@walletconnect/web3-provider": "^1.7.8",
    "crypto-browserify": "^3.12.0",
    "https-browserify": "^1.0.0",
    "os": "^0.1.2",
    "stream": "^0.0.2",
    "stream-http": "^3.2.0",
    "vue": "^2.6.10",
    "web3": "^1.7.3",
  },
  "devDependencies": {
    "@vue/cli-service": "^3.7.0",
    "webpack": "^4.39.3",
    "webpack-bundle-analyzer": "^3.6.0",
    "webpack-dev-server": "^3.11.2",
    "webpack-merge": "^4.2.1",
    "webpack-node-externals": "^1.7.2",
    "webpack-plugin-hash-output": "^3.2.1",
    "webpack-spritesmith": "^1.0.1",
  }
}

and my node version is v10.24.1, the version still can't be upgraded temporarily.

v10.24.1

As seen from the above, I have installed the suggested packages from the errors (crypto-browserify, stream-http, and https-browserify) and have included them in the vue.config.js.

How can I solve this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鸩远一方 2025-02-08 13:44:02

您使用的是WebPack 4。shurpback选项仅从WebPack > = 5.0.0开始。

对于WebPack 4,请考虑使用Alias选项。

You are using Webpack 4. The fallback option is only available from Webpack >=5.0.0 onwards.

For Webpack 4, consider using alias option.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文