Need help with file structure for deployment
sydneypatel
HOBBYOP

6 months ago

I keep seeing this error, though my project has a backend/server.js.

However, my route is in frontend/ because thats where the package.json is.

> jnr-management@0.0.0 start

> node server.js

node:internal/modules/cjs/loader:1143

throw err;

^

Error: Cannot find module '/app/server.js'

$10 Bounty

17 Replies

Railway
BOT

6 months ago

Hey there! We've found the following might help you get unblocked faster:

If you find the answer from one of these, please let us know by solving the thread!


vivekvt
HOBBY

6 months ago

can you share your full package.json file.

do you have backend folder inside your frontend folder? like frontend/backend/server.js

how many package.json files do you have?


sam_sd
PRO

6 months ago

Railway’s looking for server.js in /app/, but it’s not there.

That’s usually because your package.json is in frontend/, so when it runs npm start it tries to find server.js inside that folder instead of your backend.

Quick fixes:

Move server.js into the same folder as the package.json Railway is using.

Or update the start script to point to your backend file:

"start": "node ../backend/server.js"

Or set Railway’s start command to run from the backend folder:

cd backend && npm start

Basically, just make sure your start command knows where server.js actually lives.


sydneypatel
HOBBYOP

6 months ago

I have three package.json folders, one in root, and one in frontend and backend.

I added a package.json to my root folder to app/ would work. This is what it has:

{
  "name": "jnr",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "node backend/server.js"
  },
  "engines": {
    "node": "18.x"
  },
  "repository": {
    "type": "git",
    "url": "git+https://github.com/sydneypatel/jnr-hotels-website.git"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "type": "commonjs",
  "bugs": {
    "url": "https://github.com/sydneypatel/jnr-hotels-website/issues"
  },
  "homepage": "https://github.com/sydneypatel/jnr-hotels-website#readme",
  "dependencies": {
    "mongodb": "^6.18.0"
  }
}

However, now my backend isnt loading and this is what I see when the website is live:
https://www.jnrcapitalpartners.com/: Backend is running. Try /api/health.

and api/health
https://jnr-hotels-website-production.up.railway.app/api/health: {"ok":true,"db":1,"time":1754916706239}


clashing
HOBBY

6 months ago

Your root folder's package.json is serving backend/server.js. which is most probably a server file, and that is why you are seeing the same server response.

Please provide the screenshot of the file that you intened to serve as the frontend, because the backend seems to work fine

Attachments


sydneypatel
HOBBYOP

6 months ago

package.json in root:

{
  "name": "jnr",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "node backend/server.js"
  },
  "engines": {
    "node": "18.x"
  },
  "repository": {
    "type": "git",
    "url": "git+https://github.com/sydneypatel/jnr-hotels-website.git"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "type": "commonjs",
  "bugs": {
    "url": "https://github.com/sydneypatel/jnr-hotels-website/issues"
  },
  "homepage": "https://github.com/sydneypatel/jnr-hotels-website#readme",
  "dependencies": {
    "mongodb": "^6.18.0"
  }
}

package.json in frontend:

{
  "name": "jnr-management",
  "private": true,
  "version": "0.0.0",
  "type": "module",
  "scripts": {
    "dev": "vite",
    "build": "vite build",
    "lint": "eslint .",
    "preview": "vite preview",
    "start": "node backend/server.js",
    "postinstall": "npm run build"
  },
  "dependencies": {
    "@emotion/react": "^11.14.0",
    "@emotion/styled": "^11.14.0",
    "@mui/icons-material": "^7.2.0",
    "@mui/material": "^7.1.1",
    "axios": "^1.11.0",
    "framer-motion": "^12.23.7",
    "jwt-decode": "^4.0.0",
    "react": "^19.1.0",
    "react-dom": "^19.1.0",
    "react-icons": "^5.5.0",
    "react-router": "^6.30.1",
    "react-router-dom": "^6.30.1",
    "slugify": "^1.6.6",
    "styled-components": "^6.1.18"
  },
  "devDependencies": {
    "@eslint/js": "^9.32.0",
    "@types/react": "^19.1.2",
    "@types/react-dom": "^19.1.2",
    "@vitejs/plugin-react": "^4.4.1",
    "eslint": "^9.32.0",
    "eslint-plugin-react": "^7.37.5",
    "eslint-plugin-react-hooks": "^5.2.0",
    "eslint-plugin-react-refresh": "^0.4.19",
    "globals": "^16.3.0",
    "prettier": "^3.6.2",
    "vite": "^6.3.5"
  }
}

package.json in backend:

{
  "name": "backend",
  "version": "1.0.0",
  "type": "module",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "description": "",
  "dependencies": {
    "bcrypt": "^6.0.0",
    "cors": "^2.8.5",
    "dotenv": "^17.2.1",
    "express": "^5.1.0",
    "jsonwebtoken": "^9.0.2",
    "mongoose": "^8.17.0"
  }
}

sydneypatel
HOBBYOP

6 months ago

Also, my backend is running but the api requests and not returning anything, like my website pages content are empty


sydneypatel

Also, my backend is running but the api requests and not returning anything, like my website pages content are empty

sydneypatel
HOBBYOP

6 months ago

I get responses like these: Cannot GET /news


sydneypatel

package.json in root:{ "name": "jnr", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1", "start": "node backend/server.js" }, "engines": { "node": "18.x" }, "repository": { "type": "git", "url": "git+https://github.com/sydneypatel/jnr-hotels-website.git" }, "keywords": [], "author": "", "license": "ISC", "type": "commonjs", "bugs": { "url": "https://github.com/sydneypatel/jnr-hotels-website/issues" }, "homepage": "https://github.com/sydneypatel/jnr-hotels-website#readme", "dependencies": { "mongodb": "^6.18.0" } }package.json in frontend:{ "name": "jnr-management", "private": true, "version": "0.0.0", "type": "module", "scripts": { "dev": "vite", "build": "vite build", "lint": "eslint .", "preview": "vite preview", "start": "node backend/server.js", "postinstall": "npm run build" }, "dependencies": { "@emotion/react": "^11.14.0", "@emotion/styled": "^11.14.0", "@mui/icons-material": "^7.2.0", "@mui/material": "^7.1.1", "axios": "^1.11.0", "framer-motion": "^12.23.7", "jwt-decode": "^4.0.0", "react": "^19.1.0", "react-dom": "^19.1.0", "react-icons": "^5.5.0", "react-router": "^6.30.1", "react-router-dom": "^6.30.1", "slugify": "^1.6.6", "styled-components": "^6.1.18" }, "devDependencies": { "@eslint/js": "^9.32.0", "@types/react": "^19.1.2", "@types/react-dom": "^19.1.2", "@vitejs/plugin-react": "^4.4.1", "eslint": "^9.32.0", "eslint-plugin-react": "^7.37.5", "eslint-plugin-react-hooks": "^5.2.0", "eslint-plugin-react-refresh": "^0.4.19", "globals": "^16.3.0", "prettier": "^3.6.2", "vite": "^6.3.5" } }package.json in backend:{ "name": "backend", "version": "1.0.0", "type": "module", "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "keywords": [], "author": "", "license": "ISC", "description": "", "dependencies": { "bcrypt": "^6.0.0", "cors": "^2.8.5", "dotenv": "^17.2.1", "express": "^5.1.0", "jsonwebtoken": "^9.0.2", "mongoose": "^8.17.0" } }

clashing
HOBBY

6 months ago

You forgot to add the start command for your server, so that your correct file can be used to serve as the backend!
Add the following line in the backend's package.json (inside the scripts object):

start: node index.js

I hope that would solve the issue


sydneypatel
HOBBYOP

6 months ago

No, that didn't work unfortunately


sydneypatel
HOBBYOP

6 months ago

If I make my git repo public so you can view it, do you think that could help me resolve the issue?


clashing
HOBBY

6 months ago

Just make 3 separate repositories for 3 services, and place their respective package json files in the root directory.

And make sure they have the start script command in the package file.

That would work for sure


sydneypatel
HOBBYOP

6 months ago

Wait, I don;t understand what you mean by 3 services


clashing
HOBBY

6 months ago

Deploy your backend, frontend as separate services (not just clubbing everything as you are doing as of now).

So there would be 2 githun repos: one for front-end and the other one for the server.

Already mentioned the same in the earlier post. That's all from my side


sydneypatel
HOBBYOP

6 months ago


jitmisra
HOBBY

6 months ago

This error is happening because your start command, node server.js, is looking for the server.js file in the main project folder, but your file is actually located inside the backend subfolder.

You have two easy ways to fix this, depending on how you want to structure your project.

Option 1: Fix the Start Command (The Quickest Fix)

You can simply update your start command to include the correct path to your server.js file.

  1. Open your package.json file.

  2. Find the scripts section.

  3. Change the start script to point to the file inside the backend folder.

Before:

JSON

"scripts": {
  "start": "node server.js"
}

After:

JSON

"scripts": {
  "start": "node backend/server.js"
}

Commit and push this change to your repository, and Railway will deploy it correctly.

Option 2: Set the Root Directory (The Best Fix for Monorepos)

This is the recommended approach if your repository contains separate frontend and backend folders and you are deploying them as different services. You can tell Railway to treat the backend folder as the main directory for this specific service.

  1. In your Railway project, go to the service that is failing.

  2. Click on the Settings tab.

  3. Under the Service section, find the Root Directory field.

  4. Enter backend into this field and save the changes.

  5. Trigger a new deployment.

With this setting, Railway will run all commands from within the /backend folder, so your original node server.js start command will work without any changes to your package.json.


sydneypatel
HOBBYOP

6 months ago

I already have that in my package.json in my root folder. My main issue now is that my backend isn't loading the data to the frontend, so I can't see my portfolio, news, gallery, or past fund data.


Loading...