Problem deploying React client + Express server, can't find module, build failed

I’m trying to re-deploy an old project that was on Heroku over here. It’s has a React client that handles the routing, and an Express server to handle server side functions. I used this Build Command: “cd client && npm install && npm run build”, but when it tried to run the server command it failed:

==>  Build successful 🎉
==> Deploying...
==> Using Node version 12.18.3 via environment variable NODE_VERSION
==> Docs on specifying a Node version: https://render.com/docs/node-version
==> Starting service with 'node server.js'
  internal/modules/cjs/loader.js:968
    throw err;
  Error: Cannot find module 'express'

I then changed the build command and tried to include ‘npm install’ on the server side as well as the client, and got an error about “Unsupported engine for node-gyp@9.1.0: wanted: {“node”:”^12.22…"

Does anyone have any tips or guidance on deploying this type of app

Hi Jordan,

Since you cding into client, could you check that you have a package.json file that includes express in your /client directory?

Regards,

Matt

Hi Matt, thanks for the quick reply!

I only have Express in the base directory’s package.json, it is not in the Client folder’s package.json.
The React app is in “/client” and the Express server.js in “/”
this is how the Express server.js is set up:

// make our client directory the default directory to serve static assets
if (process.env.NODE_ENV === 'production') {
  app.use(express.static(path.join(__dirname, 'client/build')));
  // for any route requested, that is not otherwise specified, respond with this file.
  app.get('*', function (req, res) {
    res.sendFile(path.join(__dirname, 'client/build', 'index.html'));
  });
}
....
app.post('/payment', (req, res) => {...};

Hi Jordan,

In that case, it looks like you might be trying to start the wrong service. Are you cding into the client directory in your start command?

Sounds like you might benefit from out Monorepo functionality. See https://render.com/docs/monorepo-support for more information on that.

Regards,

Matt

I’m pretty sure it’s the right service, because I’m trying to first, cd into the Client folder, install packages and build the react app, then on the start command I run the Express server with “node server.js” which should route almost every request to the built react app in “/client/build” folder (as shown in the code in the previous comment). The react build succeeds, but I get an issue either with Express module not loading or wrong node version on the start command.
like this:

npm ERR! notsup Required: {"node":"12.22.12","npm":"6.14.6"}
npm ERR! notsup Actual:   {"npm":"6.14.16","node":"12.22.12"}

This is an old project I’d really like to get working again for my portfolio, since most of my projects are also outdated and broken, smh…

Thanks for all your help!

Hi Jordan,

The problem is you’re trying to run server.js in your project’s root directory, which requires express. But since you cd into client before running npm install, the dependencies in your root directory are not being installed.

Maybe you need to add an extra npm install to your build command. I.e. npm install && cd client && npm install && npm run build

Regards,

Matt

Sadly I got the same error as in my last message (about the node and npm versions), this time right away, instead of after the building the client. rearranging my package.json to see if this goes away, other than that I asked about this in another forum as well and I think I’ll try to update some dependencies to see if that helps.

Alright, I was able to fix it!
I had to make sure I had the right build command and Node and NPM versions that worked with Render and my app, then make sure they were correctly noted in my package.json.

Render used Node 12.22 and NPM 6.14.16, and my app was using Node 12.14 and NPM 6.14.6. I overlooked the difference in NPM versions, so once I fixed that I was able to install the packages and run the build and start commands successfully.

I made sure to install packages in both the root folder and the client folder:
Build: “npm install && cd client && npm install && npm run build”
Start: “node server.js”

Thanks so much for your help Matt!!!

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.