Importing all exports in a module NodeJS
up vote
0
down vote
favorite
I want to be able to access all exports of a module without having to say module. before the export.
Let's say that I have a module:
// mymod.js
module.exports.foo = function()
console.log("foo!");
module.exports.bar = "bar!";
And a main file:
// main.js
var mymod = require("./mymod.js");
mymod.foo();
Is there a way to call foo() without needing to say mymod. before? This can be achieved in python by saying import module as *.
What is the NodeJS equivalent to this?
javascript node.js
add a comment |
up vote
0
down vote
favorite
I want to be able to access all exports of a module without having to say module. before the export.
Let's say that I have a module:
// mymod.js
module.exports.foo = function()
console.log("foo!");
module.exports.bar = "bar!";
And a main file:
// main.js
var mymod = require("./mymod.js");
mymod.foo();
Is there a way to call foo() without needing to say mymod. before? This can be achieved in python by saying import module as *.
What is the NodeJS equivalent to this?
javascript node.js
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I want to be able to access all exports of a module without having to say module. before the export.
Let's say that I have a module:
// mymod.js
module.exports.foo = function()
console.log("foo!");
module.exports.bar = "bar!";
And a main file:
// main.js
var mymod = require("./mymod.js");
mymod.foo();
Is there a way to call foo() without needing to say mymod. before? This can be achieved in python by saying import module as *.
What is the NodeJS equivalent to this?
javascript node.js
I want to be able to access all exports of a module without having to say module. before the export.
Let's say that I have a module:
// mymod.js
module.exports.foo = function()
console.log("foo!");
module.exports.bar = "bar!";
And a main file:
// main.js
var mymod = require("./mymod.js");
mymod.foo();
Is there a way to call foo() without needing to say mymod. before? This can be achieved in python by saying import module as *.
What is the NodeJS equivalent to this?
javascript node.js
javascript node.js
asked Nov 10 at 1:16
Matt X
1118
1118
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
up vote
0
down vote
You can use ES6 destructuring:
var foo = require("./mymod.js");
foo();
lets say that I have 100 exports in a file. Do I need to put commas after every import inside the ? There must be a better way to do this.
– Matt X
Nov 10 at 1:24
If you have 100 exports in a file and want to call all of them maybe the issue is of another nature. You could do something likeObject.values(mymod).forEach(f => f()).
– E. Sundin
Nov 10 at 1:29
@MattX If you provide a more specific use case you'll get better answers.
– E. Sundin
Nov 10 at 1:31
add a comment |
up vote
0
down vote
I want to be able to access all exports of a module without having to
say module. before the export.
Use the shorthand:
exports.myVar = myVar
exports.foo = () =>
Or use an Object:
module.exports =
foo,
myVar
// main.js
var mymod = require("./mymod.js");
mymod.foo();
Is there a way to call foo() without needing to say mymod. before?
This can be achieved in python by saying import module as *. What is
the NodeJS equivalent to this?
Use destructuring:
const foo = require("./mymod.js")
lets say that I have 100 exports in a file. Do I need to put commas
after every import inside the ? There must be a better way to do
this
If you have 100 exports why would you want to import them all globally as their own functions? myMod.func is better for clarity.
A hacky workaround might be to do const myMod = require('myMod') then map it putting the functions on the global object. Or put them on the global from the start instead of exporting it.
add a comment |
up vote
0
down vote
In ES6 you can import modules in the following ways
import moduleName from "path/to/module"; // import default export from the file as moduleName object, moduleName can be anything
import exportMemberName1, exportMemberName2, ... from "path/to/module"; // destructured import, it will destructure import and can access the export module without prefixing anything
import * as moduleName from "path/to/module"; // import everything exported from the file as moduleName object, you can access every export members from that object, moduleName can be anything
These are the only methods provided by ES6 to import module (you can also use require).
If you have to import 100s of modules best ways is first method, import everything as an object and destructure on the go, I meant if you have lots of functions or methods, destructure what you want in that function in side that function, eg.
import * as moduleName from "path/to/file";
function function1()
const exportMember1, exportMember2 = module;
function function2()
const exportMember1, exportMember5, exportMember7 = module;
This part is wrong I think:import moduleName from "path/to/module"; // import everything exported from the file as moduleName object, you can access every export members from that object, moduleName can be anything... Theimport module from '...'imports the default export not all exports.import * as allimports all exports.
– CodeDraken
Nov 10 at 23:53
Actually not wrong, you can but you don't have toimport * as module from "...", default import will also import everything from that file, for eg, you can check packages like react, it doesn't have default export, but we can doimport React from "react"same goes for custom modules.
– Hafeez Hamza
Nov 11 at 4:08
It's using default... github.com/facebook/react/blob/master/packages/react/index.js
– CodeDraken
Nov 11 at 4:17
developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
– CodeDraken
Nov 11 at 4:31
Yes, I'm wrong thankyou very much for correcting and arguing with me, 👍, in my thought somewhere I had an experience of using default import to import all, i tried that again and it's not working, I have corrected the answer
– Hafeez Hamza
Nov 11 at 4:40
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
You can use ES6 destructuring:
var foo = require("./mymod.js");
foo();
lets say that I have 100 exports in a file. Do I need to put commas after every import inside the ? There must be a better way to do this.
– Matt X
Nov 10 at 1:24
If you have 100 exports in a file and want to call all of them maybe the issue is of another nature. You could do something likeObject.values(mymod).forEach(f => f()).
– E. Sundin
Nov 10 at 1:29
@MattX If you provide a more specific use case you'll get better answers.
– E. Sundin
Nov 10 at 1:31
add a comment |
up vote
0
down vote
You can use ES6 destructuring:
var foo = require("./mymod.js");
foo();
lets say that I have 100 exports in a file. Do I need to put commas after every import inside the ? There must be a better way to do this.
– Matt X
Nov 10 at 1:24
If you have 100 exports in a file and want to call all of them maybe the issue is of another nature. You could do something likeObject.values(mymod).forEach(f => f()).
– E. Sundin
Nov 10 at 1:29
@MattX If you provide a more specific use case you'll get better answers.
– E. Sundin
Nov 10 at 1:31
add a comment |
up vote
0
down vote
up vote
0
down vote
You can use ES6 destructuring:
var foo = require("./mymod.js");
foo();
You can use ES6 destructuring:
var foo = require("./mymod.js");
foo();
edited Nov 10 at 1:30
answered Nov 10 at 1:23
E. Sundin
2,757924
2,757924
lets say that I have 100 exports in a file. Do I need to put commas after every import inside the ? There must be a better way to do this.
– Matt X
Nov 10 at 1:24
If you have 100 exports in a file and want to call all of them maybe the issue is of another nature. You could do something likeObject.values(mymod).forEach(f => f()).
– E. Sundin
Nov 10 at 1:29
@MattX If you provide a more specific use case you'll get better answers.
– E. Sundin
Nov 10 at 1:31
add a comment |
lets say that I have 100 exports in a file. Do I need to put commas after every import inside the ? There must be a better way to do this.
– Matt X
Nov 10 at 1:24
If you have 100 exports in a file and want to call all of them maybe the issue is of another nature. You could do something likeObject.values(mymod).forEach(f => f()).
– E. Sundin
Nov 10 at 1:29
@MattX If you provide a more specific use case you'll get better answers.
– E. Sundin
Nov 10 at 1:31
lets say that I have 100 exports in a file. Do I need to put commas after every import inside the ? There must be a better way to do this.
– Matt X
Nov 10 at 1:24
lets say that I have 100 exports in a file. Do I need to put commas after every import inside the ? There must be a better way to do this.
– Matt X
Nov 10 at 1:24
If you have 100 exports in a file and want to call all of them maybe the issue is of another nature. You could do something like
Object.values(mymod).forEach(f => f()).– E. Sundin
Nov 10 at 1:29
If you have 100 exports in a file and want to call all of them maybe the issue is of another nature. You could do something like
Object.values(mymod).forEach(f => f()).– E. Sundin
Nov 10 at 1:29
@MattX If you provide a more specific use case you'll get better answers.
– E. Sundin
Nov 10 at 1:31
@MattX If you provide a more specific use case you'll get better answers.
– E. Sundin
Nov 10 at 1:31
add a comment |
up vote
0
down vote
I want to be able to access all exports of a module without having to
say module. before the export.
Use the shorthand:
exports.myVar = myVar
exports.foo = () =>
Or use an Object:
module.exports =
foo,
myVar
// main.js
var mymod = require("./mymod.js");
mymod.foo();
Is there a way to call foo() without needing to say mymod. before?
This can be achieved in python by saying import module as *. What is
the NodeJS equivalent to this?
Use destructuring:
const foo = require("./mymod.js")
lets say that I have 100 exports in a file. Do I need to put commas
after every import inside the ? There must be a better way to do
this
If you have 100 exports why would you want to import them all globally as their own functions? myMod.func is better for clarity.
A hacky workaround might be to do const myMod = require('myMod') then map it putting the functions on the global object. Or put them on the global from the start instead of exporting it.
add a comment |
up vote
0
down vote
I want to be able to access all exports of a module without having to
say module. before the export.
Use the shorthand:
exports.myVar = myVar
exports.foo = () =>
Or use an Object:
module.exports =
foo,
myVar
// main.js
var mymod = require("./mymod.js");
mymod.foo();
Is there a way to call foo() without needing to say mymod. before?
This can be achieved in python by saying import module as *. What is
the NodeJS equivalent to this?
Use destructuring:
const foo = require("./mymod.js")
lets say that I have 100 exports in a file. Do I need to put commas
after every import inside the ? There must be a better way to do
this
If you have 100 exports why would you want to import them all globally as their own functions? myMod.func is better for clarity.
A hacky workaround might be to do const myMod = require('myMod') then map it putting the functions on the global object. Or put them on the global from the start instead of exporting it.
add a comment |
up vote
0
down vote
up vote
0
down vote
I want to be able to access all exports of a module without having to
say module. before the export.
Use the shorthand:
exports.myVar = myVar
exports.foo = () =>
Or use an Object:
module.exports =
foo,
myVar
// main.js
var mymod = require("./mymod.js");
mymod.foo();
Is there a way to call foo() without needing to say mymod. before?
This can be achieved in python by saying import module as *. What is
the NodeJS equivalent to this?
Use destructuring:
const foo = require("./mymod.js")
lets say that I have 100 exports in a file. Do I need to put commas
after every import inside the ? There must be a better way to do
this
If you have 100 exports why would you want to import them all globally as their own functions? myMod.func is better for clarity.
A hacky workaround might be to do const myMod = require('myMod') then map it putting the functions on the global object. Or put them on the global from the start instead of exporting it.
I want to be able to access all exports of a module without having to
say module. before the export.
Use the shorthand:
exports.myVar = myVar
exports.foo = () =>
Or use an Object:
module.exports =
foo,
myVar
// main.js
var mymod = require("./mymod.js");
mymod.foo();
Is there a way to call foo() without needing to say mymod. before?
This can be achieved in python by saying import module as *. What is
the NodeJS equivalent to this?
Use destructuring:
const foo = require("./mymod.js")
lets say that I have 100 exports in a file. Do I need to put commas
after every import inside the ? There must be a better way to do
this
If you have 100 exports why would you want to import them all globally as their own functions? myMod.func is better for clarity.
A hacky workaround might be to do const myMod = require('myMod') then map it putting the functions on the global object. Or put them on the global from the start instead of exporting it.
edited Nov 10 at 3:26
answered Nov 10 at 3:20
CodeDraken
516110
516110
add a comment |
add a comment |
up vote
0
down vote
In ES6 you can import modules in the following ways
import moduleName from "path/to/module"; // import default export from the file as moduleName object, moduleName can be anything
import exportMemberName1, exportMemberName2, ... from "path/to/module"; // destructured import, it will destructure import and can access the export module without prefixing anything
import * as moduleName from "path/to/module"; // import everything exported from the file as moduleName object, you can access every export members from that object, moduleName can be anything
These are the only methods provided by ES6 to import module (you can also use require).
If you have to import 100s of modules best ways is first method, import everything as an object and destructure on the go, I meant if you have lots of functions or methods, destructure what you want in that function in side that function, eg.
import * as moduleName from "path/to/file";
function function1()
const exportMember1, exportMember2 = module;
function function2()
const exportMember1, exportMember5, exportMember7 = module;
This part is wrong I think:import moduleName from "path/to/module"; // import everything exported from the file as moduleName object, you can access every export members from that object, moduleName can be anything... Theimport module from '...'imports the default export not all exports.import * as allimports all exports.
– CodeDraken
Nov 10 at 23:53
Actually not wrong, you can but you don't have toimport * as module from "...", default import will also import everything from that file, for eg, you can check packages like react, it doesn't have default export, but we can doimport React from "react"same goes for custom modules.
– Hafeez Hamza
Nov 11 at 4:08
It's using default... github.com/facebook/react/blob/master/packages/react/index.js
– CodeDraken
Nov 11 at 4:17
developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
– CodeDraken
Nov 11 at 4:31
Yes, I'm wrong thankyou very much for correcting and arguing with me, 👍, in my thought somewhere I had an experience of using default import to import all, i tried that again and it's not working, I have corrected the answer
– Hafeez Hamza
Nov 11 at 4:40
add a comment |
up vote
0
down vote
In ES6 you can import modules in the following ways
import moduleName from "path/to/module"; // import default export from the file as moduleName object, moduleName can be anything
import exportMemberName1, exportMemberName2, ... from "path/to/module"; // destructured import, it will destructure import and can access the export module without prefixing anything
import * as moduleName from "path/to/module"; // import everything exported from the file as moduleName object, you can access every export members from that object, moduleName can be anything
These are the only methods provided by ES6 to import module (you can also use require).
If you have to import 100s of modules best ways is first method, import everything as an object and destructure on the go, I meant if you have lots of functions or methods, destructure what you want in that function in side that function, eg.
import * as moduleName from "path/to/file";
function function1()
const exportMember1, exportMember2 = module;
function function2()
const exportMember1, exportMember5, exportMember7 = module;
This part is wrong I think:import moduleName from "path/to/module"; // import everything exported from the file as moduleName object, you can access every export members from that object, moduleName can be anything... Theimport module from '...'imports the default export not all exports.import * as allimports all exports.
– CodeDraken
Nov 10 at 23:53
Actually not wrong, you can but you don't have toimport * as module from "...", default import will also import everything from that file, for eg, you can check packages like react, it doesn't have default export, but we can doimport React from "react"same goes for custom modules.
– Hafeez Hamza
Nov 11 at 4:08
It's using default... github.com/facebook/react/blob/master/packages/react/index.js
– CodeDraken
Nov 11 at 4:17
developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
– CodeDraken
Nov 11 at 4:31
Yes, I'm wrong thankyou very much for correcting and arguing with me, 👍, in my thought somewhere I had an experience of using default import to import all, i tried that again and it's not working, I have corrected the answer
– Hafeez Hamza
Nov 11 at 4:40
add a comment |
up vote
0
down vote
up vote
0
down vote
In ES6 you can import modules in the following ways
import moduleName from "path/to/module"; // import default export from the file as moduleName object, moduleName can be anything
import exportMemberName1, exportMemberName2, ... from "path/to/module"; // destructured import, it will destructure import and can access the export module without prefixing anything
import * as moduleName from "path/to/module"; // import everything exported from the file as moduleName object, you can access every export members from that object, moduleName can be anything
These are the only methods provided by ES6 to import module (you can also use require).
If you have to import 100s of modules best ways is first method, import everything as an object and destructure on the go, I meant if you have lots of functions or methods, destructure what you want in that function in side that function, eg.
import * as moduleName from "path/to/file";
function function1()
const exportMember1, exportMember2 = module;
function function2()
const exportMember1, exportMember5, exportMember7 = module;
In ES6 you can import modules in the following ways
import moduleName from "path/to/module"; // import default export from the file as moduleName object, moduleName can be anything
import exportMemberName1, exportMemberName2, ... from "path/to/module"; // destructured import, it will destructure import and can access the export module without prefixing anything
import * as moduleName from "path/to/module"; // import everything exported from the file as moduleName object, you can access every export members from that object, moduleName can be anything
These are the only methods provided by ES6 to import module (you can also use require).
If you have to import 100s of modules best ways is first method, import everything as an object and destructure on the go, I meant if you have lots of functions or methods, destructure what you want in that function in side that function, eg.
import * as moduleName from "path/to/file";
function function1()
const exportMember1, exportMember2 = module;
function function2()
const exportMember1, exportMember5, exportMember7 = module;
edited Nov 11 at 4:56
answered Nov 10 at 3:03
Hafeez Hamza
307110
307110
This part is wrong I think:import moduleName from "path/to/module"; // import everything exported from the file as moduleName object, you can access every export members from that object, moduleName can be anything... Theimport module from '...'imports the default export not all exports.import * as allimports all exports.
– CodeDraken
Nov 10 at 23:53
Actually not wrong, you can but you don't have toimport * as module from "...", default import will also import everything from that file, for eg, you can check packages like react, it doesn't have default export, but we can doimport React from "react"same goes for custom modules.
– Hafeez Hamza
Nov 11 at 4:08
It's using default... github.com/facebook/react/blob/master/packages/react/index.js
– CodeDraken
Nov 11 at 4:17
developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
– CodeDraken
Nov 11 at 4:31
Yes, I'm wrong thankyou very much for correcting and arguing with me, 👍, in my thought somewhere I had an experience of using default import to import all, i tried that again and it's not working, I have corrected the answer
– Hafeez Hamza
Nov 11 at 4:40
add a comment |
This part is wrong I think:import moduleName from "path/to/module"; // import everything exported from the file as moduleName object, you can access every export members from that object, moduleName can be anything... Theimport module from '...'imports the default export not all exports.import * as allimports all exports.
– CodeDraken
Nov 10 at 23:53
Actually not wrong, you can but you don't have toimport * as module from "...", default import will also import everything from that file, for eg, you can check packages like react, it doesn't have default export, but we can doimport React from "react"same goes for custom modules.
– Hafeez Hamza
Nov 11 at 4:08
It's using default... github.com/facebook/react/blob/master/packages/react/index.js
– CodeDraken
Nov 11 at 4:17
developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
– CodeDraken
Nov 11 at 4:31
Yes, I'm wrong thankyou very much for correcting and arguing with me, 👍, in my thought somewhere I had an experience of using default import to import all, i tried that again and it's not working, I have corrected the answer
– Hafeez Hamza
Nov 11 at 4:40
This part is wrong I think:
import moduleName from "path/to/module"; // import everything exported from the file as moduleName object, you can access every export members from that object, moduleName can be anything ... The import module from '...' imports the default export not all exports. import * as all imports all exports.– CodeDraken
Nov 10 at 23:53
This part is wrong I think:
import moduleName from "path/to/module"; // import everything exported from the file as moduleName object, you can access every export members from that object, moduleName can be anything ... The import module from '...' imports the default export not all exports. import * as all imports all exports.– CodeDraken
Nov 10 at 23:53
Actually not wrong, you can but you don't have to
import * as module from "...", default import will also import everything from that file, for eg, you can check packages like react, it doesn't have default export, but we can do import React from "react" same goes for custom modules.– Hafeez Hamza
Nov 11 at 4:08
Actually not wrong, you can but you don't have to
import * as module from "...", default import will also import everything from that file, for eg, you can check packages like react, it doesn't have default export, but we can do import React from "react" same goes for custom modules.– Hafeez Hamza
Nov 11 at 4:08
It's using default... github.com/facebook/react/blob/master/packages/react/index.js
– CodeDraken
Nov 11 at 4:17
It's using default... github.com/facebook/react/blob/master/packages/react/index.js
– CodeDraken
Nov 11 at 4:17
developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
– CodeDraken
Nov 11 at 4:31
developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
– CodeDraken
Nov 11 at 4:31
Yes, I'm wrong thankyou very much for correcting and arguing with me, 👍, in my thought somewhere I had an experience of using default import to import all, i tried that again and it's not working, I have corrected the answer
– Hafeez Hamza
Nov 11 at 4:40
Yes, I'm wrong thankyou very much for correcting and arguing with me, 👍, in my thought somewhere I had an experience of using default import to import all, i tried that again and it's not working, I have corrected the answer
– Hafeez Hamza
Nov 11 at 4:40
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f53235199%2fimporting-all-exports-in-a-module-nodejs%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