Home > Back-end >  App crashing with no error message from imported function typescript
App crashing with no error message from imported function typescript

Time:02-03

When I import a helper function called insertStudent in my controller for authorization and register the route with the controller the app crashes with no error message. The controller function (signup) isn't even being run. Also when I try out the helper function without the controller but just by itself it seems to work perfectly fine. What could be causing the crash? Here's the code:

Imported helper function in database.ts:

export const insertStudent = async (
    firstName: string,
    middleName: string,
    lastName: string,
    email: string,
    password: string,
    numberInGrade: number,
    gradeId: number
): Promise<Student> => {
    const [rows] = await db.query(
        "INSERT INTO students (first_name, middle_name, last_name, email, password, number_in_grade, grade_id) VALUES (?, ?, ?, ?, ?, ?, ?); SELECT * FROM students WHERE id = LAST_INSERT_ID();",
        [
            firstName,
            middleName,
            lastName,
            email,
            password,
            numberInGrade,
            gradeId,
        ]
    );

    return rows[1][0] as unknown as Student;
};

Controller function signup for the autorization:

import {
    insertStudent
} from "../database.js";

export async function signup(req: Request, res: Response) {
    const { userType, firstName, middleName, lastName, email, password } =
        req.body;

    if (
        !userType ||
        !firstName ||
        !middleName ||
        !lastName ||
        !email ||
        !password
    )
        return res.status(400).send({ message: "Please fullfil all fields." });

    if (userType === "student") {
        const { numberInGrade, gradeId } = req.body;

        if (!numberInGrade || !gradeId)
            return res
                .status(400)
                .send({ message: "Please fullfil all fields." });

        const hashedPassword = bcrypt.hashSync(password);

        const user = await insertStudent(
            firstName,
            middleName,
            lastName,
            email,
            hashedPassword,
            numberInGrade,
            gradeId
        );

        delete user.password;

        return res
            .status(201)
            .send({ message: "Student created successfully", user });
    }

    return res.status(400).send({ message: "Invalid user type" });
}

File where the router is located (auth.ts):

import { Router } from "express";

import { signup } from "../controllers/auth.js";

export const router: Router = Router();

router.post("/signup", signup);

export const path = "/auth";

I use the exported path and router from auth.ts to register the routes using app.use(path, router); in my main file.

I usually use nodemon to run the code but when I compile the code from TypeScript to JavaScript I get an error which says the following:

TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".map" for [PATH REDACTED]\build\routes\auth.js.map
    at new NodeError (node:internal/errors:377:5)
    at Object.getFileProtocolModuleFormat [as file:] (node:internal/modules/esm/get_format:80:11)
    at defaultGetFormat (node:internal/modules/esm/get_format:122:38)
    at defaultLoad (node:internal/modules/esm/load:21:20)
    at ESMLoader.load (node:internal/modules/esm/loader:431:26)
    at ESMLoader.moduleProvider (node:internal/modules/esm/loader:350:22)
    at new ModuleJob (node:internal/modules/esm/module_job:66:26)
    at #createModuleJob (node:internal/modules/esm/loader:369:17)
    at ESMLoader.getModuleJob (node:internal/modules/esm/loader:328:34)
    at async Promise.all (index 0) {
  code: 'ERR_UNKNOWN_FILE_EXTENSION'
}

After that I just get the message that server has started: Server started on port 8393

CodePudding user response:

The error message "TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension '.map' for [PATH REDACTED]\build\routes\auth.js.map" is indicating that the file with the '.map' extension is not recognized by Node.js. It seems like you are trying to import a source map file in your code which is not supposed to be imported.

To resolve this issue, try excluding the source map files from your build process. If you are using TypeScript, you can add the following line to your tsconfig.json file:

"sourceMap": false

If you are using Babel, you can add the following line to your Babel configuration file:

"sourceMaps": false

CodePudding user response:

Try excluding the source maps from being included in your build output, or modifying your import statement to only include the JavaScript files.

Also, in your code, in the file "auth.ts", you are importing signup from "../controllers/auth.js", which suggests that the file "auth.js" exists in the folder "controllers". However, your code file is named "auth.ts", which indicates that it is written in TypeScript. It seems like there might be a naming mismatch between your TypeScript file and the compiled JavaScript file. Double-check that the compiled JavaScript file is named correctly and is in the correct location.

Without the specific error message, it's difficult to determine the exact cause of the crash. However, here are a few things you can try:

  1. Try to wrap the app.use line in a try-catch block, so that you can log the error when it occurs:

try {
    app.use(path, router);
} catch (error) {
    console.error(error);
}

  1. Make sure that your app is using the correct paths when requiring the controller and the router. You can use absolute paths to make sure you are requiring the correct files.

  2. Check if there are any missing dependencies in your code. If any of the imported modules (e.g. bcrypt, express, etc.) are not installed, your app will crash.

  3. If all else fails, you can try debugging your code by adding console.log statements to see what is happening at different points in your code.

If you still cannot figure out the cause of the crash, it may be helpful to provide more information about your setup, such as the exact versions of the modules you are using and the environment you are running your code in.

  • Related