node.js adding new export field without braking existing requires - node.js

Consider following simple scenario, file config.js:
const config = {
a: '123'
}
module.exports = config;
and it's usage:
const cfg = require('./conifg');
console.log(cfg.a);
Now I'm in need to add additional export member to config.js:
const config = {
a: '123'
}
function someFunction() {
console.log('blah');
}
module.exports = {
config,
someFunction
};
This modification brakes down so-far working code, because cfg.a in
const cfg = require('./conifg');
console.log(cfg.a);
points now to undefined.
Is there any way to extend module.exports while remaining it's "default" exported member to not brake things down?

you may export all property of config separately
module.exports = {
...config,
someFunction
};
or if you don't want use spread, you can access by
const cfg = require('./conifg');
console.log(cfg.config.a);

You can do it like this
const config = {
a: '123',
someFunction: someFunction
}
function someFunction() {
console.log('blah');
}
module.exports = config;

Related

How can I export async await functions to use like a utils file?

I have a utils file where I have async await functions.
I am exporting these functions like this:
module.exports.pressing = pressing;
module.exports.login = login;
module.exports.logout = logout;
module.exports.getImage = getImage;
module.exports.decodeUnicodeCharacters = decodeUnicodeCharacters;
module.exports.initApp = initApp;
What I would like to achieve is that through the protractor configuration file I can call the functions of the utils file like this:
browser.params.utils.pressing(protractor.Key.DOWN);
What is the best way to do it?
Create a module and require anywhere you want to use it
utils.js
module.exports = {
pressing: function () {
// what it does
},
login: function () {
// what it does
},
logout: function () {
// what it does
},
getImage: function () {
// what it does
},
decodeUnicodeCharacters: function () {
// what it does
},
initApp: function () {
// what it does
},
};
then anywhere in project
let utils = require('PATH/TO/utils.js')
await utils.login();
Make it global
config
// ...
onPrepare() {
global.utils = {
login: function () {
// what it does
},
}
}
// ...
anywhere in project
await utils.login() // no need to require

Object.assign required modules?

I'm trying to combine Object.assign with require for module import / export. Example use case:
// foo/bar.js
module.exports = () => console.log('foo')
// foo/biz.js
module.exports = Object.assign({}, require('./bar')
With the above, the object exported from biz.js is always empty. Yet to my eye it looks like it should contain the function exported from bar. Could someone ELI5 why it does not?
module.exports = () => console.log('foo')
You are exporting a function! Not an object with a key. You can try these ways:
Way 1:
// foo/bar.js
module.exports = () => console.log('foo')
// foo/biz.js
module.exports = Object.assign({}, {bar: require('./bar'})
Way 2:
// foo/bar.js
module.exports = {
bar: () => console.log('foo')
}
// foo/biz.js
module.exports = Object.assign({}, require('./bar')

How do I make "export default" compile to "module.exports"

I'm writing in typescript with it set to compile to commonjs. Whenever I put export default, it compiles into exports.default instead of module.exports. I am making an NPM package, so I want this to be fixed. How do I solve it? If needed, I can add my tsconfig.json file.
const test = ()=> 'text';
export = test;
compiled
"use strict";
const test = () => 'text';
module.exports = test;
click demo
typescript doc: https://www.typescriptlang.org/docs/handbook/modules.html#export--and-import--require
You can simply write it with module.exports in your TS file
const myExportedObj = {
};
module.exports = {
foo: myExportedObj,
};
Output:
var myExportedObj = {};
module.exports = {
foo: myExportedObj,
};
Or, as far as I can see from your question, you could don't export default but export instead
TS
export const myExportedObj = {
};
JS
exports.myExportedObj = {};
Another good approach is to make an index.ts file and export modules from there
TS
export * as Context from './context';
JS
exports.Context = require("./context");

why module.export { ...require('module') } work but not module.export { require('module') }

justExport.js
const first = () => {
console.log('frist from justExport')
}
const second = () => {
console.log('second fromt justExport')
}
module.exports = {
first,
second,
}
tmp.js
module.exports = {
...require('./justExport') // work
require('./justExport') // SyntaxError: Unexpected string
}
main.js
const justExport = require('./justExport.js')
const tmp = require('./tmp.js')
console.log('Hello World!')
I have voluntarily create a fake example with the less possible code.
{ ...require('./justExport') } is object literal spread. While { require('./justExport') } is incorrect object literal syntax because it doesn't contain a key.
Unless the intention is to create shallow copy of justExport module, object literal isn't needed. It can be:
module.exports = require('./justExport');
To further clarify the answer from #estus, note that the following works due to ES6 shorthand property names:
const justExport = require('./justExport');
module.exports = {
...justExport, // works
justExport // works because key is implicitly defined by variable name
}

How to override a method in Node.js?

I have a module that defines functions that will be shared by other modules. Some of those functions need to be overridden. This is what I was thinking, but it doesn't work:
// shared_module.js
module.exports = {
alternativeFun() {
exports.doSomething()
sharedFun()
},
sharedFun() {
console.log('shared')
}
}
// alternative_module1.js
module.exports = {
doSomething() {
console.log('alternative 1')
}
}
// alternative_module2.js
module.exports = {
doSomething() {
console.log('alternative 2')
}
}
// main.js
const shared1 = require('./shared_module')
shared1.doSomething = require('./alternative_module1').doSomething
shared1.alternativeFun()
const shared2 = require('./shared_module')
shared2.doSomething = require('./alternative_module2').doSomething
shared2.alternativeFun()
Setting module.exports does not magically change the value of exports. You will need to also assign it to the same value (module.exports = exports = ...) if you want to use it inside your module's functions.

Resources