IntroductionAs node developers, we all know what the require() function does. But how many of us actually know how it works? We use it every day khổng lồ load libraries và modules but its behavior is still a mystery for us, so let’s solve sầu this mystery…
We will cover the below topics
Why we use “require”?
require() is used lớn consume modules. It allows you to include modules in your tiện ích. You can add built-in core Node.js modules, community-based modules (node_modules), và local modules too.
var invisible = function () console.log("invisible");exports.message = "Third rock techkno";exports.say = function () console.log(exports.message);So if you run var example = require("./example.js"), then example.js will get evaluated, & then example be an object equal to:
message: "Third Rochồng techkno", say:
module.exports = function () console.log("hello world")require("./example2.js")() //require itself và run the exports objectIt is worth noting that each time you subsequently require an already-required tệp tin, the exports object is cached & reused. To illustrate this point:
node> require("./example.js")evaluating example.js message: "hi", say:
The most important functions khổng lồ kiểm tra here are_load_compileModule._loadThis function checks whether the module is in the cabít already - if so, it returns the exports object.
If the module is native, it calls the NativeModule.require() with the filename and returns the result.
Otherwise, it creates a new module for the tệp tin and saves it to lớn the cabít. Then it loads the file contents before returning its exports object.Module._compile
The compile function runs the file contents in the correct scope or sandbox, as well as exposes helper variables lượt thích require, module, or exports to the tệp tin.
The actual loading of any module using require() happens in five steps
The first step resolution is an enclosed step wherever node.js calculates the tệp tin methods etc. in the second step that is loading, the node pulls the code within the current method. In wrapping, phase wraps up the code within the operate as shown higher than so sends it to VM for evaluating so eventually catches it.
So, primarily node is never aware of what symbols a comtháng JS module goes to lớn export till and unless the module is truly evaluated. And this is often the largest distinction with ECMAScript modules, as a result of ESM is lexical & so, the exported symbols are better known before the code is truly evaluated.
The require function will look for files in the following orderBuilt-in core Node.js modules (like fs, path)NPM Modules. It will look in the node_modules thư mục.Local Modules. If the module name has a ./, / or ../, it will look for the directory/tệp tin in the given path. It matches the file extensions: *.js, *.json, *.mjs, *.cjs, *.wasm và *.node.
When you install node, it comes with many built-in modules.
Some of the most used core modules are:
fs: Allows you to manipulate (create/read/write) files & directories. path: utilities to lớn work with files and directories paths.
NPM modules are 3rd-tiệc nhỏ modules that you can use after you install them. To name a few:
lodash: a collection of utility functions for manipulating arrays, objects, và strings. request: HTTP. client simpler to lớn use than the built-in HTTPhường module. express HTTPhường server for building websites and API. Again, simpler lớn use than the built-in HTTP module.
Creating your own NodeJs modules
If you can’t find a built-in or 3rd-các buổi party library that does what you want, you will have sầu to lớn develop it yourself and import it the same way as built-in và npm-modules.
We are a team of expert developers, testers and business consultants who strive to deliver nothing but the best. Planning to lớn build a completely secure and efficient Nodejs app? "Hire NodeJS Developers".
· · · ·
Third Roông xã Techkno is a leading IT services company. We are a top-ranked website, voice, và thiết bị di động tiện ích development company with over 10 years of experience. Client success forms the core of our value system.
We have expertise in the lademo technologies including angular, react native sầu, iOs, Android, & more. Third Roông xã Techkno has developed smart, scalable, và innovative sầu solutions for clients across a host of industries.
Our team of dedicated developers combines their knowledge và skills khổng lồ develop & deliver website & Mobile apps that boost business & increase output for our clients.
Looking For Expert Guidance on Your Dream Project?
Our diverse team of industry leading veterans can help you build the most viable solution.Schedule a không lấy phí consultation gọi
Krunal Shah is the CTO và Co-founder at Third Roông chồng Techkno. With extensive experience gained over a decade, Krunal helps his clients build software solutions that stvà out in the industry & are lighter on the pocket.
May 13, 2021 | Min ReadHow lớn hire front-end development in 2021?Front over development is necessary lớn engage the clients & helps in expanding the business horizon rapidly.Read More
May 18, 2021 | Min ReadVoice Assistant Apps for Contactless Transport Booking SystemGoogle Assistant is all set to lớn facilitate Voice Assisted contactless transport booking. Cliông xã khổng lồ learn how cab booking uses virtual voice assistants và much more here.
Jun 09, 2021 | Min ReadTop 10 Famous Websites Built with React JS in 2021ReactJS development is here lớn stay & sites with ReactJS render impeccable results for businesses worldwide.
subscribe lớn our newsletter
Join our mailing danh sách lớn receive sầu the lademo news và updates from our team.
Work With UsFind Us
India Office103-104, Sarita Complex, Jain Temple Lane, Opp. Municipal Market, C.G. Road, Navrangpura, Ahmedabad - 380009, Gujarat, India.