I am using NodeJS. Is it possible to require a javascript file in node as we do in browsers? Because when I use the require() method it calls a javascript file that as no access to the global variables in my server.js it is like a "use strict" I guess. I needed to load my module and make it a continuation of the main file since my module depends on some global variables.
EDIT:
I have this server.js file:
var Settings = require("settings.js");
var Clients = require("clients.js");
var removePack = [];
/**HERE RUNS MY WEBSOCKET SERVER**/
...
...
//When a new socket connects:
io.on("connection", function(socket){
var socket.id = Math.random();
var client = Clients.create(socket.id);
});
Here my clients.js file:
exports.create = function(){
self.team = Settings.generateRandomTeam(); /*The problem is here. I can´t access the Settings variable*/
self.maxSpeed = Settings.maxSpeed;
...
...
return self;
}
EDIT 2:
When I use Settings = require("settings.js") I get the following error:
TypeError: Settings.generateRandomTeam() is not a function.
My settings.js file is like this:
Settings.generateRandomTeam = function(){
//Some code
}
module.exports = Settings;
Thanks!
I am using NodeJS. Is it possible to require a javascript file in node as we do in browsers? Because when I use the require() method it calls a javascript file that as no access to the global variables in my server.js it is like a "use strict" I guess. I needed to load my module and make it a continuation of the main file since my module depends on some global variables.
EDIT:
I have this server.js file:
var Settings = require("settings.js");
var Clients = require("clients.js");
var removePack = [];
/**HERE RUNS MY WEBSOCKET SERVER**/
...
...
//When a new socket connects:
io.on("connection", function(socket){
var socket.id = Math.random();
var client = Clients.create(socket.id);
});
Here my clients.js file:
exports.create = function(){
self.team = Settings.generateRandomTeam(); /*The problem is here. I can´t access the Settings variable*/
self.maxSpeed = Settings.maxSpeed;
...
...
return self;
}
EDIT 2:
When I use Settings = require("settings.js") I get the following error:
TypeError: Settings.generateRandomTeam() is not a function.
My settings.js file is like this:
Settings.generateRandomTeam = function(){
//Some code
}
module.exports = Settings;
Thanks!
var foo = require('foo.js');
. Make sure to export what you want to access
– Andrew Li
Commented
Aug 29, 2016 at 1:06
var foo = require('foo.js');
- This is trying to require a global module on your puter. Try var foo = require('./foo.js');
– James111
Commented
Aug 29, 2016 at 1:09
To get access to the Settings
object from the clients.js
file, just add this to your clients.js
file:
const Settings = require("settings.js");
exports.create = function(){
self.team = Settings.generateRandomTeam(); /*The problem is here. I can´t access the Settings variable*/
self.maxSpeed = Settings.maxSpeed;
...
...
return self;
}
Modules in node.js are cached so two require()
statements that resolve to the exact same file path will return the exact same cached module. So, because of the caching, you will actually get the same Settings
object each time you call require("settings.js")
.
This is a key to sharing in node.js. You don't think about loading something once and then trying to share that everywhere. Instead, you think about having each module require()
in what it needs (when possible). This promotes the independence and reusability of your modules as they tend to be more stand-alone useful rather than depending upon some global environment that's already been built for them.
There are, of course, other ways to share data by pushing data to a module in a module constructor function or pulling data by having a module call out to other modules when it loads to retrieve data from them. This may be necessary in some cases when a module needs to be given a specific instance of something (like a database handle).
I was require
ing a local module too. It was saying that the module was simply not found. But I figured it out after a few seconds...
myModule = require("./myModule.js")
You can of course replace myModule
with anything you have.