Cannot Find Module '. .. /Models/User' Nodejs

I've got a problem with require. I've got a users.js inside folder 'router'. In users.js everything works great with

let User = require('../models/user');

But when users.js file getting bigger I wanted to part this file into a few files like login.js regiseter.js and so on. I created a folder 'users' inside 'router' and put there those files (login.js, register.js)

I changed my require to

let User = require('.../models/user');

Now I got an error:

Cannot find module '.../models/user' enter image description here

2

1 Answer

Seems like your require for login.js and register.js should now be:

let User = require('../../models/user');

This will step down two directories before opening the models folder. This assumes from your post the initial structure was:

|-- app/
    |-- router/
        |-- users.js
    |-- models/
        |-- user.js

and the new structure is:

|-- app/
    |-- router/
        |-- users/
            |-- login.js
            |-- register.js
        |-- users.js
    |-- models/
        |-- user.js
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Sophia Al-Mansoor

Sophia Al-Mansoor

Global Business & E-Commerce Reporter

Sophia analyzes international trade, startup ecosystems, retail transformation, and supply chain logistics for modern digital publications.

Share this article
Twitter Facebook Pinterest