I'm working with node.js, and in one of my js files I'm using const
in "strict mode"
. When trying to run it, I'm getting an error:
SyntaxError: Use of const in strict mode.
What is the best practice to do this?
Edit:
'use strict'
const MAX_IMAGE_SIZE = 1024*1024; // 1 MB
Usually this error occurs when the version of node against which the code is being executed is older than expected. (i.e. 0.12 or older).
if you are using nvm than please ensure that you have the right version of node being used. You can check the compatibility on node.green for const under strict mode
I found a similar issue on another post and posted my answer there in detail
const is not supported by ECMAScript. So after you specify strict mode, you get syntax error. You need to use var instead of const if you want your code to be compatible with all browsers. I know, not the ideal solution, but it is what it is. There are ways to create read-only properties in JavaScript (see Can Read-Only Properties be Implemented in Pure JavaScript?) but I think it might be overkill depending on your scenario.
Below is browser compatibility note from MDN:
Browser compatibility