如需转载请标明出处: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
git clone https://github.com/itas109/node-gyp-offline-install
或
git clone https://gitee.com/itas109/node-gyp-offline-install
下载windows离线包
$ node index.js -p win32 -a x64 -v 16.14.0 -m https://npm.taobao.org/mirrors/node/
下载linux离线包
$ node index.js -p linux -a x64 -v 16.14.0 -m https://npm.taobao.org/mirrors/node/
node-gyp是node C++模块编译的重要工具,目前的安装方式主要是在线安装。但是,离线安装的需要也是不可避免的,下面介绍如何在Windows下离线安装node-gyp
windows系统:
Python3.8.8下载地址:
https://www.python.org/ftp/python/3.8.8/python-3.8.8.exe
https://npm.taobao.org/mirrors/node/v14.16.0/node-v14.16.0-x64.msi
头文件下载地址:
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
| | +--- ...
https://npm.taobao.org/mirrors/node/v14.16.0/win-x64/node.lib
https://nodejs.org/dist/v14.16.0/win-x64/node.lib
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
node-gyp编译时会检测头文件和库文件的installVersion的版本,如果头文件和库文件的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"
}
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
下载地址:
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
即可。
@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
#!/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
#!/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: