electron问题集

node-gyp addon问题

报错if not defined npm_config_node_gyp

原因:node安装时候的gyp版本太老,自动安装不会使用新下载的gyp  
解决:
- ```npm install -g node-gyp```或
- ```npm config set node_gyp "node C:\Users\me\AppData\Roaming\npm\node_modules\node-gyp\bin\node-gyp.js"```

其他

调用Debug版本动态库的时候崩溃

原因:不明
解决:换成release版本

Napi::Error.ThrowAsJavaScriptExcetion时崩溃

原因:抛出异常并不中断程序,后续还是会执行,导致程序崩溃
解决:抛出异常并退出函数

if (info.Length() < 1)
{
    Napi::TypeError::New(env, "wrong number of argument").ThrowAsJavaException();
    
    return env.Null();
}

提示jquery未定义

script前后加入

<!-- Insert this line above script imports  -->
<script>if (typeof module === 'object') {window.module = module; module = undefined;}</script>

<!-- normal script imports etc  -->
<script src="scripts/jquery.min.js"></script>    
<script src="scripts/vendor.js"></script>    

<!-- Insert this line after script imports -->
<script>if (window.module) module = window.module;</script>

提示require is not defined

原因:electron5版本问题
解决:
main.js创建窗口时加上: webPreferences: { nodeIntegration: true }

  win = new BrowserWindow({
    width: 800, 
    height: 600, 
    show: false, 
    webPreferences: {
      nodeIntegration: true
    }
  });

提示openExternal is not a function

原因:自己写错
应该是:
const {shell} = require('electron');而不是 const shell = require('electron');

引用fs模块时提示"cannot access ‘fs’ before initialization"

原因:引用fs模块之前文件有其他错误,导致并没有引用fs模块
解决:解决前面的问题。
注:fs模块不需要另外install

发布了8 篇原创文章 · 获赞 0 · 访问量 17

猜你喜欢

转载自blog.csdn.net/u014182377/article/details/105715861