node.js - 转到 Visual Studio 代码中的定义不起作用

标签 node.js visual-studio-code

我正在尝试为 https://code.visualstudio.com/docs/languages/javascript 之后的 nodejs 项目设置 Visual Studio 代码

我在根文件夹中创建了一个包含内容的 jsconfig.json 文件

{
  "compilerOptions": {
    "target": "ES5", //tried ES6 as well
    "module": "commonjs"
  }
}

This file tells VS Code you are writing ES5 compliant code and the module system you want to use is the commonjs framework. With these options set, you can start to write code that references modules in other files. For example, in app.js we require the ./routes/index module, which exports an Express.Router class. If you bring up IntelliSense on routes, you can see the shape of the Router class.

虽然它似乎不适用于 vscode 0.9.1。我没有在自己的模块上获得智能感知。转到定义也不起作用。

https://code.visualstudio.com/docs/runtimes/nodejs#_great-code-editing-experiences

有没有办法去定义工作?

最佳答案

TLDR;如果您直接在 exports 对象上设置变量/函数,它似乎可以工作。


在尝试了不同的方式来编写模块后,我发现如果你以某种方式编写模块,它就可以工作。这是一个有效的示例。

exports.myFunction = function (a, b) {
    return a + b;
};

现在,如果您从另一个文件导入此模块,您将获得智能感知并转到定义将起作用。但是,如果您这样编写模块或任何其他变体,它将无法正常工作。

var myModule = {};
myModule.myFunction = function (a,b) {
    return a + b;
};
exports = myModule;

在 vscode 存储库上提交了一个问题。

https://github.com/Microsoft/vscode/issues/15004#issuecomment-258980327

如果情况发生变化,我会更新我的答案。

关于node.js - 转到 Visual Studio 代码中的定义不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33194157/

相关文章:

git - 如何在 VS 代码中.gitignore "public/images/"文件夹?

visual-studio-code - VS Code 扩展 - 以编程方式查找键绑定(bind)

visual-studio-code - 在Visual Studio Code中生成当前打开的文件

go - 为什么 vscode golang 插件在保存时删除运算符周围的空格?

visual-studio-code - VSCode : How to change the delay for parameter hints?

javascript - 有人可以用更外行的术语解释什么是 util.inherits 吗?

node.js - 如何打印键中有逗号的 JSON 元素?

javascript - 我的JSON文件格式正确吗?

node.js - 未捕获( promise )ReferenceError : Buffer is not defined in vite/sveltekit with Torus

angularjs - Node 应用程序中 NGINX 502 错误网关中的 2 分钟 Http 请求超时