typescript

How to import cors modules in TypeScript?


I am experiencing some issues when trying to import the cors module in an Express + TypeScript project.

If I write:

import cors from "cors";

I got the following message:

"Cannot find module 'cors'. Did you mean to set the 'moduleResolution' option to 'node', or to add aliases to the 'paths' option?"

How can I fix that?

[Edited]: BTW, I tried adding the moduleResolution option to my tsconfig.json and it doesn't work.


Solution

  • I solved it by installing the "@types/cors" package.

    I was migrating a JavaScript Node.js Server to TypeScript.

    I was following this guide: https://www.typescriptlang.org/docs/handbook/migrating-from-javascript.html#writing-a-configuration-file

    Apparently, I must import all types from the packages I am using.