Have your say in Community Polls: What was/is your greatest motivation to start your own business?

Shopify hydrogen database connection issue

Shopify hydrogen database connection issue

moh5
Shopify Partner
1 0 1

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

 

 

Reply 1 (1)

squamulose
New Member
5 0 0

 

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:

MongoDB Connection Issues:

  1. "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.

PostgreSQL Issues:

  1. "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:

    • Disabling SSR bundling for PostgreSQL or using external dependencies correctly by configuring ssr.noExternal in Vite config:

 

 

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!