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

ERR_PACKAGE_PATH_NOT_EXPORTED #378

Closed keerthanapraskash closed 10 months ago

keerthanapraskash commented 10 months ago

@jaydenseric

Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: No "exports" main defined in D:\CRUD-APIS\original-issue\node_modules\graphql-upload\package.json

I'm new to Graphql. I am trying to upload a file using Graphql. I'm following the code written in the official doc of appollo but getting this error.

`const express = require('express'); const { ApolloServer, gql } = require('apollo-server-express'); const { GraphQLUpload, graphqlUploadExpress, // A Koa implementation is also exported. } = require('graphql-upload');

// const GraphQLUpload = require("graphql-upload/GraphQLUpload.js"); // const graphqlUploadExpress= require("graphql-upload/graphqlUploadExpress.js"); const { finished } = require('stream/promises'); const { ApolloServerPluginLandingPageLocalDefault } = require('apollo-server-core');

const typeDefs = gql`

scalar Upload

type File { filename: String! mimetype: String! encoding: String! }

type Query {

otherFields: Boolean!

}

type Mutation {

singleUpload(file: Upload!): File!

} `;

const resolvers = {

Upload: GraphQLUpload,

Mutation: { singleUpload: async (parent, { file }) => { const { createReadStream, filename, mimetype, encoding } = await file;

  const stream = createReadStream();

  const out = require('fs').createWriteStream('local-file-output.txt');
  stream.pipe(out);
  await finished(out);

  return { filename, mimetype, encoding };
},

}, };

async function startServer() { const server = new ApolloServer({ typeDefs, resolvers,

csrfPrevention: true,
cache: 'bounded',
plugins: [ApolloServerPluginLandingPageLocalDefault({ embed: true })],

}); await server.start();

const app = express();

app.use(graphqlUploadExpress());

server.applyMiddleware({ app });

await new Promise((r) => app.listen({ port: 4000 }, r));

console.log(🚀 Server ready at http://localhost:4000${server.graphqlPath}); }

startServer();`

jaydenseric commented 10 months ago

graphql-upload is a pure ESM package, that works with how Node.js runs ESM:

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

You can learn more about working with pure ESM packages here:

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

You can't require this package, you can only import it.

You are also making a second error; resulting in ERR_PACKAGE_PATH_NOT_EXPORTED. You are not deep importing from the relevant module; this package doesn't have a main index module. The publicly exported modules are documented here:

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

Hope that helps! Next time, please try searching the issues for similar ones raised in the past.