How to get intellisense for express.js in vscode
Asked Answered
I

4

11

I have tried installing type definitions with:

My code is in server.js javascript file.

npm install @types/express

But still it doesn't seem to give intellisense for app after instantiating express .

But when I use express like this :

app = express() ;

app.listen(8000) ;

I am not getting the codecompletion or intellisense for app instance of express.

How to fix this ?

Ibbison answered 18/12, 2017 at 7:52 Comment(5)
Are you working with plain javascript or Typescript?Merozoite
I am working with plain javascriptIbbison
What packages do you have installed in your VSC?Cyclamen
I have installed express , @typings/express using npm . That's all .Ibbison
This may need restart of VS CodeSunlit
D
7

Quick Fix

Add this JsDoc comment to your controllers:

/** @type {import("express").RequestHandler} */
exports.myController = (req, res) => {
  // biz logic. ( here you would get intelliscence on req, res objects )
  const { id } = req.query;
  const { data } = req.body;
  res.status(200).json({data});
}

Optional

If you are using typescript you can create a utility function to wrap your actual handler to avoid writing RequestHandler types on every controller.

// endpoint.ts
import { RequestHandler } from 'express';

export const endpoint = (cb: RequestHandler) => {
  const handler: RequestHandler  = (req, res, next) => {
    cb(req,res,next)
  }
  return handler
}
// myController.ts
import { endpoint } from 'utils/endpoint';

export const myEndpoint = endpoint((req,res) => {
  res.send("Hey!");
});

Deformation answered 9/2, 2022 at 15:3 Comment(2)
do we have a shortcut, rather than adding /** @type {import("express").RequestHandler} */ this comment to every controller?Alkanet
Na, actually there is no way to know if that function is a controller without explicitly telling its type coz you can also export normal functions like that.Deformation
T
3

import this code:

/** @type {import("express").RequestHandler} */

Tigon answered 26/9, 2020 at 8:22 Comment(0)
M
1

A lot of delay in my answer, but here it goes:

You're using a .js file, so types doesn't exist on it. When you install the @types/something dependency via npm, this allows you using types, but only in Typescript.

Mihe answered 17/12, 2019 at 20:0 Comment(0)
G
-1

VS Code editor supports code auto completion for many of the programming languages like Javascript, TypeScript, JSON, HTML, CSS, Less, and Sass. Where as for other programming languages you may need to go and install the plugins.

To get auto completion type a key word and hit Ctrl+space to get the code auto completion suggestions as shown below.

enter image description here

But if you are still not getting then you need to go back and check your settings. Go to File->Preferences->Settings this opens up the window where you can change some of your preferences

enter image description here

and in this section go to Text Editor and Suggestions and check what are your settings for quick suggestions like if it is enabled or not and what is the delay set for the suggestions to appear you can make changes in it and check if it is working or not. Hope this would be helpful.

Garfieldgarfinkel answered 10/9, 2018 at 11:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.