jaydenseric / graphql-upload

Middleware and an Upload scalar to add support for GraphQL multipart requests (file uploads via queries and mutations) to various Node.js GraphQL servers.
https://npm.im/graphql-upload
MIT License
1.42k stars 131 forks source link

Error [ERR_REQUIRE_ESM]: require() of ES Module not supported #368

Closed tommaso-merlini closed 1 year ago

tommaso-merlini commented 1 year ago

Even if the package is import via ESM the error: "Error [ERR_REQUIRE_ESM]: require() of ES Module not supported" still appears. The error only appears with versions > 13.0.0. Hope this is going to get fixed because the 13.0.0 version has a severe vulnerability (link: https://www.cve.org/CVERecord?id=CVE-2022-29353)

jaydenseric commented 1 year ago

Most likely what is happening in your project is that your source code looks like ESM, but the code that actually runs in Node.js after your build process transpiles it is CJS.

GRA0007 commented 1 year ago

We're having this issue as well and we can't use ESM as an output, because some of the other packages we use only work in CJS. The latest update of this package has broken our build.

I've managed to get around this temporarily using the esbuild config option noExternal which will force esbuild to transpile the library to CJS as well, but it's not ideal having this manually specified in our config.

samihuc commented 1 year ago

I have the same problem. I guess I'll be downgrading to version 15 for now. (I will say I don't think this would be a problem if the package main index module came back? I may be wrong on that but I've never had this issue with another package despite the fact that many others in our setup use mjs files, which the package or the package manager probably transpiles to cjs??)

dylantf commented 1 year ago

So @jaydenseric what's the solution here for anyone using typescript with a commonjs output? It seems impossible to use this package now.

jaydenseric commented 1 year ago

@dylantf this package is, and will only be, published in standard ESM format. It is up to you to figure out how to use the non-standard CJS format in your projects, it's beyond the scope of support for this repo. I highly recommend using standard ESM in your Node.js projects so you can easily use both CJS and ESM dependencies. I do so at work in TypeScript projects without issue. You can learn more about using an ESM package in different kinds of projects in this gist and it's comments:

https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c

alex-kinokon commented 1 year ago

non-standard CJS format

I’m pretty sure CJS has been standardized (2009) way before ESM (2015) was even a thing to begin with. It’s fine you don’t like the format, but calling it “non-standard” is straight out deceptive.

magrinj commented 1 year ago

Hi @jaydenseric,

I appreciate your stance on pushing the adoption of ESM and understand the benefits it brings to the table. The static analysis, tree shaking, and other features ESM provides are definitely compelling.

However, I believe it's important to consider the broad usage of the library. While ESM is indeed a standard, it's also true that not all frameworks and tools have fully embraced it. For example, many projects using frameworks such as Nest.js, which is based on the Express.js framework and largely CommonJS-centric, might face challenges when trying to incorporate libraries provided exclusively in ESM format.

Nest.js, while robust and versatile, doesn't currently provide a clean and straightforward way to incorporate ESM libraries. This can lead to added complexity in projects and potential hindrances to developers who are eager to use your library but may not have the resources or the capability to refactor their existing projects or handle additional configurations.

In my opinion, it might be a bit premature to transition exclusively to ESM, considering the current state of some popular frameworks and their compatibility with ESM. To make your library more accessible and easy to adopt, perhaps it could be beneficial to continue offering a CommonJS version alongside the ESM version for the time being ?

bebjakub commented 1 year ago

There is an alternative fork compatible with Nest.js https://www.npmjs.com/package/graphql-upload-minimal