I have a package.json
file that contains the line "type": "module",
. This line triggers errors when running node a_script.js
. Its as if the default parameter functionality of ECMAScript is inactivated.
// a_script.js
const doThing = (foo = "A", bar = "B") => {
console.log(foo);
console.log(bar);
}
doThing(foo="AYE") // Out: AYE\nB
doThing(bar="BEE") // Out: A\nBEE
doThing(foo="AYE", bar="BEE") // Out: AYE\nBEE
doThing() // Out: A\nB
Removing "type": "module",
from package.json
allows the script to run without errors. The error in question is,
dothing(foo="AYE")
^
ReferenceError: foo is not defined
What is the reason for this?
foo
is undeclared variabledoThing(bar="BEE") // Out: A\nBEE
- really?foo=
andbar=
in the function calls don't do what you thought they did - if youlet foo, bar
before all those function calls, then your code will work, again, not doing what you think it's doing - also, if you"use strict"
at the top of the file, you'll see it fail when incommonjs
mode