Module not found: Error: Can't resolve 'crypto' in 'C:\

ERROR MESSAGE:

Compiled with problems:X

ERROR in ./node_modules/@solana/spl-name-service/dist/utils.js 16:17-34

Module not found: Error: Can't resolve 'crypto' in 'C:\Users\...\node_modules\@solana\spl-name-service\dist'

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 }

Where the problem started:

  • I tried using getParsedNftAccountsByOwner() from: https://github.com/NftEyez/sol-rayz
  • Then, I got this error message.

My project:

  • getParsedNftAccountsByOwner()
  • React, Typescript project
  • Libraries used: @solana/web3.js | @nfteyez/sol-rayz
  • webpack 5.67.0

Even though I was able to resolve these errors in a few minutes, the 'crypto' one is different:

Stream problem:

ERROR in ./node_modules/cipher-base/index.js 3:16-43

Module not found: Error: Can't resolve 'stream' in 'C:\Users\...\node_modules\cipher-base'

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: { "stream": require.resolve("stream-browserify") }'
    - install 'stream-browserify'
If you don't want to include a polyfill, you can use an empty module like this:
    resolve.fallback: { "stream": false }

assert problem:

ERROR in ./node_modules/@solana/spl-name-service/dist/utils.js 14:33-50

Module not found: Error: Can't resolve 'assert' in 'C:\Users\...\node_modules\@solana\spl-name-service\dist'

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: { "assert": require.resolve("assert/") }'
    - install 'assert'
If you don't want to include a polyfill, you can use an empty module like this:
    resolve.fallback: { "assert": false }

What I have done so far:

  1. I tried performing the two suggestions from the error message, and it didn't work

  2. I tried the first two solutions already:

    "compilerOptions": { "baseUrl": "./", "paths": { "crypto": [ "node_modules/crypto-js" ] }

Module not found: Error: Can't resolve 'crypto'

  1. I tried this solution:

    "browser": { "crypto": false }

https://github.com/tensorflow/tfjs/issues/494#issuecomment-481636128

  1. I tried this solution: node-polyfill-webpack-plugin

https://stackoverflow.com/a/65556946/18007508

const NodePolyfillPlugin = require("node-polyfill-webpack-plugin")
module.exports = {
        // Other rules...
        plugins: [
            new NodePolyfillPlugin()
        ]
    } 

My Code: webpack.config.js

const HtmlWebpackPlugin = require("html-webpack-plugin");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const NodePolyfillPlugin = require("node-polyfill-webpack-plugin")
module.exports = {
  entry: "./src/components/index.tsx",
  target: "web",
  mode: "development",
  output: {
    path: path.resolve(\__dirname, "build"),
    filename: "bundle.js",
  },
  resolve: {
    extensions: [".js", ".jsx", ".json", ".ts", ".tsx"],
    fallback: {
      "crypto":require.resolve('crypto-browserify') ,
    } 
  },
  module: {
    rules: [
      {
        test: /\.(ts|tsx)$/,
        loader: "awesome-typescript-loader",
      },
      {
        enforce: "pre",
        test: /\.js$/,
        loader: "source-map-loader",
      },
      {
        test: /\.css$/,
        loader: "css-loader",
      },
    ],
  },
  plugins: [
    new HtmlWebpackPlugin({
      template: path.resolve(\__dirname, "src", "components", "index.html"),
    }),
    new MiniCssExtractPlugin({
      filename: "./src/yourfile.css",
    }),
    new NodePolyfillPlugin()
  ],
};

package.json

{
  "name": "my-app",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@headlessui/react": "^1.4.3",
    "@heroicons/react": "^1.0.5",
    "@nfteyez/sol-rayz": "^0.9.1",
    "@nfteyez/sol-rayz-react": "^0.9.1",
    "@solana/spl-name-service": "^0.1.3",
    "@solana/wallet-adapter-base": "^0.9.2",
    "@solana/wallet-adapter-react": "^0.15.2",
    "@solana/wallet-adapter-react-ui": "^0.9.4",
    "@solana/wallet-adapter-wallets": "^0.14.2",
    "@solana/web3.js": "^1.32.0",
    "@testing-library/jest-dom": "^5.16.1",
    "@testing-library/react": "^12.1.2",
    "@testing-library/user-event": "^13.5.0",
    "@types/jest": "^27.4.0",
    "@types/node": "^16.11.19",
    "assert": "^2.0.0",
    "axios": "^0.25.0",
    "browser": "^0.2.6",
    "crypto": "^1.0.1",
    "crypto-browserify": "^3.12.0",
    "crypto-js": "^4.1.1",
    "install": "^0.13.0",
    "node-polyfill-webpack-plugin": "^1.1.4",
    "react": "^17.0.2",
    "react-dom": "^17.0.2",
    "react-redux": "^7.2.6",
    "react-router": "^6.2.1",
    "react-router-dom": "^6.2.1",
    "react-scripts": "5.0.0",
    "redux": "^4.1.2",
    "redux-thunk": "^2.4.1",
    "store": "^2.0.12",
    "stream": "^0.0.2",
    "stream-browserify": "^3.0.0",
    "styled-components": "^5.3.3",
    "three": "^0.136.0",
    "web-vitals": "^2.1.3"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": [
      "react-app",
      "react-app/jest"
    ]
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  },
  "devDependencies": {
    "@types/react": "^17.0.38",
    "@types/react-dom": "^17.0.11",
    "@types/react-redux": "^7.1.22",
    "@types/react-router-dom": "^5.3.2",
    "@types/styled-components": "^5.1.20",
    "autoprefixer": "^10.4.2",
    "awesome-typescript-loader": "^5.2.1",
    "css-loader": "^6.5.1",
    "html-webpack-plugin": "^5.5.0",
    "mini-css-extract-plugin": "^2.5.0",
    "postcss": "^8.4.5",
    "source-map-loader": "^3.0.1",
    "tailwindcss": "^3.0.15",
    "typescript": "^4.5.4",
    "webpack-dev-server": "^4.7.3"
  }
}

I think that because your create react app (CRA) does not recognize your webpack.config.js. Your setting for polyfills is correct. Setting up node-polyfill-webpack-plugin itself is enough to load all node core polyfills.

In order to modify the webpack.config.js in CRA, you have to run

   npm run eject

this will create a config directory and inside you will have webpack.config.js which has almost 1000 lines of code. Inside that file, setting up node-polyfill-webpack-plugin should be enough.