node.jsreactjsbcrypt

Problem with React + Node + bcrypt (Module not found: Can´t resolve "fs" in \node_modules\@mapbox\node-pre-gyp\lib


I´m new to React and Node, so I´m sorry if I ask something in a wrong way... The thing is that I have a project I just completed and everything works fine, but as I have users registration and login I need to implement bcrypt to be able to hash my passwords and not storing them as plain text obviously.

After I install bcrypt with npm i bcrypt and add it with const bcrypt = require('bcrypt') I get tons of errors when compiling (116 errors!) but at least they´re mostly the same error but I don´t understand hot to fix it.

Here´s a screenshot (I´m not using webpack, at least I didn´t install it and it´s not in my package.json dependencies).

Can´t resolve path and fs Error


Solution

  • it seems you are using webpack v5 and there is some breaking changes in this version . you should add to package.json this block :

    "browser": {
        "fs": false,
        "os": false,
        "path": false
      }
    

    as the error says you have to use polyfill but if you don't want to include it you can set fs, os and path to false, to use an empty module instead of including a polyfill for the fs module.

    If you use Create React App, you might have to edit your webpack.config.json file (the path to it is node_modules/react-scripts/config/webpack.config.json) and add a fallback to your resolve.fallback property :

    resolve: {
          // ...
          fallback: {
            //  add this 👇️
            "fs": false,
            "os": false,
            "path": false,
          }
        }