EN VI

Reactjs - Failed to compile module not found in React.js?

2024-03-17 17:00:05
How to Reactjs - Failed to compile module not found in React.js

I ran into this problem website when am following this video tutorial until here: https://youtu.be/nGoSP3MBV2E?list=PLIZkq64ad9C4POK0E1fhCaxlxTXwr7wSh&t=5976.

The problem seem to arised from these 2 files: error from register/route.js & models/User.js

Here's the source code of those 2 files:

register/route.js: '''

  import mongoose from "mongoose";

    import {User} from "@/models/User";;

    export async function POST(req){
    const body = await req.json();
    mongoose.connect(process.env.MONGO_URL);
    const cretedUser = await User.create(body);
    return Response.json(createdUser);
}

'''

Model/User.js:

'''

    import {model, models, Schema} from "mongoose";

    const UserSchema = new Schema({
    email:{type:String, required:true, unique: true},
    password:{
        type:String, 
        required: true, 
        validate: pass => {
        if(!pass?.length || pass.length <5 ){
            new Error('password must be at least 5 characters');
        }
    },
    },
    },{timestamps: true}); 

export const User = models?.User || model('User', UserSchema);

'''

I have tried correcting any typo as well as fixing any incorrect path but nothing worked so far, here's how my repo look like:Repo

Solution:

This is a Next.js app and the User.js file is located in src/app/models/User.js.

The import should be import {User} from "@/src/app/models/User.js";

Read more about Next.js Path Aliases here: https://nextjs.org/docs/pages/building-your-application/configuring/absolute-imports-and-module-aliases

Answer

Login


Forgot Your Password?

Create Account


Lost your password? Please enter your email address. You will receive a link to create a new password.

Reset Password

Back to login