Can “require();” return global functions/variables?
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
add a comment |
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
add a comment |
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
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
node.js require
edited Jan 3 at 6:28
chb
1,08541936
1,08541936
asked Jan 3 at 0:53
T3rsPr0T3rsPr0
1
1
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
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.
add a comment |
You should be able to do
const {sayName, sayAge} = require('./mod');
and then refer to them without prefix.
add a comment |
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
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%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
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.
add a comment |
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.
add a comment |
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.
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.
edited Jan 3 at 3:25
answered Jan 3 at 0:57
jfriend00jfriend00
441k55580624
441k55580624
add a comment |
add a comment |
You should be able to do
const {sayName, sayAge} = require('./mod');
and then refer to them without prefix.
add a comment |
You should be able to do
const {sayName, sayAge} = require('./mod');
and then refer to them without prefix.
add a comment |
You should be able to do
const {sayName, sayAge} = require('./mod');
and then refer to them without prefix.
You should be able to do
const {sayName, sayAge} = require('./mod');
and then refer to them without prefix.
answered Jan 3 at 0:57
JoeJoe
31.9k1490107
31.9k1490107
add a comment |
add a comment |
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
add a comment |
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
add a comment |
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
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
answered Jan 3 at 4:41
Vishal-LiaVishal-Lia
565514
565514
add a comment |
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%2f54015050%2fcan-require-return-global-functions-variables%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