Deploy Node web app Error

Hello
I am new to Render and have encountered an issue while trying to deploy my repository to Render. I’m not quite sure what steps to take as I’ve only tested my web app locally.
Any help is appreciated.
Thanks in advance

Hi there,

Thanks for reaching out.

The error seems pretty clear:

Error: Cannot find module '/opt/render/project/src/index.js'

It’s looking for index.js in the root of your project, a couple of things to check:

  • Is the index.js in the root of your GitHub repo or is it in a subfolder?

  • Is the casing correct? (e.g. is it Index.js?)

Hope that helps

Alan

1 Like

Thanks for the reply

I’ve checked my repo and see that, indeed, ‘index.js’ is not at the root.
However, the file does exist within a folder called ‘src’ in the root.

Does this file path not point to the folder?

Would the correct file path look like this?
'/opt/render/project/src/src/index.js ’

Or is there a better way to point to the file ‘index.js’

Thanks!

Your code is stored in /opt/render/project/src/ on Render Native Environments.

It looks like this was coming up from a Start Command of node index.js So if the file in within a src folder in your repo, the Start Command would need to be node ./src/index.js

Alan

Hi. Thanks for the suggestion.
I made the changes for my Start Command and it looks like the deploy works until I hit another error:

How would I configure my package.json file so this resolves, or is there something else that I need to be doing so that I am able to import files for my app?
I’m guessing that the way my files are packaged or configured to run on Render isn’t optimal for what I’m trying to do…

Thanks

Again the error seems pretty clear:

SyntaxError: Cannot use import statement outside a module

And it includes a suggested a solution:

Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.

It seems adding "type": "module" to the top level of your package.json may resolve that issue. Searching for these errors brings up quite a lot of resources, as it appears to be a common NodeJS stumbling block.

Other things to consider when deploying your projects:

  • Match your local/development package manager in the Build Command. If you use yarn locally, use that on Render. Or swap it for npm if that’s your chosen manager.

  • Still around the Build Command; maybe your project needs building for production? e.g. a Build Command of npm install; npm run build;

  • Match your local/development Node version. This can be set in various ways, more in docs: Specifying a Node Version

Alan

Hi
Thanks for the suggestion. I’ve made the following changes with some success:

  • changed Build Command to ‘npm run build’
  • changed Start Comman to ‘npm start’
  • deleted “type”: “module” in my package.json file as that was giving me errors

The build seems to be succesful as seen in the logs:

However it seems strange that my project is running as ‘localhost’. Is there some configuration here I need to do so that my app will run via a Render host?

Ultimately, the deployment is determined as ‘failed’ even though the final statement in the log indicates ‘Compiled Succesfully’ (not seen in picture above).

I think I’m getting closer to a succesful deployment, but again, there’s something missing…

On a Web Service your Build Command would still need to install dependencies, so like my previous example, it should probably be npm install; npm run build;

The start script appears to be starting webpack-dev-server, you wouldn’t want to use that in production.

I’m getting the impression this project is actually a Static Site. Maybe that’s worth a try?

With a Static Site your dependencies are installed automatically, so your Build Command would just be npm run build

There’s no Start Command on a Static Site, just the Publish Directory, which with webpack defaults to dist

Alan

Hi
I took a closer look at my code and saw that a few packages needed some updating. I also realized that I was pointing at the incorrect repo.

However, this does not change too much in regards to deploying my project via Render - I am still running a static site that is based in Node. After deploying my project, I get a different error:

This indicates I do not have react-scripts in my packages, even though I am quite certain they exist when I am able to run my app locally - where I’ve checked my ‘package.json’ file I’ve made sure to update ‘react-scripts’ to most recent possible.

What could be the cause of failing to load the react-script?

Hi.

I found your service, but there are still issues that I’ve covered before:

  • You are using a Web Service & not a Static Site, that’s fine if this is a Web Service e.g. served with Express. But if it has no server-side requirements, you may be better off with a Static Site

  • Your Build Command is just npm run build, with a Web Service you need to install your own dependencies. npm install; npm run build

Your repo is public and looks like a Create React App project. We have a guide on that in the docs: Deploy a Create React App Static Site

  1. New > Static Site
  2. Connect Repo
  3. Give it a name
  4. Build Command: npm run build (dependencies are installed automatically on Static Sites)
  5. Publish Directory: public

Alan

Hi
Thanks for the reminder about the Build Command.
I did not have ‘npm install’ with my ‘npm run build’.
After including both commands, I’ve succesfully deployed my app!

My site does use a Node back-end that accesses a database and connects with an API of another site, so I was pretty sure that the Web Service was what I needed.

Thank you for your help.

From the look of your repo, your API interactions are client-side, so you don’t need a Web Service with this static React project. You’re also still starting a development server for production use, which probably won’t perform as you expect. Obviously, you know your project better than me, so I’m glad to hear you’re up and running.

Alan

How would I start my app as a production server?

My purposes for this project is simply to display my app in action. It is in no way a full-on app that is a business or otherwise heavily relied upon. Would a production server be over kill?
I did notice that it takes a very long time (by web standards) for the site to be active from clicking/opening the URL.

Also, I use a database for User logins and registration. I’m in the process of setting up a PostGRS database with Render and configuring my code so it will use the Render database instead of the current database on Heroku.
Would my site still be considered Static?

It looks like a static site, so you could try deploying to a Static Site as I outlined in detail above.

If the app isn’t completely static, then you should look at the deployment docs of Create React App: https://create-react-app.dev/docs/deployment/#other-solutions

Alan