JavaScript Install and Use Sequelize CLI

Dilip Agheda
Dilip Agheda
Full Stack JavaScript Techdegree Graduate 28,578 Points

How does this work? var sequelize = require('../models').sequelize; I mean how does js find sequelize?

How does this work? var sequelize = require('../models').sequelize;

I mean how does js find sequelize?

1 Answer

Brendan Whiting
Brendan Whiting
Front End Web Development Techdegree Graduate 84,616 Points

We installed sequelize with npm install sequelize. So npm fetched the sequelize code and put it in the node_modules folder. Then under /models there's an index.js file where we import the sequelize npm package, create an instance, do some configuration, and at the bottom of the file we end up assigning an object called sequelize to the exports.

So when you do require('../models').sequelize, it's going to look in ../models/index.js because that's the default behavior when you don't specify a file name. And it's going to get that sequelize object from the exports that was configured.