Windows下napi环境部署

写在前面

上篇文章记录了在Ubuntu虚拟机搭建开发环境的过程,但是最近公司开始禁用虚拟机了,不得不把开发环境转到Windows上,这篇文章记录了Windows下napi环境部署过程,还有踩的一些坑(⊙︿⊙)…

一、开发环境准备

1、git

下载链接:link
首先安装git是因为在安装sourcetree的时候,需要git环境,而sourcetree自动安装git时用公司网络总是安装不成功。

2、sourcetree

用sourcetree做代码管理工具,下载链接:link
3、Node
官网下载,链接:link

二、开发环境部署

1、安装node

node安装成功后,需要配置node-gyp工作路径。找到安装后文件夹“***\node_modules\npm\bin\node-gyp-bin”目录,将该目录设置添加到环境变量。任一路径下打开终端窗口,输入node-gyp,能识别到该指令即!为安装成功。
在这里插入图片描述

2、安装Python和Visual Studio

2.1、使用node-gyp configure build进行编译时,报错

Python is not set from command line or npm configuration

下载链接:link
安装时选择自定义安装,勾选将Python添加到环境变量。
在这里插入图片描述
在这里插入图片描述

2.2、安装Python后,再次使用node-gyp configure build进行编译,报错

gyp info it worked if it ends with ok
gyp info using node-gyp@10.1.0
gyp info using node@18.20.4 | win32 | ia32
gyp info find Python using Python version 3.12.3 found at "E:\Python\setup\python.exe"
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

可以看到Python已经安装成功且能识别到工作路径,报错信息是提示需要安装Visual Studio。下载链接:link
安装时需要添加工作负荷中的“C++桌面开发",添加后点击继续,等待自动下载安装完成即可。
到这一步,所需要的软件及插件已经全部安装完成, 全部安装无误后可以进行napi环境测试。安装过程中遇到的部分问题,记录在第三章。

三、报错提示&解决方法

1、使用node-gyp指令报错,提示“不是内部或外部命令”。

阶段一:

出现此报错一般有是两种原因:
1)node-gyp没有安装成功;
2)没有设置node-gyp全局路径。
开始考虑是node安装时自带安装node-gyp没有成功,找到安装目录下发现有node-gyp目录,如下:
在这里插入图片描述

于是在系统变量中添加了node-gyp路径:E:\Node\steup\node_modules\npm\node_modules\node-gyp 添加完成后,使用node-gyp指令还是报同样的错误。

后来在自己笔记本上重装后发现到这一步就可以解决问题了,原因是设置的路径有问题,实际应将环境变量里的路径设置为:E:\Node\steup\node_modules\npm\bin\node-gyp-bin。至此可跳过踩坑的阶段二和阶段三。

阶段二:

继续搜索问题,有帖子说可以通过-g全局安装的方法解决,使用指令:npm install -g node-gyp,安装成功后由于不了解全局安装原理,以为是覆盖E盘的安装目录,于是又走了一遍阶段一的坑。。。
在另一个帖子上发现安装成功后提示的有C盘的路径,在电脑C盘同样路径下果然找到了,如下:
在这里插入图片描述

于是把C盘该目录:***AppData\Roaming\npm\node_modules\node-gyp\node_modules添加到系统变量,输入指令,结果还是同样的报错。。。

阶段三:

此时问题仍然没有解决,不过可以确定的是node-gyp是安装好了的,那么大概率还是环境变量设置的有问题。然后就在“此电脑”搜node-gyp,可以找到在*AppData\Roaming\npm目录下有个node-gyp文件,将此文件目录添加到PATH中,问题得到解决。

2、使用npm install指令时,报错CERT_HAS_EXPIRED

关闭npm的https:
npm config set strict-ssl false

再重新安装:
npm install echart --save

参考:https://blog.csdn.net/weixin_43896145/article/details/136175349

3、安装Vidual Studio时提示没有安装VC++工作集

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, try re-running with '--loglevel silly' for more details.
gyp ERR! find VS
gyp ERR! find VS Failure details: undefined
gyp ERR! find VS checking VS2022 (17.10.35122.118) found at:
gyp ERR! find VS "I:\Visual Studio\Community"
gyp ERR! find VS - found "Visual Studio C++ core features"
gyp ERR! find VS - missing any VC++ toolset
gyp ERR! find VS could not find a version of Visual Studio 2017 or newer to use
gyp ERR! find VS could not use PowerShell to find Visual Studio 2017 or newer, try re-running with '--loglevel silly' for more details.
gyp ERR! find VS
gyp ERR! find VS Failure details: undefined
gyp ERR! find VS unsupported version "17.10.35122.118" found at "I:\Visual Studio\Community"
gyp ERR! find VS could not find a version of Visual Studio 2017 or newer to use
gyp ERR! find VS not looking for VS2015 as it is only supported up to Node.js 18
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

从报错信息中可以看出,此时配置的环境变量已经生效,提示信息是缺少VC++工作集,需要安装 “Desktop development with C++” 的工作负载。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值