Electron内置模块和npm安装的模块有什么区别吗?如何从其他模块访问电子对象?
Is there any difference between Electron built-in module and the one installed with npm? How to access to the electron object from other modules?
文档
如果你勾选 this electron installation manual,你会发现你应该这样安装 electron:
npm install electron --save-dev
所以我做到了。但是如果你检查这个 other document 他们会说:
When using Electron's built-in module you might encounter an error like this:
> require('electron').webFrame.setZoomFactor(1.0)
Uncaught TypeError: Cannot read property 'setZoomLevel' of undefined
This is because you have the npm electron module installed either locally or globally, which overrides Electron's built-in module.
我不知道 "locally" 是否是这样的意思(没有 --save-dev
):
npm install electron
解析电子
检查安装是否正确:
To verify whether you are using the correct built-in module, you can print the path of the electron module:
console.log(require.resolve('electron'))
and then check if it is in the following form:
"/path/to/Electron.app/Contents/Resources/atom.asar/renderer/api/lib/exports/electron.js"
If it is something like node_modules/electron/index.js, then you have to either remove the npm electron module, or rename it.
我申请的结果是
...\app_folder\node_modules\electron\dist\resources\electron.asar\browser\api\exports\electron.js
问题
我可以从 main.js
文件访问电子对象。这工作正常:
const {app} = require('electron');
但是如果我在其他 js 文件中执行此操作(我需要来自 main.js 的这些文件),我会得到一个未定义的值。这是正常的吗?我是否需要将电子对象作为参数发送给这些其他模块?
他们也这么说,但我正在考虑:
However if you are using the built-in module but still getting this error, it is very likely you are using the module in the wrong process. For example electron.app can only be used in the main process, while electron.webFrame is only available in renderer processes.
此文档是否仍然是最新的?我应该如何安装Electron才能使内置模块工作?
具体问题(更新)
实际上如果我在另一个模块中这样做
const electron = require('electron');
console.log(electron)
console.log(electron.app)
对象被打印:
{ clipboard: [Getter],
crashReporter: [Getter],
nativeImage: [Getter],
shell: [Getter],
app: [Getter],
autoUpdater: [Getter],
BrowserView: [Getter],
BrowserWindow: [Getter],
contentTracing: [Getter],
dialog: [Getter],
globalShortcut: [Getter],
ipcMain: [Getter],
inAppPurchase: [Getter],
Menu: [Getter],
MenuItem: [Getter],
net: [Getter],
netLog: [Getter],
Notification: [Getter],
powerMonitor: [Getter],
powerSaveBlocker: [Getter],
protocol: [Getter],
screen: [Getter],
session: [Getter],
systemPreferences: [Getter],
TopLevelWindow: [Getter],
TouchBar: [Getter],
Tray: [Getter],
View: [Getter],
webContents: [Getter],
WebContentsView: [Getter] }
App {
_events:
{ login: [Function],
'certificate-error': [Function],
'select-client-certificate': [Function],
quit: [Function],
'web-contents-created': [Function],
'session-created': [Function],
'will-quit': [Function],
ready: [ [Function], [Function] ],
'window-all-closed': [Function] },
_eventsCount: 9,
_maxListeners: undefined,
whenReady: [Function: whenReady],
setApplicationMenu: [Function: setApplicationMenu],
getApplicationMenu: [Function: getApplicationMenu],
commandLine:
{ appendSwitch: [Function: appendSwitch],
appendArgument: [Function: appendArgument] },
getAppMetrics: [Function],
isPackaged: false,
allowNTLMCredentialsForAllDomains: [Function],
releaseSingleInstance: [Function],
makeSingleInstance: [Function] }
但是如果我尝试获取用户数据路径
const __user_data = electron.app.getPath('userData');
我收到这个错误:
Cannot read property 'getPath' of undefined
我想知道为什么会这样,因为应用程序存在,但如果我 运行 app.getPath()
应用程序不再存在。 electron.remote
也发生了类似的事情,我也试过了,即使这是在主进程中。
除了安装路径的疑惑,我已经解决了。我在我的应用程序的不同地方需要这个文件。有时我从主进程调用它,在其他情况下从渲染器进程调用它。所以我必须这样做来支持这两种情况:
var app = null;
if (typeof(electron.remote) !== 'undefined') {
app = electron.remote.app;
} else {
app = electron.app
}
const __user_data = app.getPath('userData');
文档
如果你勾选 this electron installation manual,你会发现你应该这样安装 electron:
npm install electron --save-dev
所以我做到了。但是如果你检查这个 other document 他们会说:
When using Electron's built-in module you might encounter an error like this:
> require('electron').webFrame.setZoomFactor(1.0) Uncaught TypeError: Cannot read property 'setZoomLevel' of undefined
This is because you have the npm electron module installed either locally or globally, which overrides Electron's built-in module.
我不知道 "locally" 是否是这样的意思(没有 --save-dev
):
npm install electron
解析电子
检查安装是否正确:
To verify whether you are using the correct built-in module, you can print the path of the electron module:
console.log(require.resolve('electron'))
and then check if it is in the following form:
"/path/to/Electron.app/Contents/Resources/atom.asar/renderer/api/lib/exports/electron.js"
If it is something like node_modules/electron/index.js, then you have to either remove the npm electron module, or rename it.
我申请的结果是
...\app_folder\node_modules\electron\dist\resources\electron.asar\browser\api\exports\electron.js
问题
我可以从 main.js
文件访问电子对象。这工作正常:
const {app} = require('electron');
但是如果我在其他 js 文件中执行此操作(我需要来自 main.js 的这些文件),我会得到一个未定义的值。这是正常的吗?我是否需要将电子对象作为参数发送给这些其他模块?
他们也这么说,但我正在考虑:
However if you are using the built-in module but still getting this error, it is very likely you are using the module in the wrong process. For example electron.app can only be used in the main process, while electron.webFrame is only available in renderer processes.
此文档是否仍然是最新的?我应该如何安装Electron才能使内置模块工作?
具体问题(更新)
实际上如果我在另一个模块中这样做
const electron = require('electron');
console.log(electron)
console.log(electron.app)
对象被打印:
{ clipboard: [Getter],
crashReporter: [Getter],
nativeImage: [Getter],
shell: [Getter],
app: [Getter],
autoUpdater: [Getter],
BrowserView: [Getter],
BrowserWindow: [Getter],
contentTracing: [Getter],
dialog: [Getter],
globalShortcut: [Getter],
ipcMain: [Getter],
inAppPurchase: [Getter],
Menu: [Getter],
MenuItem: [Getter],
net: [Getter],
netLog: [Getter],
Notification: [Getter],
powerMonitor: [Getter],
powerSaveBlocker: [Getter],
protocol: [Getter],
screen: [Getter],
session: [Getter],
systemPreferences: [Getter],
TopLevelWindow: [Getter],
TouchBar: [Getter],
Tray: [Getter],
View: [Getter],
webContents: [Getter],
WebContentsView: [Getter] }
App {
_events:
{ login: [Function],
'certificate-error': [Function],
'select-client-certificate': [Function],
quit: [Function],
'web-contents-created': [Function],
'session-created': [Function],
'will-quit': [Function],
ready: [ [Function], [Function] ],
'window-all-closed': [Function] },
_eventsCount: 9,
_maxListeners: undefined,
whenReady: [Function: whenReady],
setApplicationMenu: [Function: setApplicationMenu],
getApplicationMenu: [Function: getApplicationMenu],
commandLine:
{ appendSwitch: [Function: appendSwitch],
appendArgument: [Function: appendArgument] },
getAppMetrics: [Function],
isPackaged: false,
allowNTLMCredentialsForAllDomains: [Function],
releaseSingleInstance: [Function],
makeSingleInstance: [Function] }
但是如果我尝试获取用户数据路径
const __user_data = electron.app.getPath('userData');
我收到这个错误:
Cannot read property 'getPath' of undefined
我想知道为什么会这样,因为应用程序存在,但如果我 运行 app.getPath()
应用程序不再存在。 electron.remote
也发生了类似的事情,我也试过了,即使这是在主进程中。
除了安装路径的疑惑,我已经解决了。我在我的应用程序的不同地方需要这个文件。有时我从主进程调用它,在其他情况下从渲染器进程调用它。所以我必须这样做来支持这两种情况:
var app = null;
if (typeof(electron.remote) !== 'undefined') {
app = electron.remote.app;
} else {
app = electron.app
}
const __user_data = app.getPath('userData');