Function parameters with default value is not compiled properly to node environment
Hi,
i'm working on a node project and i recently modified my project to support the new feature of async/await :+1: . as part of the modification i'v change the target to ES6 and added "strict mode" on the headers.
now i'm getting an error from node when trying to execute the project because of a compile error. the exception is thrown while node try to parse a function that has a defined parameter with default value.
it's seems that the equal symbol is compiled with the default value but the node compiler don't understand it.
the function is:
/**
* Creating an Error Message for the response.
* @param error - the error to show
* @param statusCode - the status code.
* @returns {{statusCode: number, error: any}}
*/
public static CreateErrorResponse(error, statusCode = 0) {
return {
statusCode: statusCode,
error: error
};
}
the output of the compiler is:
static CreateErrorResponse(error, statusCode = 0) {
return {
statusCode: statusCode,
error: error
};
}
Exception:
/home/elior/dev/genesis/server/genesis-api-server/dist/server/utils.js:3
static CreateErrorResponse(error, statusCode = 0) {
^
SyntaxError: Unexpected token =
at exports.runInThisContext (vm.js:53:16)
at Module._compile (module.js:404:25)
at Object.Module._extensions..js (module.js:432:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:313:12)
at Module.require (module.js:366:17)
at require (module.js:385:17)
at Object.<anonymous> (/home/elior/dev/genesis/server/genesis-api-server/dist/server/actions/data-types/setDataType.js:15:15)
at Module._compile (module.js:425:26)
at Object.Module._extensions..js (module.js:432:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:313:12)
at Module.require (module.js:366:17)
at require (module.js:385:17)
at Object.api.actions.loadFile (/home/elior/dev/genesis/server/genesis-api-server/node_modules/actionhero/initializers/actions.js:75:26)
at /home/elior/dev/genesis/server/genesis-api-server/node_modules/actionhero/initializers/actions.js:104:21
my compile options:
"compilerOptions": {
"target": "ES6",
"module": "commonjs",
"moduleResolution": "node",
"isolatedModules": false,
"jsx": "react",
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"declaration": false,
"noImplicitAny": false,
"removeComments": true,
"noLib": false,
"preserveConstEnums": true,
"suppressImplicitAnyIndexErrors": true,
"outDir": "../../dist/server/"
}
i have tried to search this on google but with no luck..
i'm using node v5.1.1 and typescript 1.7.3 to compile to code.
have you tried --harmony-default-parameters? (https://github.com/nodejs/node/issues/3070)
@eliorcohen you are using target:ES6 so tsc emits the statusCode = 0 default parameter into the JavaScript, but V8 doesn't support default parameters yet (unless using special flags as @vladima suggests), so you get a runtime error.
This is an example of the granular targeting problems discussed in #4692, e.g. see https://github.com/Microsoft/TypeScript/issues/4692#issuecomment-160789493
If you want to use target:ES6 with Node, you just have to be careful to avoid using ES6 features V8 doesn't support yet (e.g. default parameters, destructuring) as you won't get a compile-time error but you will get a runtime error.
i have added the --harmony-default-parameters flag to the run command, and it's seems to work, thanks @vladima .
this is not the desired solution from my perspective, but will work for now, hoping until i finish the project the feature will not needed to be flag with harmony, i don't know if there is any more limitations..
i'm fully supporting the idea in #4692 that by @weswigham to enable/disable features be demand that fit the target platform and environment.
thanks for the help!
Would target es5 be redundant but resolve this?
@amcdnl You can't use await/async unless you target es6
Interestingly, it's 2016 now, and I have the opposite problem!
I want TypeScript to output es6/es2015 code with the above mentioned default parameters syntax.
Any idea how to do this?
Currently it converts this:
static MyFunc(arg1, arg2 = true): any {
}
Into this:
function MyFunc(arg1) {
var arg2 = arguments.length <= 1 || arguments[1] === undefined ? true : arguments[1];
}
Whereas I want it to keep the default-parameters syntax, like this:
static MyFunc(arg1, arg2 = true) {
}
I want TypeScript to output es6/es2015 code with the above mentioned default parameters syntax.
tsc --target es2015
We had our nodejs installed at:
C:\Program Files\nodejs
So, copied from: C:\Program Files\nodejs to: C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Web\External
This solved the issue of older nodejs in visual studio environment.