I'm trying to create a graphql api to upload files using nodejs. I use graphql-upload package to upload the file but it doesn't work. I'm getting 'ERR_PACKAGE_PATH_NOT_EXPORTED' Error. I tried so many solutions but nothing worked.
This is the code I wrote.
const { ApolloServer, gql } = require('apollo-server');
const { GraphQLUpload } = require('graphql-upload');
const fs = require('fs');
const typeDefs = gql`
scalar Upload
type File {
filename: String!
mimetype: String!
encoding: String!
}
type Query {
hello: String!
}
type Mutation {
singleUpload(file: Upload!): File!
}
`;
const resolvers = {
Upload: GraphQLUpload,
Query: {
hello: () => 'Hello, GraphQL!',
},
Mutation: {
singleUpload: async (_, { file }) => {
const { createReadStream, filename, mimetype, encoding } = await file;
// Modify the file path where you want to save the uploaded file
const filePath = `./uploads/${filename}`;
const stream = createReadStream();
const writeStream = fs.createWriteStream(filePath);
await stream.pipe(writeStream);
return { filename, mimetype, encoding };
},
},
};
const server = new ApolloServer({ typeDefs, resolvers });
server.listen().then(({ url }) => {
console.log(`Server ready at ${url}`);
});
When I run the above code it results with an error.
D:\CRUD-APIS\graphql-file-upload>node index.js
Debugger listening on ws://127.0.0.1:50971/33dd6fd0-e3f5-4780-9c5d-c7dd5474bdcc
For help, see: https://nodejs.org/en/docs/inspector
Debugger attached.
Waiting for the debugger to disconnect...
node:internal/modules/cjs/loader:488
throw e;
^
Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: No "exports" main defined in D:\CRUD-APIS\graphql-file-upload\node_modules\graphql-upload\package.json
at new NodeError (node:internal/errors:371:5)
at throwExportsNotFound (node:internal/modules/esm/resolve:453:9)
at packageExportsResolve (node:internal/modules/esm/resolve:731:3)
at resolveExports (node:internal/modules/cjs/loader:482:36)
at Function.Module._findPath (node:internal/modules/cjs/loader:522:31)
at Function.Module._resolveFilename (node:internal/modules/cjs/loader:919:27)
at Function.Module._load (node:internal/modules/cjs/loader:778:27)
at Module.require (node:internal/modules/cjs/loader:1005:19)
at require (node:internal/modules/cjs/helpers:102:18)
at Object.<anonymous> (D:\CRUD-APIS\graphql-file-upload\index.js:2:27) {
code: 'ERR_PACKAGE_PATH_NOT_EXPORTED'
}
I didn't understand what exactly happening here. If anyone knows how to upload a file using graphql with nodejs please share.
Thank you
I'm trying to write an api using graphql and nodejs to upload a file. I tried the code which I attached to this but every time I run the project Im getting
Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: No "exports" main defined in D:\CRUD-APIS\graphql-file-upload\node_modules\graphql-upload\package.json
I couldnt upload the file using this. If anyone know how to upload file using graphql and nodejs please share your answers.