使用 npm 安装 bcrypt 时出错

我无法在我的机器上使用 npm安装 bcrypt,因为我遇到了以下错误。我一直在解决这个问题,但运气不佳。您能推荐一些诊断或修复问题的步骤吗? 这样我就可以成功地运行 npm install bcrypt了?

Someones-Macbook:node_modules Cody$ npm install bcrypt
npm WARN package.json grunt-contrib-htmlmin@0.1.3 No README data
|
> bcrypt@0.7.7 install /Applications/MAMP/htdocs/richintime/ultimate-seed/node_modules/bcrypt
> node-gyp rebuild


child_process: customFds option is deprecated, use stdio instead.
CXX(target) Release/obj.target/bcrypt_lib/src/blowfish.o
CXX(target) Release/obj.target/bcrypt_lib/src/bcrypt.o
CXX(target) Release/obj.target/bcrypt_lib/src/bcrypt_node.o
../src/bcrypt_node.cc:118:18: error: no member named 'Dispose' in 'v8::Persistent<v8::Function, v8::NonCopyablePersistentTraits<v8::Function>
>'
callback.Dispose();
~~~~~~~~ ^
../src/bcrypt_node.cc:202:19: error: unknown type name 'uv_work_t'
void GenSaltAsync(uv_work_t* req) {
^
../src/bcrypt_node.cc:219:24: error: unknown type name 'uv_work_t'
void GenSaltAsyncAfter(uv_work_t* req) {
^
../src/bcrypt_node.cc:220:17: error: calling a protected constructor of class 'v8::HandleScope'
HandleScope scope;
^
/Users/Cody/.node-gyp/0.12.0/deps/v8/include/v8.h:816:13: note: declared protected here
V8_INLINE HandleScope() {}
^
../src/bcrypt_node.cc:228:44: error: no member named 'New' in 'v8::String'
argv[0] = Exception::Error(String::New(baton->error.c_str()));
~~~~~~~~^
../src/bcrypt_node.cc:229:19: error: no matching function for call to 'Undefined'
argv[1] = Undefined();
^~~~~~~~~
/Users/Cody/.node-gyp/0.12.0/deps/v8/include/v8.h:305:28: note: candidate function not viable: requires single argument 'isolate', but no
arguments were provided
friend Handle<Primitive> Undefined(Isolate* isolate);
^
../src/bcrypt_node.cc:232:19: error: no matching function for call to 'Undefined'
argv[0] = Undefined();
^~~~~~~~~
/Users/Cody/.node-gyp/0.12.0/deps/v8/include/v8.h:305:28: note: candidate function not viable: requires single argument 'isolate', but no
arguments were provided
friend Handle<Primitive> Undefined(Isolate* isolate);
^
../src/bcrypt_node.cc:238:20: error: member reference type 'v8::Persistent<v8::Function>' is not a pointer; maybe you meant to use '.'?
baton->callback->Call(Context::GetCurrent()->Global(), 2, argv);
~~~~~~~~~~~~~~~^~
.
../src/bcrypt_node.cc:238:22: error: no member named 'Call' in 'v8::Persistent<v8::Function, v8::NonCopyablePersistentTraits<v8::Function> >'
baton->callback->Call(Context::GetCurrent()->Global(), 2, argv);
~~~~~~~~~~~~~~~  ^
../src/bcrypt_node.cc:238:36: error: no member named 'GetCurrent' in 'v8::Context'
baton->callback->Call(Context::GetCurrent()->Global(), 2, argv);
~~~~~~~~~^
../src/bcrypt_node.cc:246:34: error: unknown type name 'Arguments'; did you mean 'v8::internal::Arguments'?
Handle<Value> GenerateSalt(const Arguments &args) {
^~~~~~~~~
v8::internal::Arguments
/Users/Cody/.node-gyp/0.12.0/deps/v8/include/v8.h:127:7: note: 'v8::internal::Arguments' declared here
class Arguments;
^
../src/bcrypt_node.cc:247:17: error: calling a protected constructor of class 'v8::HandleScope'
HandleScope scope;
^
/Users/Cody/.node-gyp/0.12.0/deps/v8/include/v8.h:816:13: note: declared protected here
V8_INLINE HandleScope() {}
^
../src/bcrypt_node.cc:249:32: error: type 'const v8::internal::Arguments' does not provide a subscript operator
const ssize_t rounds = args[0]->Int32Value();
~~~~^~
../src/bcrypt_node.cc:250:30: error: type 'const v8::internal::Arguments' does not provide a subscript operator
const int rand_len = args[1]->Int32Value();
~~~~^~
../src/bcrypt_node.cc:251:58: error: type 'const v8::internal::Arguments' does not provide a subscript operator
Local<Function> callback = Local<Function>::Cast(args[2]);
~~~~^~
../src/bcrypt_node.cc:255:45: error: 'New' is a private member of 'v8::PersistentBase<v8::Function>'
baton->callback = Persistent<Function>::New(callback);
^
/Users/Cody/.node-gyp/0.12.0/deps/v8/include/v8.h:572:23: note: declared private here
V8_INLINE static T* New(Isolate* isolate, T* that);
^
../src/bcrypt_node.cc:255:57: error: too few arguments to function call, expected 2, have 1
baton->callback = Persistent<Function>::New(callback);
~~~~~~~~~~~~~~~~~~~~~~~~~         ^
/Users/Cody/.node-gyp/0.12.0/deps/v8/include/v8.h:572:3: note: 'New' declared here
V8_INLINE static T* New(Isolate* isolate, T* that);
^
/Users/Cody/.node-gyp/0.12.0/deps/v8/include/v8config.h:289:20: note: expanded from macro 'V8_INLINE'
# define V8_INLINE inline __attribute__((always_inline))
^
../src/bcrypt_node.cc:259:5: error: unknown type name 'uv_work_t'
uv_work_t* req = new uv_work_t;
^
../src/bcrypt_node.cc:259:26: error: unknown type name 'uv_work_t'
uv_work_t* req = new uv_work_t;
^
fatal error: too many errors emitted, stopping now [-ferror-limit=]
20 errors generated.
make: *** [Release/obj.target/bcrypt_lib/src/bcrypt_node.o] Error 1
gyp ERR! build error
gyp ERR! stack Error: `make` failed with exit code: 2
gyp ERR! stack     at ChildProcess.onExit (/usr/local/lib/node_modules/npm/node_modules/node-gyp/lib/build.js:267:23)
gyp ERR! stack     at ChildProcess.emit (events.js:110:17)
gyp ERR! stack     at Process.ChildProcess._handle.onexit (child_process.js:1067:12)
gyp ERR! System Darwin 13.4.0
gyp ERR! command "node" "/usr/local/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js" "rebuild"
gyp ERR! cwd /Applications/MAMP/htdocs/richintime/ultimate-seed/node_modules/bcrypt
gyp ERR! node -v v0.12.0
gyp ERR! node-gyp -v v1.0.2
gyp ERR! not ok
npm ERR! Darwin 13.4.0
npm ERR! argv "node" "/usr/local/bin/npm" "install" "bcrypt"
npm ERR! node v0.12.0
npm ERR! npm  v2.7.1
npm ERR! code ELIFECYCLE


npm ERR! bcrypt@0.7.7 install: `node-gyp rebuild`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the bcrypt@0.7.7 install script 'node-gyp rebuild'.
npm ERR! This is most likely a problem with the bcrypt package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR!     node-gyp rebuild
125634 次浏览

The easy solution is just switch from the "bcrypt" npm module to bycryptjs or bcrypt-nodejs. It's the exact same API, but pure JS so no native add-ons to deal with.

npm install --save bcryptjs && npm uninstall --save bcrypt

Then change your require calls to "bcryptjs", but all your code otherwise can be unchanged.

Long term, I suspect at the moment bcrypt may not be ready for node v0.12.0 yet, but if not it will eventually be updated and ready.

On Mac OS, after a Xcode update, running sudo gcc and accepting Xcode's new license solved the problem!

Had the same problem. Updating npm and try again (npm install -g npm usually). It fix the issue for me.

For windows install the following module:

npm install -g node-gyp
npm install --g --production windows-build-tools

Then try to install:

npm install bcrypt

On this command:

npm install --g --production windows-build-tools

You may have to run this in Powershell as administrator on your Windows machine.

You do that by right-clicking on Powershell and select "Run As Administrator"

I just ran the following then tried the install and it worked:

npm install node-pre-gyp -g

I was facing the same issue on the server side( aws ubuntu 16.04).

I tried deleting node_module/bcrypt by sudo rm -rf node_module/bcrypt multiple times and reinstalled it again and again by various ways but still facing the same issue.

I checked node version with nvm and it was also showing stable (v10.11.0).

Finally, I tried.

sudo npm cache clean -f
sudo npm install -g n
sudo n stable

and it worked for me.

If you are on windows run powershell as administrator and use these commands.

npm install --g --production windows-build-tools

and then install bcrypt using

npm install --save bcrypt

I was also having the same problem. The following commands helped me.

npm rebuild

then

npm install bcrypt

Reason for Error : Node maybe can’t able to find the Python path on your system

Solution

step: 1 Rum command prompt as administrator step 2 Install the package

  npm install --global --production windows-build-tools

It may take some while, keep patient

step 3 Now install

npm install node-gyp

Last step Now you are ready to Go

npm install bcrypt

if any of the above fails to work, do:

npm install node-gyp

Afterwards visit the official python download site Official Python download link

Install the latest version of python, make sure to check add python to path add python to path

After installation, do:

npm --save install bcrypt

I managed to solve this by running the command: sudo apt-get install -y build-essential python and then yarn add bcrypt.

You can find the instructions here:

https://github.com/kelektiv/node.bcrypt.js/wiki/Installation-Instructions

https://github.com/kelektiv/node.bcrypt.js

I installed bcrypt-nodejs though it's already deprecated, and then removed it and installed bcrypt again. Like this, I Installed bcrypt successfully and it worked fine.

yarn add bcrypt-nodejs
yarn remove bcrypt-nodejs
yarn add bcrypt

The accepted solution works, but bcryptjs is slower than python and C++ version.

If you still want bcrypt instead of bcryptjs, working solutions are the commands below, but they can also have their own problems.

✔️ npm install -g node-gyp
❌ npm install --g --production windows-build-tools

In Windows, the second command will try to install Visual Studio along with python and hence the problem arises because the Visual Studio installation takes a lot of time. Also it also hinders the installation of python. If you forget to run those commands as Administrator, then they will get installed at C:\Users\User.windows-build-tools which will still not work because node will search for python in C:\ or C:\Program Files. Hence these commands in some cases may increase your complications.

So the best idea for you is to ✔️install python manually for All Users. It is not necessary to add python to the environment path.

Then run:

✔️ npm install -g node-gyp

Now you are ready to install bcrypt:

npm install bcrypt

Upgrade to bcrypt@5.0.0

npm i bcrypt@5.0.0

I am not getting errors on this newer version of bcrypt, older version gave different types of errors ranging from not able to install bcrypt to typescript type errors

Running

sudo apt install build-essential

installs all the tools you need to install bcrypt on Linux. Then you can run

npm install

I had node version 12.18.3 and fixed the same by running npm i bcrypt@3.0.6

This happened to me as I was installing a package from github that had an older version of bcrypt as a dependency. I just uninstalled the old bcrypt version and installed the newest version:

    npm uninstall bcrypt
npm install bcrypt

Then, voila. It worked.