![]() ![]() The new object only contains the function, so the value1 is no longer exported.Įxporting values with just the exports keyword is a quick way to export values from a module. is a way of reassigning a new object to the exports property. It only has the function exported using module.exports. The second log does not contain the value1 export anymore.By doing exports.value1 = value1, it added the value1 property to the module.exports object, as you can see in the first log. The exports keyword is a reference to the exports object in the modules object.Say, for example, that you have a utility file utility.js with the following code: // utility.jsĬonst replaceStr = (str, char, replacer) => , Node.js treats each file in a Node project as a module that can export values and functions from the file. Node.js already exports in-built modules which include fs, path, and http to name a few. If you want to learn more about ES Modules (along with CommonJS modules), you can check out this in-depth guide. Node.js supports CommonJS Modules and ECMAScript Modules.įor the rest of this article, we'll focus on CommonJS Modules, the original approach to packaging modules in Node.js. When you export a module, you can import it into other parts of your applications and consume it. Or they can even provide constants and primitive values. ![]() They can provide methods for making API requests. They can provide simple utilities to modify strings. You'll want to export modules so that you can use them in other parts of your application. In this article, I will show you how to share functions and values by exporting and importing modules in Node.js. These values can also be shared across the entire program and can be used in different ways. In programming, modules are components of a program with one or more functions or values. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |