Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript Install and Use Sequelize CLI

Dilip Agheda
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Dilip Agheda
Full Stack JavaScript Techdegree Graduate 28,581 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
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Brendan Whiting
Front End Web Development Techdegree Graduate 84,738 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.