adds code


This tutorial explains the default method of exporting modules and then importing / requiring them inside your projects. Node.js comes with a set of built-in modules that we can use in our code without having to install them. To do this, we need to require the module using the require keyword and assign the result to a variable. This can then be used to invoke any methods the module exposes. Now let’s look at how to create our own module and export it for use elsewhere in our program. Start off by creating a export.js file and adding the following: module.exports.variable1=32 exports.variable2=35 exports.nodesample= function () { console.log("we export function") } Now create an require.js file in the same folder and add this: const apnode= require("./export") console.log("variable1= "+apnode.variable1) console.log("variable2= "+apnode.variable2) apnode.nodesample() Run the program using" node require.js" and you should see the following output to the terminal: ------------------------- variable1= 32 variable2= 35 we export function -----------------------------
Previous Post Next Post