VS Code中Node.js与Express.js的IntelliSense代码提示异常求助
问题
在使用VS Code开发Node.js与Express.js项目时,遇到IntelliSense代码提示失效的问题。示例代码如下:
const expressHandlebars = require("express-handlebars"); app.engine("handlebars", expressHandlebars({})); app.set("view engine", "handlebars"); app.set("views", "views/handlebars/pages");
尝试在expressHandlebars({})的参数中添加配置时,没有任何相关代码提示。查看该函数的实现源码可知配置对象包含7个可配置项:
function ExpressHandlebars(config) { // Config properties with defaults. utils.assign(this, { handlebars : Handlebars, extname : '.handlebars', layoutsDir : undefined, // Default layouts directory is relative to `express settings.view` + `layouts/` partialsDir : undefined, // Default partials directory is relative to `express settings.view` + `partials/` defaultLayout : undefined, helpers : undefined, compilerOptions: undefined, }, config); // Express view engine integration point. this.engine = this.renderView.bind(this); // Normalize `extname`. if (this.extname.charAt(0) !== '.') { this.extname = '.' + this.extname; } // Internal caches of compiled and precompiled templates. this.compiled = Object.create(null); this.precompiled = Object.create(null); // Private internal file system cache. this._fsCache = Object.create(null); }
已在项目根目录添加jsconfig.json文件,内容如下:
{ "compilerOptions": { "module": "commonjs", "target": "es6" }, "exclude": ["node_modules"] }
解决方案
安装类型定义文件
在终端执行以下命令,为express-handlebars补充TypeScript类型定义,这是VS Code提供精准提示的核心依赖:npm install --save-dev @types/express-handlebars优化
jsconfig.json配置
修改配置文件,开启JS类型检查,帮助VS Code更深入分析代码结构:{ "compilerOptions": { "module": "commonjs", "target": "es6", "checkJs": true }, "exclude": ["node_modules"] }保存后重启VS Code,让配置生效。
重置IntelliSense缓存
按Ctrl+Shift+P(Windows/Linux)或Cmd+Shift+P(Mac)打开命令面板,输入TypeScript: Restart TS Server并执行,强制刷新IntelliSense服务。确认依赖完整性
执行npm ls express-handlebars检查依赖是否正确安装,若未安装则执行npm install express-handlebars补全依赖。
内容的提问来源于stack exchange,提问作者Abdallah M Yassin
相关产品推荐
相关产品推荐

