Polly
Polly

Reputation: 657

How export variable correctly nodejs

Suppose to have to file A.js and B.js: the path of this folder is:

A.js is in Project/Home and B.js is in Project

Project
    Home 
      A.js
    B.js

Suppose the code of B.js is something like this:

var elementi={name:'hi'};

    var esporta={};

    esporta.getAllElementi = elementi;

    module.exports = esporta;

The code of A.js is :

var elementi=require("../B.js");
//but when I call

var variable=elementi.getAllElementi;

the problem is the variable is null, Anyone can help me?

Upvotes: 0

Views: 36

Answers (1)

bassi
bassi

Reputation: 61

I ran your code , was unable to reproduce the error.

Try replacing B.js with

var elementi={name:'hi'};

exports.getAllElementi = elementi;

and replace A.js with

var elementi=require(__dirname+"/../B.js");
var variable=elementi.getAllElementi;
console.log(variable);

If none of the above changes work , then try updating you Node js package.

Upvotes: 1

Related Questions