Collection endpoints - errors with modules #678
-
Hello again, Thanks to @GeorgeyB (#675 (comment)) I moved my custom express endpoint implementation to collection property
I use much more packages and each used in the handler throws such error "Module not found". Surprisingly endpoint works ok and I'm getting a response. When I used custom express endpoint I hadn't such errors. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
Hey @wojciechkrol — this is because you need to use Webpack Here's an example from one of our projects, where we alias a bunch of server-only modules: webpack: (config) => ({
...config,
resolve: {
...config.resolve,
alias: [
path.resolve(__dirname, './hookHelperFunctions/stripeFunctions'),
path.resolve(__dirname, './twilio'),
path.resolve(__dirname, './email/sendTemplatedEmail'),
].reduce((alias, aliasPath) => ({
...alias,
[aliasPath]: mockModulePath,
}), config.resolve.alias),
},
}), We basically tell Webpack that whenever one of those files gets imported, replace that file with the const mockModulePath = path.resolve(__dirname, './mocks/emptyModule.js'); And then module.exports = {}; There are some docs written up for this too here: Give it a shot! |
Beta Was this translation helpful? Give feedback.
-
Well, it looks a bit complicated. I will look into this solution. Thank you for the explanation! |
Beta Was this translation helpful? Give feedback.
Hey @wojciechkrol — this is because you need to use Webpack
alias
es to make sure that your custom endpoint code is not loaded into the admin JS bundle. Because the entire Payload config is imported directly in Webpack, and you now have your custom endpoint handlers imported in your Payload code, the admin panel is actually trying to load and execute those modules. And,fs
is a server-side only package built into Node.Here's an example from one of our projects, where we alias a bunch of server-only modules: