javascript - NodeJs Require a File - Stack Overflow

admin2025-04-17  0

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!

Share edited Aug 29, 2016 at 2:03 Daniel Oliveira asked Aug 29, 2016 at 1:05 Daniel OliveiraDaniel Oliveira 1,2901 gold badge15 silver badges36 bronze badges 16
  • 1 You just do var foo = require('foo.js');. Make sure to export what you want to access – Andrew Li Commented Aug 29, 2016 at 1:06
  • Make sure you use the right path! If you require your file like 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
  • 3 First off, in node.js don't ever think about using global variables (99.9999% or the time they are not needed or appropriate). You need to learn how modules in node.js work and how to share data among modules. Hint: using globals is NOT the way to do things in node.js. You can use a push or pull model to share. The simplest way is to call a module constructor and pass it the data you want to share which I refer to as the "push" model since you're pushing data to the other module to share. – jfriend00 Commented Aug 29, 2016 at 1:09
  • Hello jfriend00. I think you hit the point. Can you explain me or send a good tutorial about pushing or pull data between node modules? Because I have modules that depend on other modules. I search on google before asking here but I did not find much. – Daniel Oliveira Commented Aug 29, 2016 at 1:18
  • 1 This site works a lot better if you show us the code you have for two modules and explain what you want to share between them and we can show you exactly how to solve that problem. Without that, we have to e up with a generic tutorial on sharing data between modules which is both a lot more for us to write and it only has about a 30% chance of actually covering what you want. So, please be more specific in your question and include code from your two modules that shows what you want to share and which module is likely to be loaded first. – jfriend00 Commented Aug 29, 2016 at 1:21
 |  Show 11 more ments

2 Answers 2

Reset to default 2

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 requireing 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.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1744856877a270880.html

最新回复(0)