Is there any difference between Electron built-in module and the one installed with npm? How to access to the...











up vote
2
down vote

favorite












Documentation



If you check this electron installation manual, you can read that you should install electron this way:



npm install electron --save-dev


So I did it. But if you check this other document they say:




When using Electron's built-in module you might encounter an error like this:



  > require('electron').webFrame.setZoomFactor(1.0)
Uncaught TypeError: Cannot read property 'setZoomLevel' of undefined


This is because you have the npm electron module installed either locally or globally, which overrides Electron's built-in module.




I do not know if with "locally" they mean like this (without --save-dev):



npm install electron


Resolve electron



To check if the installation is right:




To verify whether you are using the correct built-in module, you can print the path of the electron module:



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


and then check if it is in the following form:



  "/path/to/Electron.app/Contents/Resources/atom.asar/renderer/api/lib/exports/electron.js"


If it is something like node_modules/electron/index.js, then you have to either remove the npm electron module, or rename it.




The result in my application is



...app_foldernode_moduleselectrondistresourceselectron.asarbrowserapiexportselectron.js


Problem



I can access to the electron object from the main.js file. This is working fine:



const {app} = require('electron');


But if I do this in other js files (I require these files from main.js) I get an undefined value. Is this normal? Do I need to send the electron object as argument to these other modules?



They also say this, but I am taking into account:




However if you are using the built-in module but still getting this error, it is very likely you are using the module in the wrong process. For example electron.app can only be used in the main process, while electron.webFrame is only available in renderer processes.




Is this documentation still up to date? How should I install Electron to make work the built-in module?



Specific problem (Update)



Actually if I do in this other module



const electron = require('electron');
console.log(electron)
console.log(electron.app)


The objects are printed:



{ clipboard: [Getter],
crashReporter: [Getter],
nativeImage: [Getter],
shell: [Getter],
app: [Getter],
autoUpdater: [Getter],
BrowserView: [Getter],
BrowserWindow: [Getter],
contentTracing: [Getter],
dialog: [Getter],
globalShortcut: [Getter],
ipcMain: [Getter],
inAppPurchase: [Getter],
Menu: [Getter],
MenuItem: [Getter],
net: [Getter],
netLog: [Getter],
Notification: [Getter],
powerMonitor: [Getter],
powerSaveBlocker: [Getter],
protocol: [Getter],
screen: [Getter],
session: [Getter],
systemPreferences: [Getter],
TopLevelWindow: [Getter],
TouchBar: [Getter],
Tray: [Getter],
View: [Getter],
webContents: [Getter],
WebContentsView: [Getter] }

App {
_events:
{ login: [Function],
'certificate-error': [Function],
'select-client-certificate': [Function],
quit: [Function],
'web-contents-created': [Function],
'session-created': [Function],
'will-quit': [Function],
ready: [ [Function], [Function] ],
'window-all-closed': [Function] },
_eventsCount: 9,
_maxListeners: undefined,
whenReady: [Function: whenReady],
setApplicationMenu: [Function: setApplicationMenu],
getApplicationMenu: [Function: getApplicationMenu],
commandLine:
{ appendSwitch: [Function: appendSwitch],
appendArgument: [Function: appendArgument] },
getAppMetrics: [Function],
isPackaged: false,
allowNTLMCredentialsForAllDomains: [Function],
releaseSingleInstance: [Function],
makeSingleInstance: [Function] }


But if I try to get the user data path



const __user_data = electron.app.getPath('userData');


I get this error:



Cannot read property 'getPath' of undefined


I am wondering why is this happening because app exists, but if I run app.getPath() app is not existing anymore. A similar thing happens with electron.remote, I have tried taht as well, even this is in the main process.










share|improve this question
























  • app exists, its getPath which does not based on the metadata you printed above.
    – Daniel
    Nov 11 at 13:26










  • @Daniel If I print app in other place (main.js, the start point of the application where getPath works), the same metadata is showed :/. I am wondering now if app only can be instantiated once in the whole program
    – ChesuCR
    Nov 12 at 16:29










  • I have just realised, in spite of getting the error on the console (chromium console), I am getting the right path in the terminal console :O
    – ChesuCR
    Nov 12 at 17:16










  • Ah! I was calling the file from different processes, main an renderer, that was the mistake!! I have solved as I have written in my answer
    – ChesuCR
    Nov 12 at 17:31






  • 1




    Good work. Glad you got it fixed.
    – Daniel
    Nov 12 at 18:11















up vote
2
down vote

favorite












Documentation



If you check this electron installation manual, you can read that you should install electron this way:



npm install electron --save-dev


So I did it. But if you check this other document they say:




When using Electron's built-in module you might encounter an error like this:



  > require('electron').webFrame.setZoomFactor(1.0)
Uncaught TypeError: Cannot read property 'setZoomLevel' of undefined


This is because you have the npm electron module installed either locally or globally, which overrides Electron's built-in module.




I do not know if with "locally" they mean like this (without --save-dev):



npm install electron


Resolve electron



To check if the installation is right:




To verify whether you are using the correct built-in module, you can print the path of the electron module:



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


and then check if it is in the following form:



  "/path/to/Electron.app/Contents/Resources/atom.asar/renderer/api/lib/exports/electron.js"


If it is something like node_modules/electron/index.js, then you have to either remove the npm electron module, or rename it.




The result in my application is



...app_foldernode_moduleselectrondistresourceselectron.asarbrowserapiexportselectron.js


Problem



I can access to the electron object from the main.js file. This is working fine:



const {app} = require('electron');


But if I do this in other js files (I require these files from main.js) I get an undefined value. Is this normal? Do I need to send the electron object as argument to these other modules?



They also say this, but I am taking into account:




However if you are using the built-in module but still getting this error, it is very likely you are using the module in the wrong process. For example electron.app can only be used in the main process, while electron.webFrame is only available in renderer processes.




Is this documentation still up to date? How should I install Electron to make work the built-in module?



Specific problem (Update)



Actually if I do in this other module



const electron = require('electron');
console.log(electron)
console.log(electron.app)


The objects are printed:



{ clipboard: [Getter],
crashReporter: [Getter],
nativeImage: [Getter],
shell: [Getter],
app: [Getter],
autoUpdater: [Getter],
BrowserView: [Getter],
BrowserWindow: [Getter],
contentTracing: [Getter],
dialog: [Getter],
globalShortcut: [Getter],
ipcMain: [Getter],
inAppPurchase: [Getter],
Menu: [Getter],
MenuItem: [Getter],
net: [Getter],
netLog: [Getter],
Notification: [Getter],
powerMonitor: [Getter],
powerSaveBlocker: [Getter],
protocol: [Getter],
screen: [Getter],
session: [Getter],
systemPreferences: [Getter],
TopLevelWindow: [Getter],
TouchBar: [Getter],
Tray: [Getter],
View: [Getter],
webContents: [Getter],
WebContentsView: [Getter] }

App {
_events:
{ login: [Function],
'certificate-error': [Function],
'select-client-certificate': [Function],
quit: [Function],
'web-contents-created': [Function],
'session-created': [Function],
'will-quit': [Function],
ready: [ [Function], [Function] ],
'window-all-closed': [Function] },
_eventsCount: 9,
_maxListeners: undefined,
whenReady: [Function: whenReady],
setApplicationMenu: [Function: setApplicationMenu],
getApplicationMenu: [Function: getApplicationMenu],
commandLine:
{ appendSwitch: [Function: appendSwitch],
appendArgument: [Function: appendArgument] },
getAppMetrics: [Function],
isPackaged: false,
allowNTLMCredentialsForAllDomains: [Function],
releaseSingleInstance: [Function],
makeSingleInstance: [Function] }


But if I try to get the user data path



const __user_data = electron.app.getPath('userData');


I get this error:



Cannot read property 'getPath' of undefined


I am wondering why is this happening because app exists, but if I run app.getPath() app is not existing anymore. A similar thing happens with electron.remote, I have tried taht as well, even this is in the main process.










share|improve this question
























  • app exists, its getPath which does not based on the metadata you printed above.
    – Daniel
    Nov 11 at 13:26










  • @Daniel If I print app in other place (main.js, the start point of the application where getPath works), the same metadata is showed :/. I am wondering now if app only can be instantiated once in the whole program
    – ChesuCR
    Nov 12 at 16:29










  • I have just realised, in spite of getting the error on the console (chromium console), I am getting the right path in the terminal console :O
    – ChesuCR
    Nov 12 at 17:16










  • Ah! I was calling the file from different processes, main an renderer, that was the mistake!! I have solved as I have written in my answer
    – ChesuCR
    Nov 12 at 17:31






  • 1




    Good work. Glad you got it fixed.
    – Daniel
    Nov 12 at 18:11













up vote
2
down vote

favorite









up vote
2
down vote

favorite











Documentation



If you check this electron installation manual, you can read that you should install electron this way:



npm install electron --save-dev


So I did it. But if you check this other document they say:




When using Electron's built-in module you might encounter an error like this:



  > require('electron').webFrame.setZoomFactor(1.0)
Uncaught TypeError: Cannot read property 'setZoomLevel' of undefined


This is because you have the npm electron module installed either locally or globally, which overrides Electron's built-in module.




I do not know if with "locally" they mean like this (without --save-dev):



npm install electron


Resolve electron



To check if the installation is right:




To verify whether you are using the correct built-in module, you can print the path of the electron module:



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


and then check if it is in the following form:



  "/path/to/Electron.app/Contents/Resources/atom.asar/renderer/api/lib/exports/electron.js"


If it is something like node_modules/electron/index.js, then you have to either remove the npm electron module, or rename it.




The result in my application is



...app_foldernode_moduleselectrondistresourceselectron.asarbrowserapiexportselectron.js


Problem



I can access to the electron object from the main.js file. This is working fine:



const {app} = require('electron');


But if I do this in other js files (I require these files from main.js) I get an undefined value. Is this normal? Do I need to send the electron object as argument to these other modules?



They also say this, but I am taking into account:




However if you are using the built-in module but still getting this error, it is very likely you are using the module in the wrong process. For example electron.app can only be used in the main process, while electron.webFrame is only available in renderer processes.




Is this documentation still up to date? How should I install Electron to make work the built-in module?



Specific problem (Update)



Actually if I do in this other module



const electron = require('electron');
console.log(electron)
console.log(electron.app)


The objects are printed:



{ clipboard: [Getter],
crashReporter: [Getter],
nativeImage: [Getter],
shell: [Getter],
app: [Getter],
autoUpdater: [Getter],
BrowserView: [Getter],
BrowserWindow: [Getter],
contentTracing: [Getter],
dialog: [Getter],
globalShortcut: [Getter],
ipcMain: [Getter],
inAppPurchase: [Getter],
Menu: [Getter],
MenuItem: [Getter],
net: [Getter],
netLog: [Getter],
Notification: [Getter],
powerMonitor: [Getter],
powerSaveBlocker: [Getter],
protocol: [Getter],
screen: [Getter],
session: [Getter],
systemPreferences: [Getter],
TopLevelWindow: [Getter],
TouchBar: [Getter],
Tray: [Getter],
View: [Getter],
webContents: [Getter],
WebContentsView: [Getter] }

App {
_events:
{ login: [Function],
'certificate-error': [Function],
'select-client-certificate': [Function],
quit: [Function],
'web-contents-created': [Function],
'session-created': [Function],
'will-quit': [Function],
ready: [ [Function], [Function] ],
'window-all-closed': [Function] },
_eventsCount: 9,
_maxListeners: undefined,
whenReady: [Function: whenReady],
setApplicationMenu: [Function: setApplicationMenu],
getApplicationMenu: [Function: getApplicationMenu],
commandLine:
{ appendSwitch: [Function: appendSwitch],
appendArgument: [Function: appendArgument] },
getAppMetrics: [Function],
isPackaged: false,
allowNTLMCredentialsForAllDomains: [Function],
releaseSingleInstance: [Function],
makeSingleInstance: [Function] }


But if I try to get the user data path



const __user_data = electron.app.getPath('userData');


I get this error:



Cannot read property 'getPath' of undefined


I am wondering why is this happening because app exists, but if I run app.getPath() app is not existing anymore. A similar thing happens with electron.remote, I have tried taht as well, even this is in the main process.










share|improve this question















Documentation



If you check this electron installation manual, you can read that you should install electron this way:



npm install electron --save-dev


So I did it. But if you check this other document they say:




When using Electron's built-in module you might encounter an error like this:



  > require('electron').webFrame.setZoomFactor(1.0)
Uncaught TypeError: Cannot read property 'setZoomLevel' of undefined


This is because you have the npm electron module installed either locally or globally, which overrides Electron's built-in module.




I do not know if with "locally" they mean like this (without --save-dev):



npm install electron


Resolve electron



To check if the installation is right:




To verify whether you are using the correct built-in module, you can print the path of the electron module:



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


and then check if it is in the following form:



  "/path/to/Electron.app/Contents/Resources/atom.asar/renderer/api/lib/exports/electron.js"


If it is something like node_modules/electron/index.js, then you have to either remove the npm electron module, or rename it.




The result in my application is



...app_foldernode_moduleselectrondistresourceselectron.asarbrowserapiexportselectron.js


Problem



I can access to the electron object from the main.js file. This is working fine:



const {app} = require('electron');


But if I do this in other js files (I require these files from main.js) I get an undefined value. Is this normal? Do I need to send the electron object as argument to these other modules?



They also say this, but I am taking into account:




However if you are using the built-in module but still getting this error, it is very likely you are using the module in the wrong process. For example electron.app can only be used in the main process, while electron.webFrame is only available in renderer processes.




Is this documentation still up to date? How should I install Electron to make work the built-in module?



Specific problem (Update)



Actually if I do in this other module



const electron = require('electron');
console.log(electron)
console.log(electron.app)


The objects are printed:



{ clipboard: [Getter],
crashReporter: [Getter],
nativeImage: [Getter],
shell: [Getter],
app: [Getter],
autoUpdater: [Getter],
BrowserView: [Getter],
BrowserWindow: [Getter],
contentTracing: [Getter],
dialog: [Getter],
globalShortcut: [Getter],
ipcMain: [Getter],
inAppPurchase: [Getter],
Menu: [Getter],
MenuItem: [Getter],
net: [Getter],
netLog: [Getter],
Notification: [Getter],
powerMonitor: [Getter],
powerSaveBlocker: [Getter],
protocol: [Getter],
screen: [Getter],
session: [Getter],
systemPreferences: [Getter],
TopLevelWindow: [Getter],
TouchBar: [Getter],
Tray: [Getter],
View: [Getter],
webContents: [Getter],
WebContentsView: [Getter] }

App {
_events:
{ login: [Function],
'certificate-error': [Function],
'select-client-certificate': [Function],
quit: [Function],
'web-contents-created': [Function],
'session-created': [Function],
'will-quit': [Function],
ready: [ [Function], [Function] ],
'window-all-closed': [Function] },
_eventsCount: 9,
_maxListeners: undefined,
whenReady: [Function: whenReady],
setApplicationMenu: [Function: setApplicationMenu],
getApplicationMenu: [Function: getApplicationMenu],
commandLine:
{ appendSwitch: [Function: appendSwitch],
appendArgument: [Function: appendArgument] },
getAppMetrics: [Function],
isPackaged: false,
allowNTLMCredentialsForAllDomains: [Function],
releaseSingleInstance: [Function],
makeSingleInstance: [Function] }


But if I try to get the user data path



const __user_data = electron.app.getPath('userData');


I get this error:



Cannot read property 'getPath' of undefined


I am wondering why is this happening because app exists, but if I run app.getPath() app is not existing anymore. A similar thing happens with electron.remote, I have tried taht as well, even this is in the main process.







javascript node.js npm electron built-in






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 9 at 19:29

























asked Nov 9 at 18:06









ChesuCR

5,34832052




5,34832052












  • app exists, its getPath which does not based on the metadata you printed above.
    – Daniel
    Nov 11 at 13:26










  • @Daniel If I print app in other place (main.js, the start point of the application where getPath works), the same metadata is showed :/. I am wondering now if app only can be instantiated once in the whole program
    – ChesuCR
    Nov 12 at 16:29










  • I have just realised, in spite of getting the error on the console (chromium console), I am getting the right path in the terminal console :O
    – ChesuCR
    Nov 12 at 17:16










  • Ah! I was calling the file from different processes, main an renderer, that was the mistake!! I have solved as I have written in my answer
    – ChesuCR
    Nov 12 at 17:31






  • 1




    Good work. Glad you got it fixed.
    – Daniel
    Nov 12 at 18:11


















  • app exists, its getPath which does not based on the metadata you printed above.
    – Daniel
    Nov 11 at 13:26










  • @Daniel If I print app in other place (main.js, the start point of the application where getPath works), the same metadata is showed :/. I am wondering now if app only can be instantiated once in the whole program
    – ChesuCR
    Nov 12 at 16:29










  • I have just realised, in spite of getting the error on the console (chromium console), I am getting the right path in the terminal console :O
    – ChesuCR
    Nov 12 at 17:16










  • Ah! I was calling the file from different processes, main an renderer, that was the mistake!! I have solved as I have written in my answer
    – ChesuCR
    Nov 12 at 17:31






  • 1




    Good work. Glad you got it fixed.
    – Daniel
    Nov 12 at 18:11
















app exists, its getPath which does not based on the metadata you printed above.
– Daniel
Nov 11 at 13:26




app exists, its getPath which does not based on the metadata you printed above.
– Daniel
Nov 11 at 13:26












@Daniel If I print app in other place (main.js, the start point of the application where getPath works), the same metadata is showed :/. I am wondering now if app only can be instantiated once in the whole program
– ChesuCR
Nov 12 at 16:29




@Daniel If I print app in other place (main.js, the start point of the application where getPath works), the same metadata is showed :/. I am wondering now if app only can be instantiated once in the whole program
– ChesuCR
Nov 12 at 16:29












I have just realised, in spite of getting the error on the console (chromium console), I am getting the right path in the terminal console :O
– ChesuCR
Nov 12 at 17:16




I have just realised, in spite of getting the error on the console (chromium console), I am getting the right path in the terminal console :O
– ChesuCR
Nov 12 at 17:16












Ah! I was calling the file from different processes, main an renderer, that was the mistake!! I have solved as I have written in my answer
– ChesuCR
Nov 12 at 17:31




Ah! I was calling the file from different processes, main an renderer, that was the mistake!! I have solved as I have written in my answer
– ChesuCR
Nov 12 at 17:31




1




1




Good work. Glad you got it fixed.
– Daniel
Nov 12 at 18:11




Good work. Glad you got it fixed.
– Daniel
Nov 12 at 18:11












1 Answer
1






active

oldest

votes

















up vote
0
down vote



accepted










Apart from the doubts about the installation paths I have solved the issue. I was requiring this file from different places in my application. Sometimes I call it from the main process and in other cases from the renderer process. So I had to do this to support both cases:



var app = null;
if (typeof(electron.remote) !== 'undefined') {
app = electron.remote.app;
} else {
app = electron.app
}
const __user_data = app.getPath('userData');





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53231159%2fis-there-any-difference-between-electron-built-in-module-and-the-one-installed-w%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote



    accepted










    Apart from the doubts about the installation paths I have solved the issue. I was requiring this file from different places in my application. Sometimes I call it from the main process and in other cases from the renderer process. So I had to do this to support both cases:



    var app = null;
    if (typeof(electron.remote) !== 'undefined') {
    app = electron.remote.app;
    } else {
    app = electron.app
    }
    const __user_data = app.getPath('userData');





    share|improve this answer



























      up vote
      0
      down vote



      accepted










      Apart from the doubts about the installation paths I have solved the issue. I was requiring this file from different places in my application. Sometimes I call it from the main process and in other cases from the renderer process. So I had to do this to support both cases:



      var app = null;
      if (typeof(electron.remote) !== 'undefined') {
      app = electron.remote.app;
      } else {
      app = electron.app
      }
      const __user_data = app.getPath('userData');





      share|improve this answer

























        up vote
        0
        down vote



        accepted







        up vote
        0
        down vote



        accepted






        Apart from the doubts about the installation paths I have solved the issue. I was requiring this file from different places in my application. Sometimes I call it from the main process and in other cases from the renderer process. So I had to do this to support both cases:



        var app = null;
        if (typeof(electron.remote) !== 'undefined') {
        app = electron.remote.app;
        } else {
        app = electron.app
        }
        const __user_data = app.getPath('userData');





        share|improve this answer














        Apart from the doubts about the installation paths I have solved the issue. I was requiring this file from different places in my application. Sometimes I call it from the main process and in other cases from the renderer process. So I had to do this to support both cases:



        var app = null;
        if (typeof(electron.remote) !== 'undefined') {
        app = electron.remote.app;
        } else {
        app = electron.app
        }
        const __user_data = app.getPath('userData');






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 16 at 9:57

























        answered Nov 12 at 17:30









        ChesuCR

        5,34832052




        5,34832052






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53231159%2fis-there-any-difference-between-electron-built-in-module-and-the-one-installed-w%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Schultheiß

            Verwaltungsgliederung Dänemarks

            Liste der Kulturdenkmale in Wilsdruff