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 trialMichael Williams
Courses Plus Student 8,059 PointsCan someone explain "require" in plain english?
Andrew talks about the require
method but doesn't explain it. So I went to the MDN link in the teacher's notes, and that wasn't much more helpful. Can someone explain (in plain English) what he's doing here with the require
method?
He starts talking about require
around the 4:30 mark.
1 Answer
Dario Bahena
10,697 Pointsrequire is a method that is part of the node.js framework. In other words, it is globally accessible. You probably won't find it in MDN because it is not part of JavaScript itself. In node, if you want to use a library (module) you can use the require method to load it and make it accessible.
hypothetical: lets say you have a library that prints "hello" called 'somelib'
const someVariable = require('somelib')
// someVariable now contains the library which can be used to access its contents.
someVariable.print() // will print "hello"
Michael Williams
Courses Plus Student 8,059 PointsMichael Williams
Courses Plus Student 8,059 PointsThat's precisely the answer I was looking for. Thanks!