Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to import custom js modules using es module imports

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

Since the date.js file is in the same directory, you can use relative path:

import date from "./date.js"
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading