I'm looking to setup a custom endpoint that makes MSSQL queries using the mssql package. Thus far, I've been able to get it to work if I load a page with a button to call the endpoint. However, if I load a page fresh after uncommenting the connection code, the page is then just blank and the console reports a "Buffer is not defined" error. It seems to expect Buffer to be in the global namespace of the client... but what is odd is this code should only on the server.
I've run into a similar issue and solved it via webpack aliasing to get to this point. It builds just fine, but the client-side is the issue.
I've tried putting in the following snippet to get it to work but it just then proceeds to the next bit of code that shouldn't be running on the client anyway:
plugins:[
...(config.resolve.plugins || []),
new webpack.ProvidePlugin({
Buffer: ['buffer', 'Buffer'],
}),
]
When I do that, it then starts throwing compile errors regarding a property 'tap' being unable to be accessed from an undefined value.
I even just aliased
mssql
altogether to see if it would just not attempt to compile anything for client usage. Again, builds fine, but Buffer is not defined at runtime.
Here's my aliasing code at the moment:
alias: {
...(config.resolve.alias || {}),
...['mssql'].reduce((c, i) => ({ ...c, [i]: path.resolve(__dirname, `mocks/${i}.js`) }), {}),
},
Hope I'm being clear enough. Basically, whenever I try to use mssql, stuff breaks at runtime 🙂 .
Any feedback is appreciated, thank you.
Hmm that is odd, maybe some kind of workaround?
https://github.com/vitejs/vite/discussions/2785#discussioncomment-2663819, though Node.js Buffer references seem server-specific so it's odd that this is in your client bundle
Thanks @thisisnotchris !
I looked into your feedback and tried many of the adjustments suggested in a couple different ways with no luck.
However, I did find along the chain some advice to removed node_modules and the package-lock.json and reinstall the dependencies. I did this along with removing all suggested code updates to bring things back to the basic state.
After doing that and confirming everything built and ran fine, I reintroduced my
mssql
code... and it didn't produce the Buffer error I was getting anymore.
Not sure why this would've happened but I do see that I installed
buffer
as a package dependency at 6.0.3... not sure if that's related but I'm reluctant to kick the tower of cards at the moment, lol.
Hope someone finds this useful!
Ah! Well I'm glad it's working! Let us know if you gain more context on the issue!
Star
Discord
online
Get help straight from the Payload team with an Enterprise License.