To include external .js file in Node.js app, we create a module.
For instance, we write
const make = (Schema, mongoose) => {
CarSchema = new Schema({
brand: String,
type: String,
});
mongoose.model("Car", CarSchema);
};
module.exports.make = make;
in car.js to export the make function.
Then we use it by writing
const app = require("express").createServer();
const mongoose = require("mongoose");
const Schema = mongoose.Schema;
const ObjectId = Schema.ObjectId;
require("./models/car.js").make(Schema, mongoose);
We call require to require the car.js file and call make from the returned object.