I am trying to use ES6 on server side. I a have separated endpoint from server file:
archive.js:
import { Router } from "express";
const router = Router();
router.get("/", async (req, res) => { "some code"});
export default router;
when i want to import it to my server file like this:
import archive from "./endpoints/archive.js";
app.use("/archive", archive);
it gives me an error:
(node:9565) ExperimentalWarning: The ESM module loader is experimental. internal/modules/run_main.js:54 internalBinding('errors').triggerUncaughtException()
any idea guys? i dont want to go back to require/module.exports
you can use Node v14.12.0; where this probleme is solved, and read this doc https://nodejs.org/api/esm.html#esm_package_json_type_field
I realize after a few tries that "--experimental-modules" flag on the "nodemon" causes issues to realize the imports in the whole app so I did update that flag to use the new flag for explicit filenames"--es-module-specifier-resolution=node" and now works perfect here is an article about this new flag : https://nodejs.medium.com/announcing-a-new-experimental-modules-1be8d2d6c2ff
So for explane you can do a script like this:
nodemon --es-module-specifier-resolution=node
Also keep in mind the nodejs version you use a version under 12.XX, 14.XX or the last stable version.
Upgrade Node.js to the latest version on Mac OS
I hoper this helps more developers :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With