Skip to content
Advertisement

How to get intellisense for middleware of express in external file in vscode?

I’m trying to write a middleware of express. And I wrote that in a single file test2.js

In the server, I can have intellisense like:

enter image description here

In that single file, the middleware works fine, but I can’t have intellisense of req and res

Is there any way to get the intellisense?


Here is my server test1.js:

JavaScript

Here is my middleware test2.js:

JavaScript

Advertisement

Answer

Maybe JSDoc is an option? You might need to install the type definitions: npm i @types/express -D (“Automatic Type Acquisition” in VS Code may or may not do that for your automatically)

JavaScript

https://github.com/Microsoft/TypeScript/wiki/JSDoc-support-in-JavaScript


You can also use your own typescript declaration file:

myTypes.d.ts

JavaScript

Types are usually bound to the module’s scope, but you can import any type and re-declare it in the global scope.

Now vscode finds the types without the “dirty” {import("express")}

myMiddleware.js

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement