Reputation: 497
import path from 'path';
import fs from 'fs';
fs.mkdirSync(path.join(__dirname, 'folderName'));
I want to create directories in node, when I require the modules (commonjs) everything works but when I change the type in my package.json
to module
and use imports the folder doesn't get created, what could I be doing wrong?
Upvotes: 0
Views: 1316
Reputation: 497
I figured __dirname
isn't available on es6 modules so I just replaced that with './'
. You could use an npm package for that if you are looking for elegance.
Upvotes: 0
Reputation: 707328
There is no __dirname
in an ESM module. If it's something you need, you can manufacture it with this:
import path from 'path';
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
But, many of the functions in the fs
module can take import.meta.url
more directly. See this other answer for details on that.
Upvotes: 2