broofa / mime

Mime types for JavaScript

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Error with implementation in NestJS project: Error [ERR_REQUIRE_ESM]: require() of ES Module (...) not supported

uszakow opened this issue · comments

Sanity Checks

  • This is NOT about a specific MIME type⬌extension mapping. Such concerns should be addressed in the mime-db project. (Be sure to search the issues first. Odds are good your concern has already been reported.)
  • Your issue title is descriptive and specific to the problem (i.e. search-engine friendly)
  • You understand what an "MRE" is, and why providing one is important. (If not, read this)

MRE (link)

https://github.com/uszakow/nest-test-project.git

MRE (supporting details)

I want to use mime lib in a new NestJS project. I add the code in app.service.ts file:

import mime from 'mime'

(...)
const mimeType = mime.getType('txt')
console.log('mimeType', mimeType)

Actual behavior: an error

const mime_1 = require("mime");

Error [ERR_REQUIRE_ESM]: require() of ES Module /file_name/ not supported.

Expected behavior: project can be built

"Actually, let me just tell you about my problem instead ..."

No response

Same issue

Works for me ...

$ git clone https://github.com/uszakow/nest-test-project.git

[...]

$ cd nest-test-project.git

[...]

$ npm install

[...]

$ npm run build

> nest-test-project@0.0.1 build
> nest build

[... build runs with no errors]

Error [ERR_REQUIRE_ESM]: require() of ES Module /file_name/ not supported.

I suspect nest is compiling your TS to CommonJS (using require() instead of import). If so, then this is expected. mime@4+ is ESM only.

You'll need to configure your nest / your project to compile to ESM. Either that or pin your mime dependency to version 3.