找不到模块“电子”

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

任何想法是什么? 谢谢!

       

网上收集的解决方案 "找不到模块“电子”"

有几种方法可以解决电子模块导入问题,其中涉及到电子1.0中的API更改 。

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

在你的构build之外声明electron

我通常把electron作为一个全局variables来声明:

 <!-- 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 

运行这个命令

npm install –save-dev电子

更多细节请点击这里