When using the commonjs I used to import custom modules like this:-
const date = require(__dirname + "/date.js");
after switching to es module type I can no longer use the __dirname without creating one manually
import { fileURLToPath } from 'url';
import { dirname } from 'path';
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
import date from __dirname + "/date.js"
When I use this it shows me "unexpected identifier __dirname" on the import line.
date.js is in the same level of directory as this code file is in.
How do I import custom js module using es module syntax? Please help
Thank you
Since the date.js
file is in the same directory, you can use relative path:
import date from "./date.js"