0%

nodejieba安装记

记录安装nodejieba时踩过的坑。


软件工程课程设计之“咕咕报”需要实现一个词云功能,即根据历史聊天记录生成相应的可视化图片,因此需要接入两种API:分词、词云。在分词上,小组成员选择了Nodejieba,一个用Node.js实现的中文分词工具。不过这个包在Windows下的安装有点儿小坑,因此著文以记之。

本文按照发现问题的时间先后进行记录,如果想要完整的安装顺序,那就得倒着看啦。

安装nodejieba

  • 命令

    1
    npm install nodejieba
  • 结果

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    > nodejieba@2.4.1 install C:\Users\Doris\Desktop\chat-online-demo\node_modules\nodejieba
    > node-pre-gyp install --fallback-to-build

    node-pre-gyp WARN Using request for node-pre-gyp https download
    node-pre-gyp WARN Tried to download(404): https://github.com/yanyiwu/nodejieba/releases/downlo
    node-pre-gyp WARN Pre-built binaries not found for nodejieba@2.4.1 and node@13.11.0 (node-v79
    gyp ERR! find VS
    gyp ERR! find VS msvs_version not set from command line or npm config
    gyp ERR! find VS VCINSTALLDIR not set, not running in VS Command Prompt
    gyp ERR! find VS could not use PowerShell to find Visual Studio 2017 or newer
    gyp ERR! find VS looking for Visual Studio 2015
    gyp ERR! find VS - not found
    gyp ERR! find VS not looking for VS2013 as it is only supported up to Node.js 8
    gyp ERR! find VS
    gyp ERR! find VS **************************************************************
    gyp ERR! find VS You need to install the latest version of Visual Studio
    gyp ERR! find VS including the "Desktop development with C++" workload.
    gyp ERR! find VS For more information consult the documentation at:
    gyp ERR! find VS https://github.com/nodejs/node-gyp#on-windows
    gyp ERR! find VS **************************************************************
    gyp ERR! find VS
    gyp ERR! configure error
    gyp ERR! stack Error: Could not find any Visual Studio installation to use
    gyp ERR! stack at VisualStudioFinder.fail (C:\Program Files\nodejs\node_modules\npm\node_m
    gyp ERR! stack at C:\Program Files\nodejs\node_modules\npm\node_modules\node-gyp\lib\find-
    gyp ERR! stack at VisualStudioFinder.findVisualStudio2013 (C:\Program Files\nodejs\node_mo
    gyp ERR! stack at C:\Program Files\nodejs\node_modules\npm\node_modules\node-gyp\lib\find-
    gyp ERR! stack at C:\Program Files\nodejs\node_modules\npm\node_modules\node-gyp\lib\find-
    gyp ERR! stack at C:\Program Files\nodejs\node_modules\npm\node_modules\node-gyp\lib\util.
    gyp ERR! stack at C:\Program Files\nodejs\node_modules\npm\node_modules\node-gyp\lib\util.
    gyp ERR! stack at ChildProcess.exithandler (child_process.js:310:5)
    gyp ERR! stack at ChildProcess.emit (events.js:315:20)
    gyp ERR! stack at maybeClose (internal/child_process.js:1026:16)
    gyp ERR! System Windows_NT 10.0.18362
    gyp ERR! command "C:\\Program Files\\nodejs\\node.exe" "C:\\Program Files\\nodejs\\node_module "--module=C:\\Users\\Doris\\Desktop\\chat-online-demo\\node_modules\\nodejieba\\build\\Releasop\\chat-online-demo\\node_modules\\nodejieba\\build\\Release" "--napi_version=6" "--node_abi_
    gyp ERR! cwd C:\Users\Doris\Desktop\chat-online-demo\node_modules\nodejieba
    gyp ERR! node -v v13.11.0
    gyp ERR! node-gyp -v v5.0.7
    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 lback-to-build --module=C:\Users\Doris\Desktop\chat-online-demo\node_modules\nodejieba\build\Rchat-online-demo\node_modules\nodejieba\build\Release --napi_version=6 --node_abi_napi=napi --
    node-pre-gyp ERR! stack at ChildProcess.<anonymous> (C:\Users\Doris\Desktop\chat-online-de
    node-pre-gyp ERR! stack at ChildProcess.emit (events.js:315:20)
    node-pre-gyp ERR! stack at maybeClose (internal/child_process.js:1026:16)
    node-pre-gyp ERR! stack at Process.ChildProcess._handle.onexit (internal/child_process.js:
    node-pre-gyp ERR! System Windows_NT 10.0.18362
    node-pre-gyp ERR! command "C:\\Program Files\\nodejs\\node.exe" "C:\\Users\\Doris\\Desktop\\chk-to-build"
    node-pre-gyp ERR! cwd C:\Users\Doris\Desktop\chat-online-demo\node_modules\nodejieba
    node-pre-gyp ERR! node -v v13.11.0
    node-pre-gyp ERR! node-pre-gyp -v v0.14.0
    node-pre-gyp ERR! not ok
    Failed to execute 'C:\Program Files\nodejs\node.exe C:\Program Files\nodejs\node_modules\npm\ns\Doris\Desktop\chat-online-demo\node_modules\nodejieba\build\Release\nodejieba.node --module_odejieba\build\Release --napi_version=6 --node_abi_napi=napi --napi_build_version=0 --node_nap
    npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@1.2.12 (node_modules\fsevents):
    npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.2.12: wanted

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

    npm ERR! A complete log of this run can be found in:
    npm ERR! C:\Users\Doris\AppData\Roaming\npm-cache\_logs\2020-05-27T10_07_17_889Z-debug.log
  • 问题及解决办法
    通过报错信息,将错误定位于缺少node-gyp包。原因在于nodejieba的底层算法是由C++实现的,C++需要编译,因此需要安装原生插件构建工具node-gyp。

安装node-gyp

  • 命令

    1
    npm install node-gyp
  • 结果
    安装成功,不过跑程序的时候还是有报错信息Error: Cannot find module 'nodejieba'

  • 问题及解决办法
    其实从node-gyp也能找到安装注意事项。由于node-gyp需要依赖Python2.7和VC++库,因此要先安装这俩。安装方式有两种:使用npm、手动官网下载,这里我选择了前者。参考

安装windows-build-tools

  • 命令

    1
    npm install --production windows-build-tools
  • 结果

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    > windows-build-tools@5.2.2 postinstall C:\Users\Doris\Desktop\chat-online-demo\node_modules\windows-build-tools
    > node ./dist/index.js



    Downloading python-2.7.15.amd64.msi
    [============================================>] 100.0% of 20.25 MB (457.55 kB/s)
    Downloaded python-2.7.15.amd64.msi. Saved to C:\Users\Doris\.windows-build-tools\python-2.7.15.amd64.msi.
    Downloading vs_BuildTools.exe
    [============================================>] 100.0% of 1.12 MB (1.12 MB/s)
    Downloaded vs_BuildTools.exe. Saved to C:\Users\Doris\.windows-build-tools\vs_BuildTools.exe.

    Starting installation...
    Please restart this script from an administrative PowerShell!
    The build tools cannot be installed without administrative rights.
    To fix, right-click on PowerShell and run "as Administrator".
    npm ERR! code ELIFECYCLE
    npm ERR! errno 1
    npm ERR! windows-build-tools@5.2.2 postinstall: `node ./dist/index.js`
    npm ERR! Exit status 1
    npm ERR!
    npm ERR! Failed at the windows-build-tools@5.2.2 postinstall 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\Doris\AppData\Roaming\npm-cache\_logs\2020-05-27T14_10_33_202Z-debug.log
  • 问题及解决办法
    需要以管理员身份运行。

  • 结果
    Python的安装被挂起了。

  • 问题及解决办法
    不太清楚什么问题,找到一个解决办法是添加版本号。参考

  • 结果
    安装成功。

配置依赖

  • 命令

    1
    2
    npm config set python python2.7
    npm config set msvs_version 2015
  • 结果
    配置成功。

回到起点

  • 命令

    1
    npm install nodejieba
  • 结果
    安装成功。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    > nodejieba@2.4.1 install C:\Users\Doris\Desktop\chat-online-demo\node_modules\nodejieba
    > node-pre-gyp install --fallback-to-build

    node-pre-gyp WARN Using request for node-pre-gyp https download
    node-pre-gyp WARN Tried to download(404): https://github.com/yanyiwu/nodejieba/releases/download/2.4.1/nodejieba-v2.4.1-node-v79-win32-x64.tar.gz
    node-pre-gyp WARN Pre-built binaries not found for nodejieba@2.4.1 and node@13.11.0 (node-v79 ABI, unknown) (falling back to source compile with node-gyp)
    Building the projects in this solution one at a time. To enable parallel build, please add the "/m" switch.
    index.cpp
    nodejieba.cpp
    win_delay_load_hook.cc
    Creating library C:\Users\Doris\Desktop\chat-online-demo\node_modules\nodejieba\build\Release\nodejieba.lib and object C:\Users\Doris\Desktop\chat-online-demo\node_modules\nodejieba\build\Release
    \nodejieba.exp
    nodejieba.vcxproj -> C:\Users\Doris\Desktop\chat-online-demo\node_modules\nodejieba\build\Release\\nodejieba.node
    nodejieba.vcxproj -> C:\Users\Doris\Desktop\chat-online-demo\node_modules\nodejieba\build\Release\nodejieba.pdb (Full PDB)
    npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@1.2.12 (node_modules\fsevents):
    npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.2.12: wanted {"os":"darwin","arch":"any"} (current: {"os":"win32","arch":"x64"})

    + nodejieba@2.4.1
    added 14 packages from 23 contributors in 54.155s

    43 packages are looking for funding
    run `npm fund` for details

抱怨

忽略最后俩,大概新增了这么些东西。这对本就不富裕的小硬盘来说,更是雪上加霜了QAQ