1

I have to export 2 type of constants and few functions like getUser.

mymodule.js

const type1 = require('./constants1.js');
const type2 = require('./constants2.js');
module.exports = Object.freeze(Object.assign(Object.create(null), type1, type2))

constants1.js

module.exports = Object.freeze({
  DOUBLE: 1,
  FLOAT: 2
})

consumer.js

const udb = require('./mymodule.js');
console.log(udb.DOUBLE);

Now I also want to export function like getUser , how do i change mymodule.js to export the functions too , so that consumer.js can call udb.getUser

something like this but it doesnt work, Please suggest.

module.exports = Object.freeze(Object.assign(Object.create(null), type1, type2)), getUser: function() {} 
6
  • You can wrap into a object like this - module.exports ={a: Object.freeze(Object.assign(Object.create(null), type1, type2)), b:getUser()} Commented Jul 5, 2022 at 15:47
  • This makes consumer.js to use udb.a.DOUBLE instead of udb.DOUBLE, right? I dont want to change in the consumer.js calling pattern Commented Jul 5, 2022 at 16:25
  • then don't give name a & b to keys. keep it what you want & destructure it Commented Jul 6, 2022 at 6:17
  • you mean something like this {Object.freeze(Object.assign(Object.create(null), type1, type2)), getUser()} ? . I get SyntaxError: Unexpected token '.' for Object.freeze Commented Jul 6, 2022 at 7:00
  • nope! give your keys a friendly name & destructure them Commented Jul 6, 2022 at 8:01

2 Answers 2

1

constant1.js

module.exports = Object.freeze({
    DOUBLE: 1,
    FLOAT: 2
});

constant2.js

module.exports = Object.freeze({
    TRIPLE: 3,
});

mymodules.js

const getUser = ()=> console.log("Something");
const getId = ()=>console.log("Something2");

const type1 = require("./constants1.cjs");
const type2 = require("./constants2.cjs");

const udb = Object.assign(Object.create(null), type1, type2);
udb.getUser = getUser;
udb.getId = getId;
module.exports = Object.freeze( udb );

consumer.js

const udb = require("./mymodule.cjs");
console.log(udb.DOUBLE);

// udb.getUser();                    
// udb.getId();                    

EDIT: Added a complete example

EDIT 2: Simpler

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for complete example. consumer.js code is something I cant ask them to change. Its like this udb = require("./mymodule.js"); udb.DOUBLE; udb.getUser(). I am trying to change library (mymodules.js) to split constants in mymodule.js to different js files based on their type and possibly include statically/dynamically.
Hi @SudarshanSoma, I have edited my answer to reflect the consumer.js limitation.
You're welcome (<- I believe the system automatically deletes these types of messages, so you may not see them).
1

You could use spread operator

const type1 = require('./constants1.js');
const type2 = require('./constants2.js');

function getId() {}
function getUser() {}

module.exports = Object.freeze({ getId, getUser, ...type1, ...type2 })

3 Comments

Dear Bill.Gates, I want to point out that we all offer a similar solution to the OP (Reading @Gulshan Aggarwal's comments, now). An important thing to notice is that we all have solved the export issue the same way.
@PatriceThimothee what do you want to say with this?
@bill.gates, I Apologise if it came up the wrong way. The OP wanted to take another direction in maybe solving this exporting issue. The three of us came up with the same solution, so it would be great for OP to be aware of this, so this solution is considered rather than thinking something more drastic.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.