electron: [v0.36.2 mac] require('electron').app undefined

require('electron').app not work but require('app') is okay. Just tried https://github.com/atom/electron-quick-start , and also got same error.

seems require(‘electron’).* all missing.

change code to this:

'use strict';

const electron = require('electron');
// Module to control application life.
const app = electron.app;

console.log(app);

// Module to create native browser window.
const BrowserWindow = electron.BrowserWindow;

got following result:

$ npm start

> electron-quick-start@1.0.0 start /Users/hialanMacAir/Work/electron-quick-start
> electron main.js

undefined
App threw an error when running [TypeError: Cannot read property 'on' of undefined]

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Reactions: 9
  • Comments: 38 (7 by maintainers)

Most upvoted comments

Hi maxfarseer, you can access the “app” using require(“electron”).remote.app. Using the example you referenced:

{
  label: "Quit",
  accelerator: "Command+Q",
  click: function() {
    require("electron").remote.app.quit();
  }
}

@maxfarseer

try, writing

const electron = require(‘electron’) var remote = require(‘electron’).remote const app = remote.app

in rederer.js

As of today (late Aug 2017), I find that electron.appworks from the main process, but not from a renderer process, and electron.remote.app works from a renderer process, but not from the main process. For example:

const electron = require('electron');
const app = (process.type === 'renderer') ? electron.remote.app : electron.app;

Do not install node-electron ( https://www.npmjs.com/package/electron ) which is belong to another project. Just remove it and it will work.

npm remove electron (for local) npm remove electron -g (for global)

I don’t understand how this fix removing electron locally and globally can work, so how we are going to use “npm start” or run the app after this? :S I’m having this issue.

what is require(‘app’) and why removed?

These modules are no longer available directly via require in order to prevent name collisions with other third party modules as discussed in #387.

i am using require(‘electron’) but i have some old project that using require(‘app’) in it and i got error

how to enable this feature?

Electron v0.35.0 started issuing deprecation warnings about require('app'), and Electron v1.0.0 removed the ability to require('app') completely, so if you don’t want to update your old code downgrade to the last release before v1.0.0.

I’m sorry to revive this, but I am having this issue on Mac OSX Sierra as well.

Here’s my environment:

  • no global electron installed (only local, via the electron package)

package.json

...
"main": "index.js",
"devDependencies": {
    "electron": "^1.4.6"
}
...

index.js

console.log(require.resolve('electron'))
console.log(require('electron'))

Then I run:

$ ./node_modules/.bin/electron index.js
.../node_modules/electron/index.js
.../node_modules/electron/dist/Electron.app/Contents/MacOS/Electron

What is the solution to resolve this?

@Pablodotnet I suggest posting your issue in https://discuss.atom.io/c/electron along with the full error information.

Yes I know that, that’s how I’m running the app and I have electron installed there in my app folder with npm install electron. But you are saying the error I’m having of the ‘on’ issue solves removing locally and globally so doing that, the script will not run because electron isnt installed neither globally and locally

@trusktr Sounds like you’re running node src/index.js instead of electron src/index.js

Either that or the ELECTRON_RUN_AS_NODE env var is set 🤔

I solve in my case changing the package.json was:

“scripts”: { “test”: “echo “Error: no test specified” && exit 1”, “start”: “node index.js” },

and I changed to:

“scripts”: { “start”: “electron .” },

Waoh, Very Thanks Point this Out!!!

@jinfagang const {remote} = require('electron').remote is the equivalent of const remote = require('electron').remote.remote, you need to use const {remote} = require('electron').

Im sorry if I didn’t explain well the first time, I’m having the same problem as hialan was having,

App threw an error when running [TypeError: Cannot read property 'on' of undefined]

When running npm start, the console in the electron app keeps giving me that error.

@Pablodotnet this doesn’t really have anything to do with installing electron locally or globally. But if you install Electron locally you can start your app with npm start changing/adding the start NPM script to your package.json and set it to run electron . or electron path/to/main.js, e.g.

"scripts": {
  "start": "electron ."
}