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?
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
add a comment |
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
add a comment |
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
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
node.js
asked Mar 21 at 16:41
YağızhanYağızhan
161114
161114
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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.
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
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
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
add a comment |
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.
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
add a comment |
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.
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.
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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