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

huangapple go评论75阅读模式
英文:

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

问题

我对React和Node都很新,所以如果我以错误的方式提问,我很抱歉... 问题是,我有一个刚刚完成的项目,一切都运行正常,但由于我有用户注册和登录,我需要实现bcrypt来能够对密码进行哈希处理,而不是将它们存储为明文。

在我使用npm i bcrypt安装了bcrypt并使用const bcrypt = require('bcrypt')添加它后,编译时出现了大量错误(116个错误!),但至少它们大多是相同的错误,但我不知道如何修复它。

这是一张截图(我不使用webpack,至少我没有安装它,它也不在我的package.json依赖项中)。

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).

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

答案1

得分: 1

看起来你正在使用webpack v5,并且在这个版本中有一些破坏性的变化。你应该在package.json中添加以下代码块:

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

正如错误提示所说,你需要使用polyfill,但如果你不想包含它,你可以将 fsospath 设置为 false,以使用一个空模块来代替包含fs模块的polyfill。

如果你使用Create React App,你可能需要编辑你的webpack.config.json文件(路径是node_modules/react-scripts/config/webpack.config.json),并为你的resolve.fallback属性添加一个回退:

resolve: {
    // ...
    fallback: {
        // 添加这个
        "fs": false,
        "os": false,
        "path": false
    }
}
英文:

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,
      }
    }

huangapple
  • 本文由 发表于 2023年6月29日 04:37:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/76576563.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定