在 w10 x64 上使用 npm 安装 bcrypt 时语法无效

Invalid syntax while installing bcrypt with npm on w10 x64

我的机器上安装了 python 2.7.15 和 3.7。

我运行这个命令每个 https://github.com/kelektiv/node.bcrypt.js/wiki/Installation-Instructions#microsoft-windows

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

当我 运行 npm install --save bcrypt 时,出现以下错误:

PS C:\Users\Yael-PC1\Projectz\ys> npm install --save bcrypt

> bcrypt@3.0.7 install C:\Users\Yael-PC1\Projectz\ys\node_modules\bcrypt
> node-pre-gyp install --fallback-to-build

node-pre-gyp WARN Using needle for node-pre-gyp https download
node-pre-gyp WARN Tried to download(404): https://github.com/kelektiv/node.bcrypt.js/releases/download/v3.0.7/bcrypt_lib-v3.0.7-node-v64-win32-x64-unknown.tar.gz
node-pre-gyp WARN Pre-built binaries not found for bcrypt@3.0.7 and node@10.15.3 (node-v64 ABI, unknown) (falling back to source compile with node-gyp)
gyp ERR! configure error
gyp ERR! stack Error: Command failed: C:\Users\Yael-PC1\AppData\Local\Programs\Python\Python37\python.EXE -c import sys; print "%s.%s.%s" % sys.version_info[:3];
gyp ERR! stack   File "<string>", line 1
gyp ERR! stack     import sys; print "%s.%s.%s" % sys.version_info[:3];
gyp ERR! stack                                ^
gyp ERR! stack SyntaxError: invalid syntax
gyp ERR! stack
gyp ERR! stack     at ChildProcess.exithandler (child_process.js:294:12)
gyp ERR! stack     at ChildProcess.emit (events.js:189:13)
gyp ERR! stack     at maybeClose (internal/child_process.js:970:16)
gyp ERR! stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:259:5)
gyp ERR! System Windows_NT 10.0.17763
gyp ERR! command "C:\Program Files\nodejs\node.exe" "C:\Program Files\nodejs\node_modules\npm\node_modules\node-gyp\bin\node-gyp.js" "configure" "--fallback-to-build" "--module=C:\Users\Yael-PC1\Projectz\ys\node_modules\bcrypt\lib\binding\bcrypt_lib.node"
"--module_name=bcrypt_lib" "--module_path=C:\Users\Yael-PC1\Projectz\ys\node_modules\bcrypt\lib\binding" "--napi_version=3" "--node_abi_napi=napi" "--napi_build_version=0" "--node_napi_label=node-v64"
gyp ERR! cwd C:\Users\Yael-PC1\Projectz\ys\node_modules\bcrypt
gyp ERR! node -v v10.15.3
gyp ERR! node-gyp -v v3.8.0
gyp ERR! not ok
node-pre-gyp ERR! build error
node-pre-gyp ERR! stack Error: Failed to execute 'C:\Program Files\nodejs\node.exe C:\Program Files\nodejs\node_modules\npm\node_modules\node-gyp\bin\node-gyp.js configure --fallback-to-build --module=C:\Users\Yael-PC1\Projectz\ys\node_modules\bcrypt\lib\binding\bcrypt_lib.node --module_name=bcrypt_lib --module_path=C:\Users\Yael-PC1\Projectz\ys\node_modules\bcrypt\lib\binding --napi_version=3 --node_abi_napi=napi --napi_build_version=0 --node_napi_label=node-v64' (1)
node-pre-gyp ERR! stack     at ChildProcess.<anonymous> (C:\Users\Yael-PC1\Projectz\ys\node_modules\node-pre-gyp\lib\util\compile.js:83:29)
node-pre-gyp ERR! stack     at ChildProcess.emit (events.js:189:13)
node-pre-gyp ERR! stack     at maybeClose (internal/child_process.js:970:16)
node-pre-gyp ERR! stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:259:5)
node-pre-gyp ERR! System Windows_NT 10.0.17763
node-pre-gyp ERR! command "C:\Program Files\nodejs\node.exe" "C:\Users\Yael-PC1\Projectz\ys\node_modules\node-pre-gyp\bin\node-pre-gyp" "install" "--fallback-to-build"
node-pre-gyp ERR! cwd C:\Users\Yael-PC1\Projectz\ys\node_modules\bcrypt
node-pre-gyp ERR! node -v v10.15.3
node-pre-gyp ERR! node-pre-gyp -v v0.13.0
node-pre-gyp ERR! not ok
Failed to execute 'C:\Program Files\nodejs\node.exe C:\Program Files\nodejs\node_modules\npm\node_modules\node-gyp\bin\node-gyp.js configure --fallback-to-build --module=C:\Users\Yael-PC1\Projectz\ys\node_modules\bcrypt\lib\binding\bcrypt_lib.node --module_name=bcrypt_lib --module_path=C:\Users\Yael-PC1\Projectz\ys\node_modules\bcrypt\lib\binding --napi_version=3 --node_abi_napi=napi --napi_build_version=0 --node_napi_label=node-v64' (1)
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@1.2.9 (node_modules\fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.2.9: wanted {"os":"darwin","arch":"any"} (current: {"os":"win32","arch":"x64"})

npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! bcrypt@3.0.7 install: `node-pre-gyp install --fallback-to-build`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the bcrypt@3.0.7 install script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\Yael-PC1\AppData\Roaming\npm-cache\_logs19-12-14T02_49_00_828Z-debug.log
PS C:\Users\Yael-PC1\Projectz\ys> ^C
PS C:\Users\Yael-PC1\Projectz\ys>

啊,这个。

如果你查看末尾的行,当它查看你的节点版本和 node-pre-gyp 版本时,它说 NOT OK

最简单的解决方案应该是更新 npm、node 和 node-pre-gyp

npm install npm@latest --save
npm install node@latest --save
npm install node-pre-gyp@latest --save
npm install prebuild@latest --save
npm install node-gyp@latest --save
npm install bcrypt --save

理论上这应该可以解决您遇到的错误。

然而

如果这种情况仍然存在,您可以考虑切换到 bcryptjs,如果您只是使用散列和比较函数,它将与您的代码一起工作,并且没有依赖性,所以这是一个更安全的选择。