Home > Software engineering >  Webpack Errors - Cannot Resolve 'crypto', 'http', and 'https' in Vue.j
Webpack Errors - Cannot Resolve 'crypto', 'http', and 'https' in Vue.j

Time:05-26

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? Thank you!

CodePudding user response:

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

For Webpack 4, consider using alias option.

  • Related