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')
.
When you hover over the string inside require
it will show the absoloute path of the file you're referencing. Check if module it's requiring exists by doing ls <path of odule>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.