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 | Show 2 more comments2 Answers
Reset to default 2Yes, 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
orrequire
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
版权声明:本文标题:javascript - Can we import from CommonJS modules in Node now using ESM import syntax? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744007103a2574900.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
node
is still changing circa 2024-2025 relevant to CommonJS and ECMAScript Module loading. – guest271314 Commented Mar 29 at 18:56node
nightly. – guest271314 Commented Mar 29 at 19:54