我正在尝试为 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/