I'm importing a module in my Sapper application, but I'm getting the error Cannot find module '../models/User'
Imported with const User = require('../models/User');
Exported as module.exports = User = mongoose.model('user', UserSchema);
What am I missing to make this work? The path is correct according to vscode.
'../models/User.js':
const mongoose = require('mongoose');
const UserSchema = new mongoose.Schema({
name: {
type: String,
required: true,
},
email: {
type: String,
required: true,
unique: true,
},
password: {
type: String,
required: true,
},
date: {
type: Date,
default: Date.now,
},
});
module.exports = User = mongoose.model('user', UserSchema);
EDIT:
I re-created the error by starting a new project with npx degit "sveltejs/sapper-template#rollup" my-app
.
I'm getting the same error even though the project is new, and I haven't installed any additional dependencies link.
The path is correct, as confirmed by vscode
I have no much experience with Sapper but as far as I could check the problem is that the actual call is made by __sapper_/dev/server/server.js
and not the one from your src
folder. If you use const User = require('../../../models/User')
it should work but you'll need to deal with this deep relative path.
You gonna need to either export your models folder to a path closer to that file or come up with a function to determine the root path and use as a prefix when requiring your models, like const User = require(rootPath() + './models/User')
.