Declaring a variable as const requires an immediate indication of its value, and this reference cannot be changed.
So you cannot define it in one place (outside of try ) and assign it a value somewhere else (inside try ).
On the other hand, creating and creating a value in a try block is great.
However, const is a block cloud, such as let , so if you create it and assign it a value in your try block, it will exist only within this area.
Therefore, if you need to access this variable outside of try , you should use let :
let configPath; try { configPath = path.resolve(process.cwd(), config); } catch(error) {
Alternatively, although probably more obscurely, you can use var to create a variable in try and use it outside it, because var has a scope within a function, not a block (and gets hoisted ):
try { var configPath = path.resolve(process.cwd(), config); } catch(error) {
source share