1. 程式人生 > 其它 >NodeJS crypto add a fallback ‘resolve.fallback: { “crypto“: require.resolve(“crypto-browserify“) }‘

NodeJS crypto add a fallback ‘resolve.fallback: { “crypto“: require.resolve(“crypto-browserify“) }‘

技術標籤:JSwebpackcryptonodejspolyfillfallback

Intro

使用node開發過程中引入了crypto,環境條件:
Windows 10
node v12.16.3
webpack 5.6.0

試圖啟動,報錯如下:

REAKING CHANGE: webpack < 5 used to include polyfills for node.js core modules by default.
This is no longer the case. Verify if you need this module and configure a polyfill for
it. If you want to include a polyfill, you need to: - add a fallback 'resolve.fallback: { "crypto": require.resolve("crypto-browserify") }' - install 'crypto-browserify' If you don't want to include a polyfill, you can use an empty module like this: resolve.
fallback: { "crypto": false }

在這裡插入圖片描述

解決

npm install crypto-browserify

配置package.json,新增以下配置項(其中browserdependencies等配置項同級別):

  "browser": {
    "crypto": false
  },

即整個package.json前後對比,新增的變動如下:

{
	// ...
  "browser": {
    "crypto": false
  },
  "dependencies"
: { "crypto-browserify": "^3.12.0", // ... } } ``