admin管理员组

文章数量:1356112

Can we import from CommonJS modules in Node now using ESM import syntax?

So if commonjsmodule is a cjs module that exports like this:

exports.add = function() {...}

Can we import from it like this assuming that we are working within ESM?

import { add } from "commonjsmodule";

In this YouTube video the author is demoing the type property and what it does in package.json.

And he does it by creating one cjs file and one mjs file and an app and in the app he's importing from both using ESM import syntax, and I just assumed that additional tooling would be needed for the cjs file.

Can we import from CommonJS modules in Node now using ESM import syntax?

So if commonjsmodule is a cjs module that exports like this:

exports.add = function() {...}

Can we import from it like this assuming that we are working within ESM?

import { add } from "commonjsmodule";

In this YouTube video the author is demoing the type property and what it does in package.json.

And he does it by creating one cjs file and one mjs file and an app and in the app he's importing from both using ESM import syntax, and I just assumed that additional tooling would be needed for the cjs file.

Share Improve this question edited Mar 29 at 18:46 jonrsharpe 122k30 gold badges268 silver badges475 bronze badges asked Mar 29 at 18:36 OleOle 47.4k70 gold badges237 silver badges445 bronze badges 7
  • 2 nodejs./api/esm.html#interoperability-with-commonjs – jonrsharpe Commented Mar 29 at 18:45
  • This is NOT a duplicate question. You can't link to questions and answers from years ago for this subject matter. node is still changing circa 2024-2025 relevant to CommonJS and ECMAScript Module loading. – guest271314 Commented Mar 29 at 18:56
  • I wonder which node version the video shows. Because this is not normal behavior and even if this were possible, I wouldn't expect it to be available without flags – Estus Flask Commented Mar 29 at 19:51
  • @EstusFlask The video date says 3 years ago. Download node nightly. – guest271314 Commented Mar 29 at 19:54
  • @guest271314 If that's the case it's probably the best illustration why not use nightly on daily basis, at least a dev won't confuse themself and others with such a demo that can't be reproduced. This was around v17, I don't remember anything like that arriving in the release, this would be a breaking change – Estus Flask Commented Mar 29 at 20:06
 |  Show 2 more comments

2 Answers 2

Reset to default 2

Yes, in Node.js it's possible to import CommonJS modules from ECMAScript modules. The documentation has more information.

This has been the case for as long as Node.js has supported ECMAScript module syntax (I just tested to verify that it works as far back as v8.5.0).

The last time I checked the Node.js authors were working on something like that.

I used to use --experimental-default-type=module before that capability was removed, ostensibly because of "syntax" analyzation.

I generally don't use a package.json file at all.

I use node nightly. It runs ECMAScript Modules or CommonJS just fine without any package.json.

You can certainly run CommonJS and ECMAScript Modules in Bun, right now, without an controversy.

Using import and require() together

In Bun, you can use import or require in the same file—they both work, all the time.

import { stuff } from "./my-commonjs.cjs";
import Stuff from "./my-commonjs.cjs";
const myStuff = require("./my-commonjs.cjs");

Really, it's far simpler to pick a side and go with that.

CommonJS is an archaic module laoding system that is non-standard.

本文标签: javascriptCan we import from CommonJS modules in Node now using ESM import syntaxStack Overflow