node.js Module not found: Error: Can’t resolve ‘crypto’

在打包過程, 遇到錯誤, 但直接用 node 執行程式是無誤的.

解法:
https://stackoverflow.com/questions/70565266/module-not-found-error-cant-resolve-crypto-react

Try adding the following just after the devDependencies in your package.json

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

遇到的錯誤訊息:

ERROR in ./src/index.js 1:13-30
Module not found: Error: Can't resolve 'crypto' in '/home/max/npm/hello/src'

BREAKING 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 }
resolve 'crypto' in '/home/max/npm/hello/src'
  Parsed request is a module
  using description file: /home/max/npm/hello/package.json (relative path: ./src)
    Field 'browser' doesn't contain a valid alias configuration
    resolve as module
      /home/max/npm/hello/src/node_modules doesn't exist or is not a directory
      looking for modules in /home/max/npm/hello/node_modules
        single file module
          using description file: /home/max/npm/hello/package.json (relative path: ./node_modules/crypto)
            no extension
              Field 'browser' doesn't contain a valid alias configuration
              /home/max/npm/hello/node_modules/crypto doesn't exist
            .js
              Field 'browser' doesn't contain a valid alias configuration
              /home/max/npm/hello/node_modules/crypto.js doesn't exist
            .json
              Field 'browser' doesn't contain a valid alias configuration
              /home/max/npm/hello/node_modules/crypto.json doesn't exist
            .wasm
              Field 'browser' doesn't contain a valid alias configuration
              /home/max/npm/hello/node_modules/crypto.wasm doesn't exist
        /home/max/npm/hello/node_modules/crypto doesn't exist
      /home/max/npm/node_modules doesn't exist or is not a directory
      /home/max/node_modules doesn't exist or is not a directory
      /home/node_modules doesn't exist or is not a directory
      /node_modules doesn't exist or is not a directory

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *