I have a collection with an upload field where only zip files are accepted. They get saved to a folder
/files
and inside it theres another dir
/unzipped
. How do I unzip the file after it gets uploaded and saved to the
/unzipped
dir? I've tried using
yauzl
,
decompressed
and
zlib
but I keep getting errors from webpack(?) about adding something like
{ "stream": false }
or it requiring something for zlib, etc. After setting the keys that were shown in the error page to false the frontend UI fails to load and I get a blank white page. Is there a way to unzip and zip?
I was facing the same issue with you and found the solution through extensive research for couple of days.(In my case, I was using
extract-zip
module which rely on
yauzl
)
1. Create a mock module to alias the server-only modules.
//emptyModuleMock.js
module.exports = {
url: () => {},
raw: () => {},
};
2. Configure aliasing in the payload admin webpack configuration.(Change the server-only module path to be aliased as per your recommendation)
//payload.config.ts
const mockModulePath = path.resolve(__dirname, './emptyModuleMock');
...
const config = buildConfig({
admin: {
webpack: (config) => ({
resolve: {
...config.resolve,
alias: {
...config.resolve.alias,
//This is for my case, you can change the following line to [path.resolve(__dirname, '../node_modules/yauzl')]: mockModulePath,
[path.resolve(__dirname, '../node_modules/extract-zip')]: mockModulePath,
},
},
}),
}
});
3. If changes to your Webpack aliases are not surfacing, they might be cached in node_modules/.cache/webpack. Try deleting that folder and restarting your server.
Hope you don't mind the ping. Thank you for the answer- I will try your suggestion in a bit. Had to run a python server for managing the zip files haha
you are welcome
Hi again, finally got around to trying it with
extract-zip
itself. I keep getting the error that is present in the second screenshot.
yarn run v1.22.21
$ cross-env PAYLOAD_CONFIG_PATH=src/payload.config.ts nodemon
[nodemon] 2.0.22
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: ts
[nodemon] starting `ts-node src/server.ts -- -I`
(node:86591) [DEP0040] DeprecationWarning: The `punycode` module is deprecated. Please use a userland alternative instead.
(Use `node --trace-deprecation ...` to show where the warning was created)
/home/zoc/Desktop/code/zaurastudios/payload/node_modules/payload/src/express/admin.ts:8
ctx.express.use(ctx.config.routes.admin, await ctx.config.admin.bundler.dev(ctx))
^
TypeError: Cannot read properties of undefined (reading 'dev')
at initAdmin (/home/zoc/Desktop/code/zaurastudios/payload/node_modules/payload/src/express/admin.ts:8:79)
at initHTTP (/home/zoc/Desktop/code/zaurastudios/payload/node_modules/payload/src/initHTTP.ts:59:20)
at processTicksAndRejections (node:internal/process/task_queues:95:5)
at Payload.init (/home/zoc/Desktop/code/zaurastudios/payload/node_modules/payload/src/index.ts:17:21)
[19:17:03] INFO (payload): Connected to MongoDB server successfully!
[19:17:03] INFO (payload): Starting Payload...
[nodemon] app crashed - waiting for file changes before starting...
Text format if you want
Have deleted the .cache/webpack and reinstall all the node modules
(Please do ping when replying)
@Zoclhas Whats the act issue here?
It seems like an issue in admin.ts
or initHttp
I've been trying to unzip a file after its been uploaded to payload but keep coming up with webpack errors or the FE is blank
OK, when do you hit webpack errors?
Just loading the module?
A few minutes. I need to re-setup the environment
Sure
Okay here we are. Without this part of the code the app launches with
yarn dev
. But once I add it I get this error:
(ignore the commented part)
can you do
node -v
whats your node version?
21.5.0
I see
Webpack right?
You may need to setup resolution fallbacks for 'util' and 'stream'
How do I do that?
maybe something like
module.exports = {
// ...
resolve: {
fallback: {
util: false,
stream: false
}
}
// ...
};
I could be wrong, but i think like that will exclude it from the client?
Woah, new docs
Same error as this
Does this have something to do with Payload 2.0?
I've moved the afterChange hook to a separate file and did the same thing
getting the same error
Would it better so that after each file upload I call an endpoint that is only exposed locally with the file path. Then the file unzips it and does the other operations?
That is exactly same what I was doing with
extract-zip
. If the alias was configured correctly, you should not see the webpack errors originated from
extract-zip
. Just ignore @notchrisis's fallback configuration because if
extract-zip
get aliased correctly, webpack will never require
util
or
stream
. Please concentrate on confirming the aliasing work correctly.
I've followed what you said, but still getting the same error
Can you share your screen?
add
console.log(path.resolve(__dirname, '../node_modules/extract-zip'))
to your config file to check you are aliasing the correct route
and extract zip is present in node_modules
Oh wait, something new now
ERROR in ./src/collections/DownloadAfterHook.ts 125:0-34
Module not found: Error: Can't resolve 'extract-zip' in '/home/zoc/Desktop/code/zaurastudios/payload/src/collections'
and this too
It would be easy to assist you if you can share your screen...
I think your aliasing is not working correctly yet.
There needed to be a
...config
above
resolve:
works now
Star
Discord
online
Get dedicated engineering support directly from the Payload team.