Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeError: Cannot read property 'app' of undefined #20

Closed
webdevpchoi opened this issue Nov 21, 2019 · 6 comments
Closed

TypeError: Cannot read property 'app' of undefined #20

webdevpchoi opened this issue Nov 21, 2019 · 6 comments

Comments

@webdevpchoi
Copy link

Trying to get concurrently + electron + create-react-app working nicely together. However, upon launching Electron, I am getting an error that states TypeError: Cannot read property 'app' of undefined from node_modules\electron-is-dev\index.js.

It's pointing to the const app = electron.app || electron.remote.app;.

Here's my package.json:

{
  "name": "ootl",
  "version": "0.1.0",
  "private": true,
  "main": "src/start.js",
  "dependencies": {
    "electron-is-dev": "^1.1.0",
    "react": "^16.11.0",
    "react-dom": "^16.11.0",
    "react-scripts": "3.2.0"
  },
  "homepage": "./",
  "build": {
    "appId": "ootl",
    "directories": {
      "buildResources": "assets"
    }
  },
  "scripts": {
    "dev": "nodemon --watch . --exec \"electron . --debug\"",
    "start": "concurrently \"react-scripts start\" \"nodemon --watch . --exec\" \"electron . --debug\"",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": "react-app"
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  },
  "devDependencies": {
    "electron": "^7.1.2",
    "electron-builder": "^22.1.0",
    "nodemon": "^1.19.4"
  }
}

I'm using the start script to run the project.
Here's my start.js files for Electron:

const electron = require("electron");
const app = electron.app;
const path = require("path");
const BrowserWindow = electron.BrowserWindow;
const isDev = require("electron-is-dev");

let mainWindow;

function createWindow() {
  mainWindow = new BrowserWindow({
    width: 800,
    height: 600,
    webPreferences: {
      nodeIntegration: true
    }
  });

  mainWindow.loadURL(
    isDev
      ? "http://localhost:3000"
      : `file://${path.join(__dirname, "../build/index.html")}`
  );

  mainWindow.on("closed", () => {
    mainWindow = null;
  });
}

app.on("ready", createWindow);

app.on("window-all-closed", () => {
  if (process.platform !== "darwin") {
    app.quit();
  }
});

app.on("activate", () => {
  if (mainWindow === null) {
    createWindow();
  }
});

I have Electron installed locally, and not globally, if it makes a difference. Would love to know why const app = electron.app || electron.remote.app; doesn't resolve.

@sindresorhus
Copy link
Owner

TypeError: Cannot read property 'app' of undefined

The only case this could happen is if electron is not available or is incorrectly mocked or shimmed. Or if BrowserWindow#webPreferences.enableRemoteModule is set to false, which looks like it's not the case.

Could you add console.log('App:', electron.app); console.log('Remote', electron.remote); to your start.js and share the output?

@talothman
Copy link

Find a solution for this? I'm getting the same issue.

@pbmzero
Copy link

pbmzero commented Feb 20, 2020

I believe electron.remote has been deprecated but it is referenced by the node_modules\electron-is-dev\index.js Please see the following reference.. I recently had to remove electron-is-dev from my project when attempting to test out the latest Electron 10 nightly build. I'm not sure if there is an alternative, but it seems to be a major dependency for this project that has broke.

Edit: Corrected Link

@masteroleary
Copy link

This may be unrelated but I found this thread because I had the same error when trying to run a new quasar electron project out of the box. I found that I needed to change "quasar dev -m electron" to "sudo quasar dev -m electron" and run as administrator. It then worked immediately.

@jinshaopu
Copy link

enableRemoteModule=true

@nobeans
Copy link

nobeans commented Dec 12, 2020

I've gotten same issue. But I fixed it thanks for the comment of @masteroleary. My issue occured by ELECTRON_RUN_AS_NODE. If it set like ELECTRON_RUN_AS_NODE=true, the error would occur. sudo runs a command in differnt environment variables from general user shell. I compared them, then I've found the different: ELECTRON_RUN_AS_NODE. (However I don't remember why the environment variable set...)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants