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.43k stars 131 forks source link

Issues importing modules #334

Closed Nathan-Nesbitt closed 1 year ago

Nathan-Nesbitt commented 1 year ago

My original issues had to do with #332 and #333 , which led me to the same problem as this comment https://github.com/jaydenseric/graphql-upload/issues/332#issuecomment-1216789254 .

I think I've read through all of the related issues associated with the changes to this library. I've made the changes as suggested in the README and it also doesn't seem to help.

I'm running into the following error when I try to use the new import:

Must use import to load ES Module: /<PATH>/src/backend/node_modules/graphql-upload/graphqlUploadExpress.mjs

The code is as follows for the import:

import { graphqlUploadExpress } from "graphql-upload/graphqlUploadExpress.mjs";

I've got the following compiler options in tsconfig.json

"compilerOptions": {
    "module": "Node16",
    "maxNodeModuleJsDepth": 10,
    "allowJs": true,
  },

and am running the app using npx nodemon

I'm not really sure what else to try. Any advice for any other things to try?

jaydenseric commented 1 year ago

Hi @Nathan-Nesbitt , the right way to do that import is like this:

import graphqlUploadExpress from "graphql-upload/graphqlUploadExpress.mjs";

As you can see the export is a default export:

https://github.com/jaydenseric/graphql-upload/blob/e01b5d5541760d529b06c900883c5fa7febcff00/graphqlUploadExpress.mjs#L34

Hope that helps!

Nathan-Nesbitt commented 1 year ago

@jaydenseric thanks for the quick response, thought this had fixed the original issue but I seem to be back at this again.

Must use import to load ES Module: /<PATH>/backend/node_modules/graphql-upload/graphqlUploadExpress.mjsor the VS warning Module 'graphql-upload/graphqlUploadExpress.mjs' cannot be imported using this construct. The specifier only resolves to an ES module, which cannot be imported synchronously. Use dynamic import instead.

jaydenseric commented 1 year ago

Most likely you are not really writing ESM; you have some sort of dev tooling that transpiles the source ESM you're writing to CJS that actually runs at runtime. In Node.js, ESM can import from ESM or CJS modules, but CJS can't require an ESM module. This is not an issue with graphql-upload, but rather how Node.js works and your project is setup.

I highly, highly, recommend using ESM in projects both in the source code and at runtime. Your best solution in the long term is to change your build tooling to emit ESM for the runtime code, or else you will get locked out of more and more npm packages over time as they all migrate to pure ESM.