Home > Software engineering >  npm package mime-types not working (giving undefined) with typescript (or nestjs)
npm package mime-types not working (giving undefined) with typescript (or nestjs)

Time:04-11

I am trying to use mime-types in NestJs as follows.

import mime from 'mime-types';

mime.contentType(req.url)

But mime gives me undefined even though I have installed mime-types npm package. It doesn't give any error in the editor (vs code)

Not facing any such issue with other packages. What is the issue with mime-types or am I using it incorrectly (On the official document also they have given CommonJs example not es6 example)

I checked in the simple node js file, there it is working. the issue only facing in NestJs (or Typescript)

CodePudding user response:

use import * as mime from 'mime-types' instead. Or use allowSyntheticDefaultImports

btw this due to how TypeScript works, NestJS has no opinions on how nodejs modules are loaded.

  • Related