New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ECMAScript Modules not working #16844
Comments
you have to use |
ahh, ok, but why Code: import fs from 'fs';
const start = () => {
console.log(`${__dirname}`);
console.log(`${__filename}`);
const buffer = fs.readFileSync(`${__filename}`)
const string = buffer.toString();
console.log(string);
}
start(); Error:
|
esm is not node-specific, and node-specific "globals" (such as |
i got it, it works, but it is too experimental to use it. thanks! |
@p3x-robot if want a temporary solution to //#region META
/** @todo Replace with import.meta eventually */
const FILENAME = typeof __filename !== 'undefined' ? __filename : (/^ +at (?:file:\/*(?=\/)|)(.*?):\d+:\d+$/m.exec(Error().stack) || '')[1];
const DIRNAME = typeof __dirname !== 'undefined' ? __dirname : FILENAME.replace(/[\/\\].*?$/, '');
//#endregion
console.log(process.argv[1], { FILENAME, DIRNAME }); |
awesome, thanks. |
@SMotaal solution is great. It needs a little fix https://regex101.com/r/pTc2Zm/1 It's not the best ( |
i i'm using node 10.12.0 |
@datdinhquoc you can use import { fileURLToPath } from 'url';
console.log(fileURLToPath(import.meta.url)); |
import path from 'path console.log( __dirname ) |
@guybedford import { fileURLToPath } from 'url';
import { dirname } from 'path';
console.log(dirname(fileURLToPath(import.meta.url))); btw this became very overcomplicated and can't be executed from external module for convenience because |
Command:
node --experimental-modules scripts\es6modules.js
Code:
Error:
The text was updated successfully, but these errors were encountered: