All things Shopify and commerce
Hi,
I'm working on a custom storefront using Shopify Hydrogen, and I'm facing an issue connecting to the database. I'm trying to integrate MongoDB into my Hydrogen app and shopify hydrogen is deployed on Shopify Oxygen. but I am facing issues when trying to connect to the database.
MongoDB Error :
Cannot read properties of undefined (reading 'connect')
when I tried to log a mongoose imported from mongoose I got undefined:
mongoose undefined
Even I tried with a MongoDB mongoConnect still didn't work and throwing below error:
ReferenceError: exports is not defined at /node_modules/mongodb/lib/index.js?v=ec958d92:2:23
I have tried with PostgreSQL as well but on connecting below errors I am having:
✘ [ERROR] Cannot bundle Node.js built-in "events" imported from "node_modules/pg/lib/client.js". Consider disabling ssr.noExternal or remove the built-in dependency. [plugin vite:dep-pre-bundle] node_modules/vite/node_modules/esbuild/lib/main.js:1225:21: 1225 │ let result = await callback({ ╵ ^ This error came from the "onResolve" callback registered here: node_modules/vite/node_modules/esbuild/lib/main.js:1150:20: 1150 │ let promise = setup({ ╵ ^
The plugin "vite:dep-pre-bundle" was triggered by this import node_modules/pg-pool/index.js:2:29: 2 │ const EventEmitter = require('events').EventEmitter
Please help me with the issue and let me know what I am doing wrong
It seems like you're encountering issues connecting MongoDB (and also PostgreSQL) to your custom Shopify Hydrogen storefront. This is how I'd look at it:
"Cannot read properties of undefined (reading 'connect')": This could be caused by not properly importing or initializing the mongoose library. Ensure you're using the correct import statement for MongoDB. You can try:
import mongoose from 'mongoose';
mongoose.connect('your-mongo-db-uri')
.then(() => console.log('Connected to MongoDB'))
.catch((err) => console.log('Error connecting to MongoDB:', err));
2. "ReferenceError: exports is not defined": This error likely arises because Shopify Hydrogen uses a bundler that might not be compatible with certain CommonJS exports. Try using an ES module import if possible or adjusting your bundler configuration (Vite/webpack) to handle CommonJS modules.
"Cannot bundle Node.js built-in 'events'": Shopify Hydrogen uses Vite, and it seems like Vite’s bundler is trying to include Node.js built-in libraries like events which it doesn't handle well for the browser environment.
You can fix this by:
ssr: {
noExternal: ['pg', 'mongoose']
}
Alternatively, you may need to restructure your app to handle database queries server-side or use serverless functions to interface with the databases.
If you continue to run into issues, consider testing both MongoDB and PostgreSQL with a simpler app setup to isolate the problem.
Would be curious to see how you go, let us know!
Hey Community! As the holiday season unfolds, we want to extend heartfelt thanks to a...
By JasonH Dec 6, 2024Dropshipping, a high-growth, $226 billion-dollar industry, remains a highly dynamic bus...
By JasonH Nov 27, 2024Hey Community! It’s time to share some appreciation and celebrate what we have accomplis...
By JasonH Nov 14, 2024