File IO in NodeJS with ES6 Modules
For some reason __dirname does not work when using CommonJS modules (the require() function).
So if you are using ES6 Modules, then this should work:
import * as fs from 'fs';
import { dirname } from 'path';
import { fileURLToPath } from 'url';
const __dirname = dirname(fileURLToPath(import.meta.url));
fs.writeFileSync(__dirname + "/somefile.txt", "Hello World!"));