windows离线安装node-gyp
作者:互联网
windows离线安装node-gyp
如需转载请标明出处:http://blog.csdn.net/itas109
QQ技术交流群:129518033
文章目录
环境(2021-04-02):
NodeJS : v14.16.0
系统 : windows 10 20H2
node-gyp : v7.1.2
Python:3.8.8 32bit
Visual Studio: 2015
前言
node-gyp是node C++模块编译的重要工具,目前的安装方式主要是在线安装。但是,离线安装的需要也是不可避免的,下面介绍如何在Windows下离线安装node-gyp
1.安装条件
windows系统:
- Python v3.6, v3.7, v3.8, v3.9
- C/C++编译器, 如 Visual Studio Community 2015
Python3.8.8下载地址:
https://www.python.org/ftp/python/3.8.8/python-3.8.8.exe
2.安装NodeJS
https://npm.taobao.org/mirrors/node/v14.16.0/node-v14.16.0-x64.msi
3.下载头文件node-v14.16.0-headers
头文件下载地址:
https://npm.taobao.org/mirrors/node/v14.16.0/node-v14.16.0-headers.tar.gz
https://nodejs.org/dist/v14.16.0/node-v14.16.0-headers.tar.gz
将node-v14.16.0-headers.tar.gz解压到%USERPROFILE%/AppData/Local/node-gyp/Cache
,并重命名为14.16.0
# 创建所需文件夹(%USERPROFILE%为当前用户目录)
$ cd %USERPROFILE%/AppData/Local
$ mkdir node-gyp
$ cd node-gyp
$ mkdir Cache
# 解压头文件
$ tar -xzf node-v14.16.0-headers.tar.gz -C %USERPROFILE%/AppData/Local/node-gyp/Cache
# 修改文件夹名称为v14.16.0
$ cd %USERPROFILE%/AppData/Local/node-gyp/Cache
$ rename node-v14.16.0 14.16.0
目录结构如下:
$ C:
$ cd %USERPROFILE%/AppData/Local/node-gyp/Cache/14.16.0
$ tree
.
+--- include
| +--- node
| | +--- node_api.h
| | +--- ...
4. 下载动态库node.lib
- win-64
https://npm.taobao.org/mirrors/node/v14.16.0/win-x64/node.lib
https://nodejs.org/dist/v14.16.0/win-x64/node.lib
- win-x86
https://npm.taobao.org/mirrors/node/v14.16.0/win-x86/node.lib
https://nodejs.org/dist/v14.16.0/win-x86/node.lib
64位的node.lib放到%USERPROFILE%/AppData/Local/node-gyp/Cache/14.16.0/x64
32位的node.lib放到%USERPROFILE%/AppData/Local/node-gyp/Cache/14.16.0/ia32
目录结构如下:
$ C:
$ cd %USERPROFILE%/AppData/Local/node-gyp/Cache/14.16.0
$ tree
.
+--- arm64
+--- ia32
| +--- node.lib
+--- include
| +--- node
| | +--- node_api.h
| | +--- ...
+--- x64
| +--- node.lib
5. 添加node-gyp对应的installVersion
node-gyp编译时会检测头文件和库文件的installVersion的版本,如果头文件和库文件的installVersion小于node-gyp的installVersion,则会重新下载安装头文件和库文件。
- 查看node-gyp的installVersion
%USERPROFILE%/AppData/Roaming/npm/node_modules/node-gyp/package.json
可以看到node-gyp的installVersion是9
{
"_from": "node-gyp",
"_id": "node-gyp@7.1.2",
...
"installVersion": 9,
...
"version": "7.1.2"
}
- 头文件同级目录新建文件installVersion内容为9
cd %USERPROFILE%/AppData/Local/node-gyp/Cache/14.16.0
echo 9 installVersion
目录结构如下:
$ C:
$ cd %USERPROFILE%/AppData/Local/node-gyp/Cache/14.16.0
$ tree
.
+--- arm64
+--- ia32
| +--- node.lib
+--- include
| +--- node
| | +--- node_api.h
| | +--- ...
+--- installVersion
+--- x64
| +--- node.lib
6.安装node-gyp
下载地址:
https://github.com/nodejs/node-gyp/archive/v7.1.2.tar.gz
将node-gyp-7.1.2.tar.gz解压到 %USERPROFILE%/AppData/Roaming/npm/node_modules(NodeJS安装路径
),并重命名为node-gyp
$ tar -xzf node-gyp-7.1.2.tar.gz -C %USERPROFILE%/AppData/Roaming/npm/node_modules
$ rename node-gyp-7.1.2 node-gyp
安装必要node_modules(在线安装好拷贝到离线环境)
$ cd node-gyp
$ npm install
验证
$ cd bin
$ node %USERPROFILE%/AppData/Roaming/npm/node_modules/node-gyp/bin/node-gyp.js -v
v7.1.2
node-gyp命令文件
在%USERPROFILE%/AppData/Roaming/npm建立node-gyp.cmd文件,用于直接使用node-gyp命令。也就是上一步的验证可以使用node-gyp -v
即可。
- node-gyp.cmd
@ECHO off
SETLOCAL
CALL :find_dp0
IF EXIST "%dp0%\node.exe" (
SET "_prog=%dp0%\node.exe"
) ELSE (
SET "_prog=node"
SET PATHEXT=%PATHEXT:;.JS;=;%
)
"%_prog%" "%dp0%\node_modules\node-gyp\bin\node-gyp.js" %*
ENDLOCAL
EXIT /b %errorlevel%
:find_dp0
SET dp0=%~dp0
EXIT /b
- node-gyp.ps1
#!/usr/bin/env pwsh
$basedir=Split-Path $MyInvocation.MyCommand.Definition -Parent
$exe=""
if ($PSVersionTable.PSVersion -lt "6.0" -or $IsWindows) {
# Fix case when both the Windows and Linux builds of Node
# are installed in the same directory
$exe=".exe"
}
$ret=0
if (Test-Path "$basedir/node$exe") {
& "$basedir/node$exe" "$basedir/node_modules/node-gyp/bin/node-gyp.js" $args
$ret=$LASTEXITCODE
} else {
& "node$exe" "$basedir/node_modules/node-gyp/bin/node-gyp.js" $args
$ret=$LASTEXITCODE
}
exit $ret
- node-gyp
#!/bin/sh
basedir=$(dirname "$(echo "$0" | sed -e 's,\\,/,g')")
case `uname` in
*CYGWIN*|*MINGW*|*MSYS*) basedir=`cygpath -w "$basedir"`;;
esac
if [ -x "$basedir/node" ]; then
"$basedir/node" "$basedir/node_modules/node-gyp/bin/node-gyp.js" "$@"
ret=$?
else
node "$basedir/node_modules/node-gyp/bin/node-gyp.js" "$@"
ret=$?
fi
exit $ret
License
License under CC BY-NC-ND 4.0: 署名-非商业使用-禁止演绎
Refrence:
- https://github.com/nodejs/node-gyp
标签:node,AppData,windows,离线,v14.16,+---,USERPROFILE%,gyp 来源: https://blog.csdn.net/itas109/article/details/115408762