npm install 失败,node-gyp rebuild 没有日志输出,不好排查原因
watchingfun opened this issue · 2 comments
watchingfun commented
这是我修改过后的rebuild.js,因为install最后报错,但是看日志并没有具体执行过程的输出,找不到问题,因为我对nodejs并不熟悉,我网上找了办法去显示子线程的日志,然后才找到rebuild失败的原因;
const path = require('path');
const child = require('child_process');
function execute(cmd, callback, cwd = process.cwd()) {
return new Promise((resolve, reject) => {
let items = cmd
let exe = items.shift()
let processor = child.spawn(exe, items, { cwd })
let collect = () => {
let str = ''
let print = () => {
let lines = str.split(/[\n|\r\n]/)
str = lines.pop()
let contents = lines.join('\r\n')
if (str === '') { // 如果本来内容就是完整断句的,这里进行修正,表示结尾是正常断句的
contents += '\r\n'
}
callback(contents)
}
return (data) => {
str += data.toString()
print()
}
}
if (typeof callback === 'function') {
processor.stdout.on('data', collect())
processor.stderr.on('data', collect())
}
processor.on('exit', code => {
if (code === 0) {
console.log('Rebuild better-sqlite3 success.');
}
process.exit(code);
});
processor.on('error', reject)
processor.on('close', resolve)
})
}
// If you prefer electron-rebuild:
// 👉 https://github.com/WiseLibs/better-sqlite3/blob/v8.5.2/docs/troubleshooting.md#electron
// 👉 https://stackoverflow.com/questions/46384591/node-was-compiled-against-a-different-node-js-version-using-node-module-versio/52796884#52796884
const better_sqlite3 = require.resolve('better-sqlite3');
const better_sqlite3_root = path.posix.join(better_sqlite3.slice(0, better_sqlite3.lastIndexOf('node_modules')), 'node_modules/better-sqlite3');
// const cp = child.spawn(
// process.platform === 'win32' ? 'npm.cmd' : 'npm',
// [
// 'run',
// 'build-release',
// `--target=${process.versions.electron}`,
// // https://github.com/electron/electron/blob/v26.1.0/docs/tutorial/using-native-node-modules.md#manually-building-for-electron
// '--disturl=https://electronjs.org/headers',
// ],
// {
// cwd: better_sqlite3_root,
// stdio: 'inherit',
// },
// );
// cp.on('exit', code => {
// if (code === 0) {
// console.log('Rebuild better-sqlite3 success.');
// }
// process.exit(code);
// });
const cmd = [
'win32' ? 'npm.cmd' : 'npm',
'run',
'build-release',
`--target=${process.versions.electron}`,
// https://github.com/electron/electron/blob/v26.1.0/docs/tutorial/using-native-node-modules.md#manually-building-for-electron
'--distUrl=https://electronjs.org/headers',
]
execute(cmd, (contents) => console.log(contents), better_sqlite3_root).catch(e => console.log(e))
顺便给其他新手说下我出现的问题和解决方法:
- 需要安装Visual studio (工作负荷勾选安装 使用c++的桌面开发),Python
- .npmrc 添加
electron-mirror=https://npmmirror.com/mirrors/electron/
msvs_version=2022
//根据你自己的安装版本和路径来
msbuild_path=F:\Program Files\Microsoft Visual Studio\2022\Community\MSBuild\Current\Bin\MSBuild.exe
- node-gyp是npm内置了的,不需要再去npm install -g node-gyp, 项目里使用的node-gyp不会使用你自己全局安装的;npm需要更新为最新,因为之前用的node-gyp v8.4.1不行,报错:
fatal error C1189: #error: "It looks like you are building this native module without using the right config.gypi. This normally means that you need to update electron-rebuild (>=3.2.8) or node-gyp (>=9.0.0) if you're building modules directly."
; 只能更新为最新的npm才能成功执行;
aehyok commented
@watchingfun 我这个为什么报错了 1.2.3都配置了的
watchingfun commented
@watchingfun 我这个为什么报错了 1.2.3都配置了的
package.json 需要使用 "overrides" 覆盖依赖里的旧node-gyp版本