0

I have an Electron application, and need a feature like module.exports. But it seems like it's not supported.

If I try,

index.js

const helper = require('./helper')

let importantVar = helper.getImportantVar

helper.js

function getImportantVar() {
    let foo = 'bar'
    return foo
}

module.exports = {
    getImportantVar: getImportantVar
}

I get,

Uncaught Error: Cannot find module 'helper'

Any idea how I can proceed with this, to get the same functionality.

using electron: 6.0.12

Strazan
  • 147
  • 1
  • 16
  • 1
    Are you sure that the path to `helper.js` is correct. If both scripts are in the same folder use `const helper = require(__dirname + '/helper')`, because `./helper` is a path considered relative to the folder from which you are calling the script. – codtex Nov 06 '19 at 08:04
  • thanks <3, so `require('./scripts/helper')` works hehe! was that what you ment with `__dirname` or is that something else? – Strazan Nov 06 '19 at 08:15
  • Check whether your `helper.js` is in current directory?? – Subburaj Nov 06 '19 at 08:24
  • @Subburaj `helper.js`and `index.js` are in the same dir! – Strazan Nov 06 '19 at 09:03
  • @codtex if you post your comment as a proper answer I'll accept it! :D – Strazan Nov 11 '19 at 10:50
  • @Strazan according to [**this**](https://stackoverflow.com/a/18283508/5452965) answer if `helper.js` and `index.js` are in the same folder your code `require('./helper')` should work. I am not sure how paths resolve in electron. After all your problem seems to be a wrong path and I don't thing it is a good answer to say _"Fix your path"_ :) – codtex Nov 11 '19 at 14:24
  • Does this answer your question? [Cannot find module in Electron](https://stackoverflow.com/questions/48075839/cannot-find-module-in-electron) – Adi Prasetyo May 12 '20 at 11:46

0 Answers0