Electron Node import returns "Undefined"

Asked

Viewed 80 times

0

I want to use Electron with Node, but when importing this framework following the step by step of tutorial of the site, it always returns undefined.

// Abaixo tanto o "app" quanto o "BrowserWindow" são undefined.
const { app, BrowserWindow } = require("electron"); 

I performed the installation of Electron using the command npm install electron. I tried to import everything to know what was returning and what I received was the absolute path to an executable called electron.exe.

// Retorna o seguinte path: "C:\\...\\MyFolder\\node_modules\\electron\\dist\\electron.exe"
const electron = require("electron");

What am I doing wrong? There was no error while installing the framework.

  • Are you executing the code via terminal (outside the IDE)? https://github.com/electron/electron/issues/8200#issuecomment-266923546

  • I am running the code via terminal. In the first example I ran through the node main.js and the second time I ran through the interactive mode of Node.

1 answer

3


Install Electron GLOBALLY like this:

npm install electron -g

Then go to your folder and run your command:

cd pasta/projeto
electron .

Your folder should contain files like:

seu-app/
├── package.json
├── main.js
└── index.html
  • I globally installed Electron and when I tried to run, it was giving an error message saying that it could not find the module. Then I saw the tree you put in the answer and realized I didn’t have the file package.json. So I had to create with the npm init. After that it worked.

  • 1

    But why do I need to install Electron globally ? What does it change ? And why do I need to have the file package.json to run the Electron ?

  • To create locally you would have to edit in package.json and add the command pointing to it, out that you have to install with the parameter --save-dev and you must have forgotten .... apart from the fact that there is no package.json problem enough, because it means that THERE WAS NOTHING installed.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.