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

Error - No "exports" main defined #337

Closed CamiloGPT closed 1 year ago

CamiloGPT commented 1 year ago

Hi

I'm using: Node.js v18.0.0 Graphql-upload 16.0.2 Express 4.18.1 Only JavaScript

const { graphqlUploadExpress } = require("graphql-upload"); 

And show this Error:

node:internal/modules/cjs/loader:489
      throw e;
      ^

Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: No "exports" main defined in /node_modules/graphql-upload/package.json

Command audit:

npm audit
found 0 vulnerabilities

I already executed these commands, but they do not solve anything:

# 👇️ delete node_modules and package-lock.json
rm -rf node_modules
rm -f package-lock.json
rm -f yarn.lock

# 👇️ clean npm cache
npm cache clean --force

npm install

how do I fix this error?

Thanks

jaydenseric commented 1 year ago

Please see the “Exports” section of the readme:

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

The correct way is:

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

Note that it's an ESM module, so you can't require it from a CJS module in your project but you can import it from an ESM module. That's just how Node.js ESM/CJS compatibility works:

https://nodejs.org/dist/latest-v18.x/docs/api/esm.html#interoperability-with-commonjs

Luchanso commented 1 year ago

workaround solution for me: 13.0.0v

ShilpaM007 commented 1 year ago

inorder to use Import, i will have to update module to commonjs, and this would make all of my code where i am using require go on toss.

Is there anyother way for same

jaydenseric commented 1 year ago

@ShilpaM007

inorder to use Import, i will have to update module to commonjs

I think you mean the opposite; you will have to update your modules to ESM.

You can import somewhere in a CJS module if it's a dynamic import:

const { default: graphqlUploadExpress } = await import("graphql-upload/graphqlUploadExpress.mjs");

Just not a regular import:

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

Note that CJS modules don't support top level await, so you will have to either chain a .then() or call an async function that contains the awaited dynamic import.

dominik-myszkowski commented 1 year ago

Please see the “Exports” section of the readme:

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

The correct way is:

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

Note that it's an ESM module, so you can't require it from a CJS module in your project but you can import it from an ESM module. That's just how Node.js ESM/CJS compatibility works:

https://nodejs.org/dist/latest-v18.x/docs/api/esm.html#interoperability-with-commonjs

How can I import and use it in my CommonJS module then?

garrettg123 commented 1 year ago

None of these work... I'm using commonjs with Node 19.

yangli-io commented 1 year ago

I'm using typescript and this mjs stuff has just made it super hard to use. I have to do stuff like allowJS which I strictly avoided before to ensure typescript was being used. Still I'm not able to get this to work

stanyq4 commented 1 year ago

Same here, been trying to upgrade to the LTS node "18.12.1" using Typescript, but this became a blocker.

@jaydenseric anything you can recommend for TS?

codelover2k commented 1 year ago

non of these solution work, also using node 18.12.1 is there no way to offer a default export that works?

clintonb commented 1 year ago

https://github.com/meabed/graphql-upload-ts seems to be a suitable replacement.

Luchanso commented 1 year ago

GraphQL not about FileUploading or binary transfer data, so the best solution using other methods for uploading, one of: using apart HTTP endpoint for uploading like example.com/upload and handle it with your framework express, fastify, etc. with app.use('/upload', (req, res) => { ... and you don't need this package in fact

P.S. this is just my experience and subjective opinion

RodeoAdrian commented 1 year ago

Hey! Why is this close? It seems you are not providing the best solution here @jaydenseric Same for me, nothing of this works. Using node v18 with commonJS, as almost every production app in the world.

Edit: I just read your comment here: https://github.com/jaydenseric/graphql-upload/issues/338#issuecomment-1242814452 I think you are right, but man, I hate the JavaScript ecosystem more and more every day.

Thank you for your hard work on this project; I will need to look for an alternative or make the 13.0.0 version work in some way


For those who can't use this package anymore I found the solution using: https://www.npmjs.com/package/graphql-upload-cjs node v18.15 @apollo/server v4 You will need to put a new header if you are using apollo server 4: https://stackoverflow.com/questions/74581070/apollo-client-this-operation-has-been-blocked-as-a-potential-cross-site-request