Can “require();” return global functions/variables?












0















Is there a way to put the exported functions of modules in the global namespace, rather than a module-specific one?



Module:



function sayName(name) {
console.log("My name is " + name);
}
function sayAge(age) {
console.log("My age is " + age);
}
module.exports = {sayName: sayName, sayAge: sayAge};


Main file:



const mod = require("./mod");
mod.sayName("Pedro"); // My name is Pedro
mod.sayAge(28); // My age is 28


This works fine, but I want to avoid having to refer to the module:



const mod = require("./mod");
sayName("Pedro"); // My name is Pedro
sayAge(28); // My age is 28


Can this be done?










share|improve this question





























    0















    Is there a way to put the exported functions of modules in the global namespace, rather than a module-specific one?



    Module:



    function sayName(name) {
    console.log("My name is " + name);
    }
    function sayAge(age) {
    console.log("My age is " + age);
    }
    module.exports = {sayName: sayName, sayAge: sayAge};


    Main file:



    const mod = require("./mod");
    mod.sayName("Pedro"); // My name is Pedro
    mod.sayAge(28); // My age is 28


    This works fine, but I want to avoid having to refer to the module:



    const mod = require("./mod");
    sayName("Pedro"); // My name is Pedro
    sayAge(28); // My age is 28


    Can this be done?










    share|improve this question



























      0












      0








      0








      Is there a way to put the exported functions of modules in the global namespace, rather than a module-specific one?



      Module:



      function sayName(name) {
      console.log("My name is " + name);
      }
      function sayAge(age) {
      console.log("My age is " + age);
      }
      module.exports = {sayName: sayName, sayAge: sayAge};


      Main file:



      const mod = require("./mod");
      mod.sayName("Pedro"); // My name is Pedro
      mod.sayAge(28); // My age is 28


      This works fine, but I want to avoid having to refer to the module:



      const mod = require("./mod");
      sayName("Pedro"); // My name is Pedro
      sayAge(28); // My age is 28


      Can this be done?










      share|improve this question
















      Is there a way to put the exported functions of modules in the global namespace, rather than a module-specific one?



      Module:



      function sayName(name) {
      console.log("My name is " + name);
      }
      function sayAge(age) {
      console.log("My age is " + age);
      }
      module.exports = {sayName: sayName, sayAge: sayAge};


      Main file:



      const mod = require("./mod");
      mod.sayName("Pedro"); // My name is Pedro
      mod.sayAge(28); // My age is 28


      This works fine, but I want to avoid having to refer to the module:



      const mod = require("./mod");
      sayName("Pedro"); // My name is Pedro
      sayAge(28); // My age is 28


      Can this be done?







      node.js require






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 3 at 6:28









      chb

      1,08541936




      1,08541936










      asked Jan 3 at 0:53









      T3rsPr0T3rsPr0

      1




      1
























          3 Answers
          3






          active

          oldest

          votes


















          2














          require() does not automatically add things to the global namespace like you're asking. It just doesn't do that and is not how it works. It returns a value which can be an individual value or an object or array full of multiple values. You HAVE to assign the return value or some property on the return value to something in order to save it for later use.



          You can use ES6 syntax to automatically create variables in the local scope for specific properties on the returned object from require() like this:



          const {sayName, sayAge} = require('./mod');

          sayName("Pedro"); // My name is Pedro
          sayAge(28); // My age is 28


          Keep in mind that these are not globals either. They are module-level variables. This is really just a language shortcut for this:



          const temp = require('./mod');
          const sayName = temp.sayName;
          const sayAge = temp.sayAge;

          sayName("Pedro"); // My name is Pedro
          sayAge(28); // My age is 28


          That language shortcut is useful for sure, but I wanted you to know what it's actually doing.






          share|improve this answer

































            1














            You should be able to do



            const {sayName, sayAge} = require('./mod');


            and then refer to them without prefix.






            share|improve this answer































              0














              Yes!!, you can add them to the Global object. (This is considered as a bad practice as anyone can make changes to our functions)



              function sayName(name) {
              console.log("My name is " + name);
              }
              function sayAge(age) {
              console.log("My age is " + age);
              }

              global.sayName = sayName;
              global.sayAge = sayAge;


              So you can just call them directly without assigning to any local variables:



              require("./mod");

              sayName("Pedro"); // My name is Pedro
              sayAge(28); // My age is 28





              share|improve this answer
























                Your Answer






                StackExchange.ifUsing("editor", function () {
                StackExchange.using("externalEditor", function () {
                StackExchange.using("snippets", function () {
                StackExchange.snippets.init();
                });
                });
                }, "code-snippets");

                StackExchange.ready(function() {
                var channelOptions = {
                tags: "".split(" "),
                id: "1"
                };
                initTagRenderer("".split(" "), "".split(" "), channelOptions);

                StackExchange.using("externalEditor", function() {
                // Have to fire editor after snippets, if snippets enabled
                if (StackExchange.settings.snippets.snippetsEnabled) {
                StackExchange.using("snippets", function() {
                createEditor();
                });
                }
                else {
                createEditor();
                }
                });

                function createEditor() {
                StackExchange.prepareEditor({
                heartbeatType: 'answer',
                autoActivateHeartbeat: false,
                convertImagesToLinks: true,
                noModals: true,
                showLowRepImageUploadWarning: true,
                reputationToPostImages: 10,
                bindNavPrevention: true,
                postfix: "",
                imageUploader: {
                brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                allowUrls: true
                },
                onDemand: true,
                discardSelector: ".discard-answer"
                ,immediatelyShowMarkdownHelp:true
                });


                }
                });














                draft saved

                draft discarded


















                StackExchange.ready(
                function () {
                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54015050%2fcan-require-return-global-functions-variables%23new-answer', 'question_page');
                }
                );

                Post as a guest















                Required, but never shown

























                3 Answers
                3






                active

                oldest

                votes








                3 Answers
                3






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes









                2














                require() does not automatically add things to the global namespace like you're asking. It just doesn't do that and is not how it works. It returns a value which can be an individual value or an object or array full of multiple values. You HAVE to assign the return value or some property on the return value to something in order to save it for later use.



                You can use ES6 syntax to automatically create variables in the local scope for specific properties on the returned object from require() like this:



                const {sayName, sayAge} = require('./mod');

                sayName("Pedro"); // My name is Pedro
                sayAge(28); // My age is 28


                Keep in mind that these are not globals either. They are module-level variables. This is really just a language shortcut for this:



                const temp = require('./mod');
                const sayName = temp.sayName;
                const sayAge = temp.sayAge;

                sayName("Pedro"); // My name is Pedro
                sayAge(28); // My age is 28


                That language shortcut is useful for sure, but I wanted you to know what it's actually doing.






                share|improve this answer






























                  2














                  require() does not automatically add things to the global namespace like you're asking. It just doesn't do that and is not how it works. It returns a value which can be an individual value or an object or array full of multiple values. You HAVE to assign the return value or some property on the return value to something in order to save it for later use.



                  You can use ES6 syntax to automatically create variables in the local scope for specific properties on the returned object from require() like this:



                  const {sayName, sayAge} = require('./mod');

                  sayName("Pedro"); // My name is Pedro
                  sayAge(28); // My age is 28


                  Keep in mind that these are not globals either. They are module-level variables. This is really just a language shortcut for this:



                  const temp = require('./mod');
                  const sayName = temp.sayName;
                  const sayAge = temp.sayAge;

                  sayName("Pedro"); // My name is Pedro
                  sayAge(28); // My age is 28


                  That language shortcut is useful for sure, but I wanted you to know what it's actually doing.






                  share|improve this answer




























                    2












                    2








                    2







                    require() does not automatically add things to the global namespace like you're asking. It just doesn't do that and is not how it works. It returns a value which can be an individual value or an object or array full of multiple values. You HAVE to assign the return value or some property on the return value to something in order to save it for later use.



                    You can use ES6 syntax to automatically create variables in the local scope for specific properties on the returned object from require() like this:



                    const {sayName, sayAge} = require('./mod');

                    sayName("Pedro"); // My name is Pedro
                    sayAge(28); // My age is 28


                    Keep in mind that these are not globals either. They are module-level variables. This is really just a language shortcut for this:



                    const temp = require('./mod');
                    const sayName = temp.sayName;
                    const sayAge = temp.sayAge;

                    sayName("Pedro"); // My name is Pedro
                    sayAge(28); // My age is 28


                    That language shortcut is useful for sure, but I wanted you to know what it's actually doing.






                    share|improve this answer















                    require() does not automatically add things to the global namespace like you're asking. It just doesn't do that and is not how it works. It returns a value which can be an individual value or an object or array full of multiple values. You HAVE to assign the return value or some property on the return value to something in order to save it for later use.



                    You can use ES6 syntax to automatically create variables in the local scope for specific properties on the returned object from require() like this:



                    const {sayName, sayAge} = require('./mod');

                    sayName("Pedro"); // My name is Pedro
                    sayAge(28); // My age is 28


                    Keep in mind that these are not globals either. They are module-level variables. This is really just a language shortcut for this:



                    const temp = require('./mod');
                    const sayName = temp.sayName;
                    const sayAge = temp.sayAge;

                    sayName("Pedro"); // My name is Pedro
                    sayAge(28); // My age is 28


                    That language shortcut is useful for sure, but I wanted you to know what it's actually doing.







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Jan 3 at 3:25

























                    answered Jan 3 at 0:57









                    jfriend00jfriend00

                    441k55580624




                    441k55580624

























                        1














                        You should be able to do



                        const {sayName, sayAge} = require('./mod');


                        and then refer to them without prefix.






                        share|improve this answer




























                          1














                          You should be able to do



                          const {sayName, sayAge} = require('./mod');


                          and then refer to them without prefix.






                          share|improve this answer


























                            1












                            1








                            1







                            You should be able to do



                            const {sayName, sayAge} = require('./mod');


                            and then refer to them without prefix.






                            share|improve this answer













                            You should be able to do



                            const {sayName, sayAge} = require('./mod');


                            and then refer to them without prefix.







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Jan 3 at 0:57









                            JoeJoe

                            31.9k1490107




                            31.9k1490107























                                0














                                Yes!!, you can add them to the Global object. (This is considered as a bad practice as anyone can make changes to our functions)



                                function sayName(name) {
                                console.log("My name is " + name);
                                }
                                function sayAge(age) {
                                console.log("My age is " + age);
                                }

                                global.sayName = sayName;
                                global.sayAge = sayAge;


                                So you can just call them directly without assigning to any local variables:



                                require("./mod");

                                sayName("Pedro"); // My name is Pedro
                                sayAge(28); // My age is 28





                                share|improve this answer




























                                  0














                                  Yes!!, you can add them to the Global object. (This is considered as a bad practice as anyone can make changes to our functions)



                                  function sayName(name) {
                                  console.log("My name is " + name);
                                  }
                                  function sayAge(age) {
                                  console.log("My age is " + age);
                                  }

                                  global.sayName = sayName;
                                  global.sayAge = sayAge;


                                  So you can just call them directly without assigning to any local variables:



                                  require("./mod");

                                  sayName("Pedro"); // My name is Pedro
                                  sayAge(28); // My age is 28





                                  share|improve this answer


























                                    0












                                    0








                                    0







                                    Yes!!, you can add them to the Global object. (This is considered as a bad practice as anyone can make changes to our functions)



                                    function sayName(name) {
                                    console.log("My name is " + name);
                                    }
                                    function sayAge(age) {
                                    console.log("My age is " + age);
                                    }

                                    global.sayName = sayName;
                                    global.sayAge = sayAge;


                                    So you can just call them directly without assigning to any local variables:



                                    require("./mod");

                                    sayName("Pedro"); // My name is Pedro
                                    sayAge(28); // My age is 28





                                    share|improve this answer













                                    Yes!!, you can add them to the Global object. (This is considered as a bad practice as anyone can make changes to our functions)



                                    function sayName(name) {
                                    console.log("My name is " + name);
                                    }
                                    function sayAge(age) {
                                    console.log("My age is " + age);
                                    }

                                    global.sayName = sayName;
                                    global.sayAge = sayAge;


                                    So you can just call them directly without assigning to any local variables:



                                    require("./mod");

                                    sayName("Pedro"); // My name is Pedro
                                    sayAge(28); // My age is 28






                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered Jan 3 at 4:41









                                    Vishal-LiaVishal-Lia

                                    565514




                                    565514






























                                        draft saved

                                        draft discarded




















































                                        Thanks for contributing an answer to Stack Overflow!


                                        • Please be sure to answer the question. Provide details and share your research!

                                        But avoid



                                        • Asking for help, clarification, or responding to other answers.

                                        • Making statements based on opinion; back them up with references or personal experience.


                                        To learn more, see our tips on writing great answers.




                                        draft saved


                                        draft discarded














                                        StackExchange.ready(
                                        function () {
                                        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54015050%2fcan-require-return-global-functions-variables%23new-answer', 'question_page');
                                        }
                                        );

                                        Post as a guest















                                        Required, but never shown





















































                                        Required, but never shown














                                        Required, but never shown












                                        Required, but never shown







                                        Required, but never shown

































                                        Required, but never shown














                                        Required, but never shown












                                        Required, but never shown







                                        Required, but never shown







                                        Popular posts from this blog

                                        android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

                                        SQL update select statement

                                        WPF add header to Image with URL pettitions [duplicate]