skip to Main Content

I try deploy my API project into railway but I get an error

nodemon src/config/server.js

[nodemon] 3.0.1

[nodemon] to restart at any time, enter rs

[nodemon] watching path(s): .

[nodemon] watching extensions: js,mjs,cjs,json

[nodemon] starting node src/config/server.js

node:internal/errors:478

ErrorCaptureStackTrace(err);

^

Error [ERR_MODULE_NOT_FOUND]: Cannot find module ‘/app/src/middleware/inputValidationMiddleware.js’ imported from /app/src/routes/authRoute.js

at new NodeError (node:internal/errors:387:5)

at finalizeResolution (node:internal/modules/esm/resolve:330:11)

at moduleResolve (node:internal/modules/esm/resolve:907:10)

at defaultResolve (node:internal/modules/esm/resolve:1115:11)

at nextResolve (node:internal/modules/esm/loader:163:28)

at ESMLoader.resolve (node:internal/modules/esm/loader:837:30)

at ESMLoader.getModuleJob (node:internal/modules/esm/loader:424:18)

at ModuleWrap. (node:internal/modules/esm/module_job:76:40)

at link (node:internal/modules/esm/module_job:75:36) {

code: ‘ERR_MODULE_NOT_FOUND’

}

[nodemon] app crashed – waiting for file changes before starting…

then go to the Files research to found what problem

InputValidationMiddleware.js

 static validateLoginInput(req, res, next) {
    const { email, password, userName } = req.body;
    const query = email ? email : userName;

    if (validator.isValidEmail(query)) {
      const emailErrors = validator.validateEmail(email);
      if (emailErrors.length > 0) {
        return res.status(400).json({
          message: "Email is invalid",
        });
      }
    } else {
      const userNameErrors = validator.validateUserName(userName);
      if (userNameErrors.length > 0) {
        return res.status(400).json({
          message: "Username is invalid",
        });
      }
    }

and get the authRoute.js

import { Router } from "express";
import authController from "../controllers/authController.js";
import InputValidationMiddleware from "../middleware/inputValidationMiddleware.js";

const router = Router();

/**
 * @swagger
 * /api/register:
 * post:
 * summary: Register a new user
 * description: Register a new user
 * requestBody:
 * content:
 * application/json:
 * schema:
 * type: object
 * properties:
 * userName:
 * type: string
 * email:
 * type: string
 * password:
 * type: string
 * required:
 * - userName
 * - email
 * - password
 * responses:
 * 201:
 * description: User created successfully
 * 409:
 * description: User already exists
 * 500:
 * description: Internal server error
 * */

router.post(
  "/register",
  InputValidationMiddleware.validateRegisterInput,
  authController.createUser
);

/**
 * @swagger
 * /api/login:
 * post:
 * summary: Login a user
 * description: Login a user
 * requestBody:
 * content:
 * application/json:
 * schema:
 * type: object
 * properties:
 * userName:
 * type: string
 * email:
 * type: string
 * password:
 * type: string
 * required:
 * - email
 * - password
 * responses:
 * 200:
 * description: User logged in successfully
 * 401:
 * description: Invalid email or password
 * 500:
 * description: Internal server error
 * */
router.post(
  "/login",
  InputValidationMiddleware.validateLoginInput,
  authController.loginUser
);

export default router;

can anyone help me to catch the error ??

Maybe model not found or files but it all here not missing

2

Answers


  1. Like the error message suggests the issue is that Railway is not able to find the InputValidationMiddleware module when starting up. Your code snippets seems to be correct. So I cannot see something wrong on code side.

    A few things to check:

    • Make sure InputValidationMiddleware.js is in the src/middleware folder in your Railway project. Railway sets the working directory to /app so paths need to be relative to there. Also try using a relative import instead of bare import:

    • Make sure InputValidationMiddleware.js is included in the files array in railway.yml so Railway knows to deploy it.

    • While you didn’t show your package.json file, it’s important to ensure that all required dependencies are listed and installed.

    Hope this helps troubleshoot!

    Login or Signup to reply.
  2. It seems to me that there might be a problem with your InputValidationMiddleware.js file. It would be better if you provided more details to this file, since it looks like you could export it incorrectly. Also, my suggestion is that you defined validateLoginInput function in some class, taking into account a static keyword.

    Anyway, in case if it’s just a single function then you should rewrite it like this:

    function validateLoginInput(req, res, next) {
       // your code here
    };
    
    export default validateLoginInput;
    

    Then import it like this:

    import validateLoginInput from "../middleware/inputValidationMiddleware.js";
    

    But if you have some sort of a class like InputValidationMiddleware for instance, then you would have to export your class using export default before its definition and leave the import statement as it is in your authRoute.js file.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search