module.exports/require of Map: same data but object is not a Map anymore

Viewed 16

using node.js v16.15.0 I export this map from a ./constants.js file

const NETWORKS_LOOKUP = new Map([
    ["1", "mainnet"],
    ["3", "ropsten"],
    ["4", "rinkeyby"],    
    ["5", "goerli"],
    ["2a", "kovan"],
    ["31337", "Hardhat"]
]);
module.exports = {NETWORKS_LOOKUP}

in another file I use

const NETWORKS_LOOKUP = require("./constants.js")

and when I do something like this with it

const CONTRACT_NETWORK = "1"
console.log(NETWORKS_LOOKUP.get(CONTRACT_NETWORK))

I get the error

TypeError: NETWORKS_LOOKUP.get is not a function

and indeed if I do this it returns false

console.log(NETWORKS_LOOKUP instanceof Map)

So why is my map losing it's mappyness? It actually still looks like it should be a map as if I do this

console.log(NETWORKS_LOOKUP)

is produces this - it still looks like a map

{
  NETWORKS_LOOKUP: Map(6) {
    '1' => 'mainnet',
    '3' => 'ropsten',
    '4' => 'rinkeyby',
    '5' => 'goerli',
    '2a' => 'kovan',
    '31337' => 'Hardhat'
  }
}

I have used this same setup in another project, although it was a react project and using export {NETWORKS_LOOKUP}; & import { NETWORKS_LOOKUP } from './constants';

I don't want to change my project to using export/import as that will give me other headaches.

I need to get it working with module.exports and require so that it's still a map and I can use .get() to do lookups.

0 Answers
Related