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 not supported #356

Closed nekmart closed 1 year ago

nekmart commented 1 year ago

Hello, I want to use graphql upload package in nestjs. But I am facing some error-

Here is my code-

import { GraphqlUpload, FileUpload } from "graphql-upload/GraphQLUpload.mjs";
import { createWriteStream } from 'fs';
    @Mutation(() => SuccessInfo, { name: "updateProfile" })
    update(
        @Args("file", { type: () => GraphqlUpload }) {
            createReadStream,
            filename
        }: FileUpload
    ) {
        console.log(filename);
    }

When I start my server I am getting this error like this-

image

How can I solve it.

jaydenseric commented 1 year ago

As documented, this package exports ESM:

https://github.com/jaydenseric/graphql-upload/tree/v16.0.2#exports

A CJS module can't require an ESM module, but it can import an ESM module using a dynamic import call:

https://nodejs.org/api/esm.html#interoperability-with-commonjs

It appears you are writing ESM in your source code, but that is not what runs. You have some sort of transpilation setup in your project that is converting that ESM to CJS, which is what your application actually runs as.

You should update your tooling to build ESM, and run ESM. That way you can import CJS or ESM dependencies without problems at runtime.