I am trying to export a module in Node.js with some functions in it. One of the functions have optional parameters which include default values.
module.exports = {
foo = (a, b = 2, c = {y:0}) => {
// Code
},
bar = () => {
// Codes
},
}
The functions work when it is not exported. When moved into module.exports
, the following error occurs:
SyntaxError: Invalid shorthand property initializer
at Module._compile (internal/modules/cjs/loader.js:723:23)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
at Module.require (internal/modules/cjs/loader.js:692:17)
at require (internal/modules/cjs/helpers.js:25:18)
at Object.<anonymous> (/<link to this file>:2:62)
at Module._compile (internal/modules/cjs/loader.js:778:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
at Module.require (internal/modules/cjs/loader.js:692:17)
at require (internal/modules/cjs/helpers.js:25:18)
at Object.<anonymous> (/<link to server.js file>:20:18)
I am aware the parameters can be default-ed in the code of the function. Still, does default-ing parameters in brackets not work in module.exports
? Any help in fixing it? Thanks.
You object notation is incorrect:
Change foo = () => {}
to foo: () => {}
module.exports = {
foo: (a, b = 2, c = {y:0}) => {
// Code
},
bar: () => {
// Codes
}
}