找不到模块'电子'

问题描述 投票:14回答:3

我正在使用一个使用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     

知道怎么了?谢谢!

node.js npm atom-editor electron
3个回答
17
投票

有几种方法可以解决有关API Changes Coming in Electron 1.0的电子模块导入问题。

请注意,这通常发生在像webpack这样的捆绑器上,它会覆盖require函数。

Make use of Webpack's target property

如果您使用最新版本的Webpack作为捆绑包,请添加

target: 'electron-renderer'

你的配置应该让你使用:

import 'electron' from electron;

Declare electron outside of your build

<!-- electron declaration -->
<script>
    const electron = require('electron');
</script>

<!-- your app build -->
<script src="dist/bundle.js"></script>

这样,我可以从任何地方访问electron

Use the window.require

电子扩展了window对象,以便您可以使用:

const electron = window.require('electron');

Use the old way (still supported)

var remote = require('remote');
var app    = remote.app; // to import the app module, for example

10
投票

运行此命令:

npm install --save-dev electron

了解更多详情click here


0
投票

当我忘记在脚本之前的某处将"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
© www.soinside.com 2019 - 2024. All rights reserved.