I am typing this in Visual Studio Code
const tours = JSON.parse(fs.readFileSync(`${__dirname/dev-data/data/tours-simple.json}`));
But when I save the file, my tours constant changes to this
const tours = JSON.parse(fs.readFileSync(`${__dirname / dev - data / data / tours - simple.json}`));
That makes my code non functional. How to remove the white spaces?
Thanks, Theo.
CodePudding user response:
You aren't writing the path in correctly - when you put it inside ${var}
, this denotes to JS that you want to escape that bracket from a string, and into a variable called var
. JS will get the value of var and put it back into the string.
Example:
const a = 'a'
console.log(`${a}b`) // Prints 'ab'
CodePudding user response:
Your closing }
character is in the wrong place. It's at the end of the string, when it should actually come right after the __dirname
variable. Try this instead:
const tours = JSON.parse(fs.readFileSync(`${__dirname}/dev-data/data/tours-simple.json`));