Electron 需要模块:由于工作目录不同而运行时智能感知不起作用

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

我正在用 vs-code 编写一个 electron-app。首先,我在要求本地文件作为模块时遇到了一些问题,但我设法修复了它们。现在唯一的问题是我失去了这些本地模块的智能感知。

项目结构:

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

我的目标:在render.js中使用config.js中的方法。

我现在明白为什么 vs-code 的行为像它一样,但我没有看到任何解决方案:当应用程序是 运行 当前工作目录(__dirname)是 ./app所以我必须在 require(./js/config) 上要求 config.js-文件。缺点是我失去了这个模块的完整智能感知。

当我开发并打开 render.js 文件时,从 vs-code 的角度来看,当前目录是 ./app/js,所以我的模块的相对路径是 ./config.js。现在我会再次拥有 intellisense,但应用程序不再工作了: require(./config) 结果为 error: cannot find module

有什么方法可以让应用程序运行并且我在 vs 代码中获得智能感知?我很感激能得到的任何帮助。

我发现了问题。我在 index.html 文件中引用了 render.js。如果我在 app.js 文件中使用 require(./app/js/render) 加载它,一切正常,包括 intellisens。

就像你发现的一样;问题是 vscode 并且应用程序未使用相同的起点。
这就是我为让它工作所做的工作:

层次结构:

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

index.html 中,我像这样加载 renderer.js:

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

这样,应用程序和 vscode 都使用根文件夹作为起点。
renderer.js 中,我只有这些:

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

这让我可以在 index.js 中要求 other.js 并使用智能感知。
index.js:

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

最后 other.js

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