Electron-vue: How to access electron "app" from vue?

Created on 9 May 2017  ·  8Comments  ·  Source: SimulatedGREG/electron-vue

I'd like to display the app version in my ui. The version should come from package.json. However I can't seem to access app from within my renderer components.

I've tried

<script>
  import app from 'electron'

  export default {

    data () {
      return {
        appVersion: app.getVersion()
      }
    },

I was considering importing package.json to read it directly from there but I'm not sure if this will work both in dev and build since package.json doesn't get packaged (I'm using electron-builder)?

question

Most helpful comment

My mistake, I for some reason thought app was a shared module. You'll need to access it from remote. In development you'll probably get the version of electron being used, but in production you should get the version from package.json.

import { remote } from 'electron'

console.log(remote.app.getVersion())

All 8 comments

@alexcroox

You almost had it, but you need to destructure the app from electron.

import { app } from 'electron'

app is undefined if I log it after the import

My mistake, I for some reason thought app was a shared module. You'll need to access it from remote. In development you'll probably get the version of electron being used, but in production you should get the version from package.json.

import { remote } from 'electron'

console.log(remote.app.getVersion())

Definitely getting somewhere but I think that is the version of electron itself since it doesn't match either of my package.json (it shows 1.6.6 when package.json has 0.1.1). When I run npm run build that random version number reveals itself:

No native production dependencies
Packaging for darwin x64 using electron 1.6.6 to dist/mac

Reading the docs for it:

app.getVersion()

Returns String - The version of the loaded application. If no version is found in the application's package.json file, the version of the current bundle or executable is returned.

Not sure why it can't read the "version": "0.1.2" in both package.json's (which explains why it's falling back to the executable version)

Just as I just previously mentioned, you will get the electron version in development, but in a production build you will get the package.json version. This behavior is described in electron's documentation (https://electron.atom.io/docs/api/app/#appgetversion).

Can confirm, when built it is correct. Thank you so much!

Hi @SimulatedGREG and @alexcroox

I had a similar problem with app.getPath('userData') returning Electron in development and My-App in production.

In .electron-vue/dev-runner.js i replaced

`electronProcess = spawn(electron, ['--inspect=5858', path.join(__dirname, '../dist/electron/main.js')])`

with

electronProcess = spawn(electron, ['--inspect=5858', '.'])

This way Electron will read package.json and load the main script.

Is that safe to use this approach of @n82, @SimulatedGREG ?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mvalim picture mvalim  ·  4Comments

iwen-pengh picture iwen-pengh  ·  3Comments

simonwjackson picture simonwjackson  ·  3Comments

webrtcn picture webrtcn  ·  3Comments

rodrigomata picture rodrigomata  ·  3Comments