0

Hi I want to share a global variable across 2 java script files, I know if I define the variable as global I should be able to access it across java-script files. But in my case it didn't work. I couldn't figure out why, I want to share the messages variable in first.js file to be access in second.js file. When I tried to access it it shows an error as messages is not defined. Any suggestions?

//first.js

let express = require('express');
let router = express.Router();

let elastic = require('../server');
let esClient = elastic.esClient;
let messages = [];

module.exports = router;

//second.js

let express = require('express');
let router = express.Router();

let elastic = require('../server');
let esClient = elastic.esClient;
let messages2 = [];
let data = new Set();

print();

function print(){
  console.log(messages.length);
}

module.exports = router;

//server.js

let express = require('express');
let app = express();
let bodyParser = require('body-parser');

const elasticsearch = require('elasticsearch');
const esClient = new elasticsearch.Client({
    host: '127.0.0.1:9200',
    log: 'error'
});

app.use(bodyParser.json());

app.use((req, res, next) => {
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
    next();
});

module.exports = {
    elasticsearch,
    esClient
};

app.use(require('./routes/first'));
app.use(require('./routes/second'));

app.listen(63145);

3 Answers3

0

You can export multiple objects as a single object on module.exports... like so:

let express = require('express');
let router = express.Router();

let elastic = require('../server');
let esClient = elastic.esClient;
let messages = [];

module.exports = { router: router, messages: messages };

Then in second file:

let express = require('express');
let router = express.Router();
let {messages} = require('./path/to/first.js');
let elastic = require('../server');
let esClient = elastic.esClient;
let messages2 = [];
let data = new Set();

print();

function print(){
  console.log(messages.length);
}

module.exports = router;

But honestly I wouldn't do it this way. instead have your messages array built out in it's own file that you can export explicitly, or set up as a class that you can access the array via methods. Then export that and use across other files.

dvsoukup
  • 1,586
  • 15
  • 31
  • I tried that one, and showed me this error. ```throw new TypeError('app.use() requires a middleware function').``` –  Oct 17 '17 at 03:55
  • You need to specifically export the "route" object from the first script, and use that in the `app.use()` of your first require of where you hook up your routes to your app. Honestly though this is bad design. Really, just create a third script, create a class, write function to access that method and write to it, and import that separately. – dvsoukup Oct 17 '17 at 03:59
0

It looks like tvanfosso has the answer for you at:

Global variables in Javascript across multiple files

So one of two things may be happening. You are not inserting the scripts into the html element in the correct order, or it's not being referenced correctly.

0

You can try this . Adding properties to global object available in node.js. It will be available throghout your node project //first.js

let express = require('express');
let router = express.Router();

let elastic = require('../server');
let esClient = elastic.esClient;
global.messages = [];

module.exports = { router: router };

//second.js

let express = require('express');
let router = express.Router();    
let elastic = require('../server');
let esClient = elastic.esClient;
//let messages2 = [];

let data = new Set();

print();

function print(){
  console.log(global.messages.length);
}

module.exports = router;
Ankit Manchanda
  • 562
  • 6
  • 21
  • This works fine, but I think it is not a good approach to use a global variable like that, correct me if I am wrong. –  Oct 17 '17 at 04:44
  • If you intend to use global variables in your Node application, the discussed global object method works fine. Try not to overuse it, though. Alternative solution of this is use of module.exports. – Ankit Manchanda Oct 17 '17 at 04:52
  • Hmm, I was trying to use module .exports but it gives me an error as not defined. Have to figure out a way to do it in module.export.Any way thanks for this one. –  Oct 17 '17 at 05:11