Is it absolutely the case that arrays are used by reference in other modules in Node.js?

Viewed 119

I have

// file cars.js
var bodyshop = require('./bodyshop')

var connections = [];

many functions which operate on connections. adding them, changing them etc.

    code in this file includes things like
    bodyshop.meld(blah)
    bodyshop.mix(blah)

exports.connections = connections

and then

// file bodyshop.js
let cars = require('./cars');

even more functions which operate on connections. adding them, changing them etc.

    code in this file includes things like
    cars.connections[3].color = pink
    cars.connections.splice(deleteMe, 1)

module.exports = { meld, mix, flatten }
  1. Is it absolutely honestly the case that code in bodyshop such as cars.connections.splice(deleteMe, 1) will indeed delete an item from "the" connections (ie, the one and only connections, declared in cars.js) and code in bodyshop such as cars.connections[3].color = pink will indeed change the color of index 3 of "the" self-same one and only connections?

  2. Is it quite OK / safe / acceptable that I used the syntax "module.exports = { }" at the end of bodyshop, rather than three lines like "exports.meld = meld" ?

  3. Is this sentence indeed to totally correct?? "In Node.js if you export from M an array, when using the array in another module X which requires M, the array will be by reference in X, i.e. not by copy" ... ?

1 Answers

I created two files with the following methods and the array as you mentioned.

First File: test1.js

const testArray = [];
const getArray = () => {
    return testArray;
};
module.exports = {
    testArray,
    getArray
}

Second File: test2.js

const { testArray, getArray } = require('./test1');

console.log('testing the required array before modifying it');
console.log(getArray());
testArray.push('test');
console.log('testing the method result after modifying the required array content');
console.log(getArray());

If you can create the mentioned files and run them locally, you will see the following result.

>node test2.js
testing the required array before modifying it
[]
testing the method result after modifying the required array content
[ 'test' ]

The points observed is,

  1. yes, it's okay if you want to export it with the syntax module.exports = { }, It not much an issue.
  2. If any of the methods modify this array outside of the required file, it will affect here as well, This because require will be a reference, not a copy.

The one possible solution will be creating a JSON copy of it while requiring as below:

const { testArray, getArray } = require('./test1');
const testArrayCopy = JSON.parse(JSON.stringify(testArray));

console.log('testing the required array before modifying it');
console.log(getArray());
testArrayCopy.push('test');
console.log('testing the method result after modifying the required array content');
console.log(getArray());

This is the result:

>node test2.js
testing the required array before modifying it
[]
testing the method result after modifying the required array content
[]

Note: JSON copy will not help you in parsing DateTime properly.

Related