3

i can start my node app fine on my local machine but if i then pull the project from github on my server and start it with npm start i get this error:

Cannot find module '../models/user'.

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

registrationController.ts is trying to access the models/user.

This is how i import in that file:

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

This is my folder structure. I get the error in the highlighted file:

enter image description here

This is my npm start script:

"scripts": {
   "start": "nodemon --watch '*.ts' --exec 'ts-node' app.ts"
}
SwiftiSwift
  • 7,528
  • 9
  • 56
  • 96

3 Answers3

2

This isn't the correct syntax to use your user model into the controllers, you should assign the required model directly to a variable (either a const or var) as follow:

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

This link has great details regarding the two ways (import and require).

Orleando Dassi
  • 454
  • 6
  • 17
1

It looks like mixing the es6 syntax with older syntax

use this way if you are exporting that module as a default export

import User from '../models/user'; 

otherwise use

const User = require('../models/user')

make sure you are exporting the module like this

module.exports = <Your Module>
Dulara Malindu
  • 1,477
  • 4
  • 18
  • 37
0

My filename was User.ts on bitbucket instead of user.ts

I pushed it to bitbucket before with user.ts but the filename hasn't changed there!!! So i had to remove the whole project from bitbucket

SwiftiSwift
  • 7,528
  • 9
  • 56
  • 96