我正在使用 vs-code 编写一个 Electron 应用程序。首先,我在需要本地文件作为模块时遇到了一些问题,但我设法修复了它们。现在唯一的问题是我失去了这些本地模块的智能传感器。
项目结构:
+---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 的 Angular 来看是 ./app/js
,因此相对路径我的模块是./config.js
。现在我将再次拥有智能感知,但应用程序不再工作:
require(./config)
导致错误:找不到模块
有什么方法可以让应用程序运行并且我在 vs-code 中获得智能感知吗?我很感激我能得到的任何帮助。
最佳答案
就像你发现的那样;问题是 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;
关于javascript - Electron 需要模块 : due to diffrent working directory while runtime intellisense is not working,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53227152/