javascript - Electron 需要模块 : due to diffrent working directory while runtime intellisense is not working

标签 javascript node.js visual-studio-code electron intellisense

我正在使用 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/

相关文章:

Javascript window.open 被 IE 弹出窗口拦截器拦截

javascript - jstree 自定义节点标记

javascript - Meteor - 将数据从一个集合移动到另一个集合

node.js - react native + SSL 套接字 : Error: xhr poll error

node.js - 在 VS 代码中调试由 Electron 生成的子进程

node.js - Visual Studio Code - 通过 "Attach"进行调试在 Mac OS 上不起作用

c# - 无法在 OSX 上使用代码声明 ArrayList

javascript - react js 处理文件上传

javascript - Cordova:套接字、PushNotifications 或重复轮询服务器?

typescript - NextJS VSCode Typescript ==> 断点未绑定(bind)