francWhite
francWhite

Reputation: 121

Electron require modules: due to diffrent working directory while runtime intellisense is not working

Im writing an electron-app with vs-code. First I had some problems with requiring local files as modules, but i managed to fix them. The only problem now ist that i've lost intellisens for these local modules.

Project structure:

+---app
|   |   config.json
|   |   index.html
|   |
|   +---css
|   |       style.css
|   |
|   +---js
|           config.js
|           render.js
|
|   main.js
|   package.json
|   README.md

My goal: use the methods from config.js in render.js.

I realize by now why vs-code behaves like it does but I dont see any solution: when the application is running the current working direcotry(__dirname) is ./app so i have to require the config.js-file over require(./js/config). The downside ist i lose complete intellisense for this module.

When im developing and have the render.js file open the current directory from vs-code's point of view is ./app/js, so the relativ path to my module is ./config.js. Now i would have intellisense again but the application is not working anymore: require(./config) results in error: cannot find module

Is there any way that the application runs and i get intellisense in vs-code? I am grateful for any help I can get.

Upvotes: 0

Views: 588

Answers (2)

Blargmode
Blargmode

Reputation: 1105

Like you've discovered; the problem is vscode and the app not using the same starting point.
This is what I did to get it working:

Hierarchy:

+---assets
|   |
|   +---js
|       |    index.js
|       |    other.js
|   
|   main.js
|   index.html
|   renderer.js

In index.html I load renderer.js like so:

<script type="text/javascript">
    require("./renderer");
</script>

That way both the app and vscode uses the root folder as a starting point.
And in renderer.js this is all I have:

require('./assets/js/index');

That allows me to require other.js in index.js with working intellisense.
index.js:

const Other= require('./other').default;
...

And lastly other.js:

class Other{
    ...
}
module.exports.default = Other;

Upvotes: 1

francWhite
francWhite

Reputation: 121

I found the problem.. I was referencing the render.js in the index.html file. If I load it with require(./app/js/render) in the app.js file everything works fine, including intellisens.

Upvotes: 0

Related Questions