Defining modules in one file and requiring it in Node.js The Next CEO of Stack OverflowHow do I debug Node.js applications?How do I get started with Node.jsWriting files in Node.jsHow do I pass command line arguments to a Node.js program?Check synchronously if file/directory exists in Node.jsRead environment variables in Node.jsHow to decide when to use Node.js?How to exit in Node.jsWhat is the purpose of Node.js module.exports and how do you use it?What's the difference between dependencies, devDependencies and peerDependencies in npm package.json file?

How to Reset Passwords on Multiple Websites Easily?

How to make a software documentation "officially" citable?

How do I solve this limit?

Trouble understanding the speech of overseas colleagues

Why is there a PLL in CPU?

Inappropriate reference requests from Journal reviewers

% symbol leads to superlong (forever?) compilations

Which organization defines CJK Unified Ideographs?

How should I support this large drywall patch?

How to get regions to plot as graphics

Can I equip Skullclamp on a creature I am sacrificing?

What can we do to stop prior company from asking us questions?

Why were Madagascar and New Zealand discovered so late?

Need some help with wall behind rangetop

Natural language into sentence logic

How to safely derail a train during transit?

Can a caster that cast Polymorph on themselves stop concentrating at any point even if their Int is low?

How easy is it to start Magic from scratch?

When airplanes disconnect from a tanker during air to air refueling, why do they bank so sharply to the right?

WOW air has ceased operation, can I get my tickets refunded?

How to count occurrences of text in a file?

How do spells that require an ability check vs. the caster's spell save DC work?

How to write the block matrix in LaTex?

Why do professional authors make "consistency" mistakes? And how to avoid them?



Defining modules in one file and requiring it in Node.js



The Next CEO of Stack OverflowHow do I debug Node.js applications?How do I get started with Node.jsWriting files in Node.jsHow do I pass command line arguments to a Node.js program?Check synchronously if file/directory exists in Node.jsRead environment variables in Node.jsHow to decide when to use Node.js?How to exit in Node.jsWhat is the purpose of Node.js module.exports and how do you use it?What's the difference between dependencies, devDependencies and peerDependencies in npm package.json file?










1















I'm updating my code to include new packages so often and I have more than 100 files.



I want to make something like this,



File : dependencies.js :



const snekfetch = require("snekfetch");
const fs = require("fs");


It's very annoying to modify every file to add just a single package.



I'm trying to require the dependencies.js using this:



require("./dependencies.js")


But I see this in my console:



ReferenceError: snekfetch is not defined


Is there any way I can succeed?










share|improve this question


























    1















    I'm updating my code to include new packages so often and I have more than 100 files.



    I want to make something like this,



    File : dependencies.js :



    const snekfetch = require("snekfetch");
    const fs = require("fs");


    It's very annoying to modify every file to add just a single package.



    I'm trying to require the dependencies.js using this:



    require("./dependencies.js")


    But I see this in my console:



    ReferenceError: snekfetch is not defined


    Is there any way I can succeed?










    share|improve this question
























      1












      1








      1








      I'm updating my code to include new packages so often and I have more than 100 files.



      I want to make something like this,



      File : dependencies.js :



      const snekfetch = require("snekfetch");
      const fs = require("fs");


      It's very annoying to modify every file to add just a single package.



      I'm trying to require the dependencies.js using this:



      require("./dependencies.js")


      But I see this in my console:



      ReferenceError: snekfetch is not defined


      Is there any way I can succeed?










      share|improve this question














      I'm updating my code to include new packages so often and I have more than 100 files.



      I want to make something like this,



      File : dependencies.js :



      const snekfetch = require("snekfetch");
      const fs = require("fs");


      It's very annoying to modify every file to add just a single package.



      I'm trying to require the dependencies.js using this:



      require("./dependencies.js")


      But I see this in my console:



      ReferenceError: snekfetch is not defined


      Is there any way I can succeed?







      node.js






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 21 at 16:41









      YağızhanYağızhan

      161114




      161114






















          1 Answer
          1






          active

          oldest

          votes


















          2














          I think you are not exporting the modules in dependencies.js
          dependencies.js should look like,



           const snekfetch = require("snekfetch");
          const fs = require("fs");
          module.exports =
          "snekfetch": snekfetch,
          "fs": fs
          ;


          Then you should be able to import this file and use it like as follows,



           var dependencies = require('./dependencies.js');
          // dependencies.fs.readFile();


          Although there are much better ways to handle your imports then just creating a simple file of dependencies. Have a look at this link.






          share|improve this answer























          • Thanks! I'll try it and will inform you.

            – Yağızhan
            Mar 21 at 16:53











          • This didn't actually solve my thing but I got to the end myself. Thanks though.

            – Yağızhan
            Mar 23 at 14:56











          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%2f55285322%2fdefining-modules-in-one-file-and-requiring-it-in-node-js%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          2














          I think you are not exporting the modules in dependencies.js
          dependencies.js should look like,



           const snekfetch = require("snekfetch");
          const fs = require("fs");
          module.exports =
          "snekfetch": snekfetch,
          "fs": fs
          ;


          Then you should be able to import this file and use it like as follows,



           var dependencies = require('./dependencies.js');
          // dependencies.fs.readFile();


          Although there are much better ways to handle your imports then just creating a simple file of dependencies. Have a look at this link.






          share|improve this answer























          • Thanks! I'll try it and will inform you.

            – Yağızhan
            Mar 21 at 16:53











          • This didn't actually solve my thing but I got to the end myself. Thanks though.

            – Yağızhan
            Mar 23 at 14:56















          2














          I think you are not exporting the modules in dependencies.js
          dependencies.js should look like,



           const snekfetch = require("snekfetch");
          const fs = require("fs");
          module.exports =
          "snekfetch": snekfetch,
          "fs": fs
          ;


          Then you should be able to import this file and use it like as follows,



           var dependencies = require('./dependencies.js');
          // dependencies.fs.readFile();


          Although there are much better ways to handle your imports then just creating a simple file of dependencies. Have a look at this link.






          share|improve this answer























          • Thanks! I'll try it and will inform you.

            – Yağızhan
            Mar 21 at 16:53











          • This didn't actually solve my thing but I got to the end myself. Thanks though.

            – Yağızhan
            Mar 23 at 14:56













          2












          2








          2







          I think you are not exporting the modules in dependencies.js
          dependencies.js should look like,



           const snekfetch = require("snekfetch");
          const fs = require("fs");
          module.exports =
          "snekfetch": snekfetch,
          "fs": fs
          ;


          Then you should be able to import this file and use it like as follows,



           var dependencies = require('./dependencies.js');
          // dependencies.fs.readFile();


          Although there are much better ways to handle your imports then just creating a simple file of dependencies. Have a look at this link.






          share|improve this answer













          I think you are not exporting the modules in dependencies.js
          dependencies.js should look like,



           const snekfetch = require("snekfetch");
          const fs = require("fs");
          module.exports =
          "snekfetch": snekfetch,
          "fs": fs
          ;


          Then you should be able to import this file and use it like as follows,



           var dependencies = require('./dependencies.js');
          // dependencies.fs.readFile();


          Although there are much better ways to handle your imports then just creating a simple file of dependencies. Have a look at this link.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 21 at 16:48









          sagar agarwalsagar agarwal

          895




          895












          • Thanks! I'll try it and will inform you.

            – Yağızhan
            Mar 21 at 16:53











          • This didn't actually solve my thing but I got to the end myself. Thanks though.

            – Yağızhan
            Mar 23 at 14:56

















          • Thanks! I'll try it and will inform you.

            – Yağızhan
            Mar 21 at 16:53











          • This didn't actually solve my thing but I got to the end myself. Thanks though.

            – Yağızhan
            Mar 23 at 14:56
















          Thanks! I'll try it and will inform you.

          – Yağızhan
          Mar 21 at 16:53





          Thanks! I'll try it and will inform you.

          – Yağızhan
          Mar 21 at 16:53













          This didn't actually solve my thing but I got to the end myself. Thanks though.

          – Yağızhan
          Mar 23 at 14:56





          This didn't actually solve my thing but I got to the end myself. Thanks though.

          – Yağızhan
          Mar 23 at 14:56



















          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%2f55285322%2fdefining-modules-in-one-file-and-requiring-it-in-node-js%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

          Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

          Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

          Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript