I have a node application that emits HTML files. This is the jest of how it works:
const fs = require('fs');
const outputPath = './dist/html/';
// code that generates names and content
const currentFile = `${outputPath}${name}.html`;
const content = '...';
fs.promises.writeFile(currentFile, content, 'utf8');
This works as intended, but generally it is a bad practice to write relative path this way (this works on Mac but probably would not work on Windows machine).
const fs = require('fs');
const path = require('path');
const outputPath = path.join(__dirname, 'dist', 'html');
// code that generates names and content
const currentFile = path.join(outputPath, `${name}.html`);
const content = '...';
fs.promises.writeFile(currentFile, content, 'utf8');
This works, but it creates an entire path (User/my.name/Documents/projects/my-project/dist/html/my-file.html) within my project, since fs.writeFile
writes the file relative to the working directory.
Can I make fs
write file to the absolute path? Alternatively, what is the proper way of generating relative paths?
I ended up using
const outputPath = `.${path.delimiter}dist${path.delimiter}ads${path.delimiter}`;
But this does not seem like the best possible solution.