我正在使用一个使用Electron的“0.34.3”版本的Node.js应用程序。
我遇到的问题是,当我尝试在渲染器过程中包含'电子'模块时,如下require('electron').remote;
和当我npm start
时 - 我收到以下错误:
{ [Error: Cannot find module 'electron' from '/Users/waley/code/PROJECT/src/connect']
stream:
Labeled {
_readableState:
ReadableState {
objectMode: true,
highWaterMark: 16,
buffer: [],
length: 0,
pipes: [Object],
pipesCount: 1,
flowing: true,
ended: false,
endEmitted: false,
reading: true,
sync: false,
needReadable: true,
emittedReadable: false,
readableListening: false,
defaultEncoding: 'utf8',
ranOut: false,
awaitDrain: 0,
readingMore: false,
decoder: null,
encoding: null,
resumeScheduled: false },
readable: true,
domain: null,
_events:
{ end: [Object],
error: [Object],
data: [Function: ondata],
_mutate: [Object] },
_eventsCount: 4,
_maxListeners: undefined,
_writableState:
WritableState {
objectMode: true,
highWaterMark: 16,
needDrain: false,
ending: true,
ended: true,
finished: true,
decodeStrings: true,
defaultEncoding: 'utf8',
length: 0,
writing: false,
corked: 0,
sync: false,
bufferProcessing: false,
onwrite: [Function],
writecb: null,
writelen: 0,
bufferedRequest: null,
lastBufferedRequest: null,
pendingcb: 0,
prefinished: true,
errorEmitted: false },
writable: true,
allowHalfOpen: true,
_options: { objectMode: true },
_wrapOptions: { objectMode: true },
_streams: [ [Object] ],
length: 1,
label: 'deps' } }
[11:36:40] js error Cannot find module 'electron' from '/Users/waley/code/PROJECT/src/connect
知道怎么了?谢谢!
有几种方法可以解决有关API Changes Coming in Electron 1.0的电子模块导入问题。
请注意,这通常发生在像webpack这样的捆绑器上,它会覆盖require
函数。
target
property如果您使用最新版本的Webpack作为捆绑包,请添加
target: 'electron-renderer'
你的配置应该让你使用:
import 'electron' from electron;
electron
outside of your build<!-- electron declaration -->
<script>
const electron = require('electron');
</script>
<!-- your app build -->
<script src="dist/bundle.js"></script>
这样,我可以从任何地方访问electron
。
window.require
电子扩展了window
对象,以便您可以使用:
const electron = window.require('electron');
var remote = require('remote');
var app = remote.app; // to import the app module, for example
当我忘记在脚本之前的某处将"main": "./main.js",
添加到package.json
时,我收到此错误。如需完整设置,请遵循这个伟大的tutorial
编辑:
以下是该链接的摘要:
安装电子
npm install electron --save-dev
更新index.html
Angular中生成的根页面将基本href指向/ - 这将导致Electron稍后出现问题,所以让我们现在更新它。只需在src / index.html中的斜杠前面添加一个句点。
<base href="./">
配置电子
在项目的根目录中创建一个名为main.js
的新文件(与package.json
处于同一级别) - 这是Electron NodeJS后端。这是Electron的切入点,定义了我们的桌面应用程序如何对通过桌面操作系统执行的各种事件做出反应。
const { app, BrowserWindow } = require('electron')
let win;
function createWindow () {
// Create the browser window.
win = new BrowserWindow({
width: 600,
height: 600,
backgroundColor: '#ffffff',
icon: `file://${__dirname}/dist/assets/logo.png`
})
win.loadURL(`file://${__dirname}/dist/index.html`)
//// uncomment below to open the DevTools.
// win.webContents.openDevTools()
// Event when the window is closed.
win.on('closed', function () {
win = null
})
}
// Create window on electron intialization
app.on('ready', createWindow)
// Quit when all windows are closed.
app.on('window-all-closed', function () {
// On macOS specific close process
if (process.platform !== 'darwin') {
app.quit()
}
})
app.on('activate', function () {
// macOS specific close process
if (win === null) {
createWindow()
}
})
将main.js
和自定义脚本添加到package.json
。你的package.json
应该是这样的:
{
"name": "angular-electron",
"version": "0.0.0",
"license": "MIT",
"main": "main.js", // <-- update here
"scripts": {
"ng": "ng",
"start": "ng serve",
"build": "ng build",
"test": "ng test",
"lint": "ng lint",
"e2e": "ng e2e",
"electron": "electron .", // <-- run electron
"electron-build": "ng build --prod && electron ." // <-- build app, then run electron
},
// ...omitted
}
运行命令以构建和启动电子
npm run electron-build