skip to Main Content

why does vs code on the nodejs project not give import suggestions when using expressjs in code?
I created node typescript project, I installed express. when I write in the code

 export const server = express()

vs code highlights the error that it cannot find express, and does not give me suggestions from where to import it, as it works for example on react
How can I enable import suggestions?
enter image description here

Here is my tsconfig.json. I tried any different settings, but it did not help

{
  "compilerOptions": {
    "lib": ["es2023"],
    "module": "node16",
    "target": "es2022",
    "strict": true,
    "esModuleInterop": true,
    "skipLibCheck": true,
    "moduleResolution": "node16",
    "outDir": "dist",
    "rootDir": "src",
    "baseUrl": ".",
    "sourceMap": true,
    "paths": {
      "@/*": ["src/*"]
    },  
    "checkJs": true
  },
  "include": ["src/**/*.ts"],
  "exclude": ["node_modules", ".idea", "**/*.test.ts"]
}

This is package.json dependencies:

    
  "devDependencies": {
    "@types/express": "^5.0.0",
    "@types/node": "^22.8.5",
    "ts-node-dev": "^2.0.0",
    "tsconfig-paths": "^4.2.0",
    "typescript": "^5.6.3"
  }

2

Answers


  1. Chosen as BEST ANSWER

    This problem exist because in express lib in node_modules in index.d.ts file no export of 'express' function, instead it exports 'e' function. So we must use 'e' from 'express' instead of 'express'. enter image description here


  2. In VS Code settings, search for javascript.suggest.autoImports and make sure it’s enabled.
    Similarly, enable typescript.suggest.autoImports if you’re working with TypeScript.

    Install the JavaScript and TypeScript Nightly extension to get enhanced auto-completion features in JavaScript/TypeScript files.
    If you’re using TypeScript, ensure the TypeScript and npm extensions are installed.

     { 
       "compilerOptions": {
       "module": "commonjs",
       "target": "es6",
       "baseUrl": "./",
       "paths": {
       "*": ["node_modules/*"]
      }
     },
        "exclude": ["node_modules"]
     }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search