question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. ItΒ collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cannot use electron in renderer

See original GitHub issue

I was using electron-vue for my project. Then I faced some issues with it. https://github.com/SimulatedGREG/electron-vue/issues/773 So I thought that it is electron-vue’s problem, so I started to use this CLI plugin. But I have the same problem with this one too.

The code in database/index.js:

import { remote } from 'electron'
let databaseAddress = path.join(remote.app.getPath('userData'), 'db.db')

Error: screenshot from 2019-01-10 00-17-04

And I realized that It’s not just for remote, I have the same problem with ipcRenderer and others too.

package.json:

{
  "name": "ganjoor-electron",
  "version": "0.1.0",
  "private": true,
  "scripts": {
    "serve": "vue-cli-service serve",
    "build": "vue-cli-service build",
    "lint": "vue-cli-service lint",
    "electron:build": "vue-cli-service electron:build",
    "electron:serve": "vue-cli-service electron:serve",
    "postinstall": "electron-builder install-app-deps"
  },
  "dependencies": {
    "@fortawesome/fontawesome-svg-core": "^1.2.12",
    "@fortawesome/free-solid-svg-icons": "^5.6.3",
    "@fortawesome/vue-fontawesome": "^0.1.4",
    "bulma-rtl": "^0.7.1",
    "pg-hstore": "^2.3.2",
    "sequelize": "^4.42.0",
    "sqlite3": "^4.0.4",
    "tedious": "^3.0.1",
    "vue": "^2.5.21",
    "vue-router": "^3.0.1",
    "vuex": "^3.0.1"
  },
  "devDependencies": {
    "@vue/cli-plugin-babel": "^3.3.0",
    "@vue/cli-plugin-eslint": "^3.3.0",
    "@vue/cli-service": "^3.3.0",
    "babel-eslint": "^10.0.1",
    "electron": "^4.0.0",
    "eslint": "^5.8.0",
    "eslint-plugin-vue": "^5.0.0",
    "vue-cli-plugin-electron-builder": "^1.0.0-rc.10",
    "vue-template-compiler": "^2.5.21"
  },
  "main": "background.js"
}

Folder Structure:

.
β”œβ”€β”€ babel.config.js
β”œβ”€β”€ dist_electron
β”‚Β Β  β”œβ”€β”€ index.js
β”‚Β Β  └── package.json
β”œβ”€β”€ package.json
β”œβ”€β”€ postcss.config.js
β”œβ”€β”€ public
β”‚Β Β  β”œβ”€β”€ favicon.ico
β”‚Β Β  β”œβ”€β”€ ganjoor.s3db
β”‚Β Β  └── index.html
β”œβ”€β”€ README.md
β”œβ”€β”€ src
β”‚Β Β  β”œβ”€β”€ App.vue
β”‚Β Β  β”œβ”€β”€ assets
β”‚Β Β  β”‚Β Β  β”œβ”€β”€ fonts
β”‚Β Β  β”‚Β Β  β”œβ”€β”€ font-style.css
β”‚Β Β  β”‚Β Β  └── logo.png
β”‚Β Β  β”œβ”€β”€ background.js
β”‚Β Β  β”œβ”€β”€ database
β”‚Β Β  β”‚Β Β  β”œβ”€β”€ index.js
β”‚Β Β  β”‚Β Β  └── models
β”‚Β Β  β”œβ”€β”€ main.js
β”‚Β Β  β”œβ”€β”€ router
β”‚Β Β  β”‚Β Β  └── index.js
β”‚Β Β  β”œβ”€β”€ store
β”‚Β Β  β”‚Β Β  β”œβ”€β”€ index.js
β”‚Β Β  β”‚Β Β  └── modules
β”‚Β Β  └── views
β”‚Β Β      β”œβ”€β”€ Poems.vue
β”‚Β Β      β”œβ”€β”€ Poets.vue
β”‚Β Β      β”œβ”€β”€ Poet.vue
β”‚Β Β      └── Verse.vue
└── yarn.lock
  • OS and version: Ubuntu 18.10
  • node version: v8.15.0
  • npm version: 6.4.1
  • yarn version : 1.12.3
  • vue-cli-plugin-electron-builder version : 1.0.0-rc.10
  • electron version: 4.0.0

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
nklaymancommented, Feb 9, 2019

The issue is that you are trying to use the same file in both the main and the render process. In the main process, it’s just electron.app.getPath(), but in the renderer it’s electron.remote.getPath(). Use this:

import electron from 'electron'
const isRenderer = process && process.type === 'renderer'
console.log((isRenderer ? electron.remote : electron).app.getPath('userData'))
0reactions
nklaymancommented, Feb 9, 2019

Actually, it does fail, but only if the server is started with the remote.app.getPath already there, it works fine if added via hmr.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Electron require is not defined in renderer process
I'm starting with electron and for some unknown reason I can't use require() function in renderer process. Uncaught ReferenceError: requireΒ ...
Read more >
Usage of electron-store in Renderer Processes #213 - GitHub
I've been trying to implement some data persistance using electron-store. But somehow, when I import it in the renderer process,Β ...
Read more >
Using Preload Scripts - Electron
In this part of the tutorial, you will learn what a preload script is and how to use one to securely expose privileged...
Read more >
Chapter 2. Your first Electron application - liveBook Β· Manning
This chapter covers. Structuring and setting up an Electron application; Generating a package.json, and configuring it to work with Electron in developmentΒ ...
Read more >
Using import/require inside of Electron app : r/electronjs - Reddit
While main is a Node.js environment where you can require() libs, renderer is a Chrome instance where you cannot access anything from theΒ ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found