Skip to content

Instantly share code, notes, and snippets.

@Krabaton
Created May 24, 2018 23:49
Show Gist options
  • Save Krabaton/668a8de2d104624d14f53c960e26f2b4 to your computer and use it in GitHub Desktop.
Save Krabaton/668a8de2d104624d14f53c960e26f2b4 to your computer and use it in GitHub Desktop.
There have been proposals about exposing these variables through import.meta, but for now, you need a hacky workaround that I found here:
// expose.js
module.exports = {__dirname};
// use.mjs
import expose from './expose.js';
const {__dirname} = expose;
In Node.js 10 there's an alternative that doesn't require creating multiple files:
import path from 'path';
const __dirname = path.dirname(new URL(import.meta.url).pathname);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment