Evil Team-LoL
Evil Team-LoL

Reputation: 11

ERR into railway deploy JS project

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

Upvotes: 0

Views: 144

Answers (1)

3ternal
3ternal

Reputation: 21

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.

Upvotes: 0

Related Questions