Node.js 中的 ECMAScript 模块

在过去的几年里,Node.js 一直致力于支持运行 ECMAScript 模块 (ESM)。这是一个非常难以支持的特性,因为 Node.js 生态系统的基础是建立在一个名为 CommonJS (CJS) 的不同模块系统上。

两个模块系统之间的互操作带来了巨大的挑战,需要兼顾许多新功能;然而,Node.js 中对 ESM 的支持现在已经在 Node.js 中实现,尘埃落定。

这就是 TypeScript 带来两个新的 modulemoduleResolution 设置的原因:node16nodenext

{
    "compilerOptions": {
        "module": "nodenext",
    }
}

这些新模式带来了一些高级功能,我们将在这里进行探讨。

package.json 中的 type 和新扩展

Node.js 支持 package.json 中的新设置 称为 type"type" 可以设置为 "module""commonjs"

{
    "name": "my-package",
    "type": "module",

    "//": "...",
    "dependencies": {
    }
}

52gAK/XbqffApodoiIjvcIvzjez7onyWHKR8O7HesKdy/o1EW5kAOV7tTl1fMYS0B7JSr3kd3HK17XoFreCOfOpm/e5G707b6hvaPfo1XT1tAzi5JubNOwPYq9rHZ+k3ru0DVW7M7yr5WDN0drNLlH7BPJnfVXbits/NGOXJdVKEx7ypV3MWBHiqhdFMkdsb1s92XQh3UPljvhpXL1QOCkZxWr5p70awrtkSqJpOFd4M865p7CZuL6DMluG7ljUyumxkjS0ZxJelWhf0YuwCCglhHltKXIHobvmb74K+81FAowmELrPiRhYDco8MMEEj

    08xIhqqraoXCYfkfTKh79oZKQS/qB2vposKbVWiXTcmrcsjqb7spodx5w07qEwxa2r3fcWqtaxkrttHvYZOunwzqARAjDrA2d+HADeeCBN9l9tImAd6DXtEKvBRVPFD/JapzscYtzxW7pkqWUDoT98K1HFQxbBC5lMdbGYh0zNqjDu1hEs6rdRiM1Qq9QBoxjFFSa4s9XOEMQHjMPMbTqLtE6mPpjZ74pKJIpVWy1kqPRZUjARnSZVZSO+/aSLHjjvT1O1O32xrpMAWEdAbBKNVsk5l2Sdr1XicFXe78aKpBiS/EnA1rvsDSRlV/SU9w7lndKv6SGO8SwBakhuUTq+cciJBQtt2shzIc5OQWRQG7TeV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j63fU1ZkeUlnJzDyuBWB1JzR0I+yOtvXuRnaWz4rhmMggKD5E7qeUWVFiMhQzB0TAy85H6aMxp8nRoTFUyRt1JTZxl6zZbHcQ4MvNLm4ajqWB2HfC5Ezv1EoonX3NiSD/fjQUxhbr6KiN8AxRoOjr6JeaEVr2jVNbUIK3OqBE59PY3ypp1MNA5gDnI/tbuuJY5khAI1+FdTEH9JfXzJ8jat7Ac+bkGur/OI6RFE30NOHjRWSNwJmgFX2F71V0RSl+DcvlaK3LVsKuK+vYNfS+OHIc3Fv8aHQDws4n55H7MXzcdCsqThin+mSymPyjNaAHr+4zjXRfnqiSmkXpxicKy9RtE9hXWLhzyDDIzh9fMPrx+BMQJhQ==

B+yV9BMoLkcg0Kr25EtmhYWVorQdw7WNYzbI4z2JQHSbcbjpWwPCOwiAhwTI2PBC9irUtEEEIDHjcGwHVg6ZGsHOJhf4TmwIK+NKyLMuukEMhog7O/Pero8MSqmXDO4udQpK2NESLmsgesFMhyA0PoDNZP6zapU3xM8zmZGF5ZNpWEkw4WkcNC673WgryRWn+CkZpyVQ0Z96FrSTPeM2KA==

// ./foo.ts
export function helper() {
    // ...
}

// ./bar.ts
import { helper } from "./foo"; // only works in CJS

helper();

+hBoDTP4tQFiJUS8WrfGDyUx8r7dBsurc3RvAG684K8ei9CSt/AJiKrfwxZuNmluSXM6ZpcFtRirOPmGof5rtibiZNLdeq149UV9SB4m53LADpD1Uc0J5UQBM9uSzEGxt0DBAckDkEJyKlSqyQDf61dvO3hkq0OfyJkc2gkSCfWHDGYW52iCM3eh2BUMouBOMAaGY/0WAm/p8n6VVnP/hQTCDF2zcNloVZqO5L1TqDG5ZZtEmdhxI3WcSlaSpT0UwrOOP+1JnosiO3BwT4aCFDBiPLiAZOZHa9fIDIH66kPGoDnaHYBlZTEHsfk2DfS7h7UKbEdSE4nVG7LpAHPnqbGDr9TKitEhaknSStN38FY=

// ./bar.ts
import { helper } from "./foo.js"; // works in ESM & CJS

helper();

QYX3iEAbZLRx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B3PQ9+017X2LxdyO6uUjllnqqeFazsRglalpgp0N2Jz/Nv60WmP601Y04afFc086EkJPc1xE+iB9YKQGuYRgReUVWLUHw==

// @filename: helper.cts
export function helper() {
    console.log("hello world!");
}

// @filename: index.mts
import foo from "./helper.cjs";

// prints "hello world!"
foo.helper();

BR8Jcz5wYU9ywHHZrF/qOIr40tmEzPD+3WpFxxxdyxaMIJZVuyuqOuKo7Ih4PgOoX+Q2olfTThJavvHByBTwcxCZhUVzNpj1UsAr/5F7fFIQIXjNbaQ+dmQkHhD5JQcsHNry9nVz2V2Xxqomit41nvmtBnJRMl9YXIp7nMCfxxvWNKKKAhdhnCUbeYVoLU9WNaGLHAyh7831asasqIgH1odk+RTwbePxS4fBea2wJNLRZGZ/SA6RahLcXoV+6lawh1d2cKp7KrjsEpNEQ7YWukI1dH8ndH0EXyBJ1cJDgKUrLBSTbrdylFDYQkQxDaOwSiwai3Nrj/i7Sxu1cAJyAnTijcJTV28RgSpzxkoqERTDCtgLrpmv2npscx7Lnv9z

// @filename: helper.cts
export function helper() {
    console.log("hello world!");
}

// @filename: index.mts
import { helper } from "./helper.cjs";

// prints "hello world!"
helper();

vWqq+S8EMpfiUP8c1YnIgmZ2bzivbzGUhO49Y+G5//qnR30TeG8CyRZPJh1E6HiORkSI+u26Nk3FABzute8db86sI7fnO3MMZMceLWQyI9o9S6aD3vOv9C6n6ZhIN9P/tMcOYSUpKMNdQoFbHKf2r7Y3In8zlyOOsokQ9mN7lk1DSq/U027GaLagxqMfFPsCLCtkKQCcI506HPhB2qWgwT/FjEXRiWa4DRS+JRQfvyftK23ENJuSUYcPvtWebGRFgzKr58uaQZMOo73PdtsIAw==

NhuL/Y6QnewczGjfkb7Sezo1wXtEyWFYN3ENMny9kWHOoNujPHmj7HNvJZiOxanWEjFr72yqh0zzAmDSZZ5mRYEArpKVj4xAh7qF5ViXA+A=

import foo = require("foo");

wj38d1+0dlAJvK8D4pTULIGgixCpmTlL24i8M5JijFxW/RSD0gUoqq5QwZ3oTlDJDcU02eZvv0b+2IG8JpEDDhSOWL/Ww66u6ZowpXhNKMjhZBpbTgL/TOp/q6+btX3/E2GcESUZGkgdZA/TuHqC+kIBGIOOFtWYA61X6OVmJ9AqYQfiYeUT3NNRgsXPzzhHNreZVeE3KvfI9GbVPQwf/uyWJ7dAPcL4UYR4SqN2l/KHVbiiYJ4Dd8fwraGxd44Szt9Fm3wMZI3E/J6plzn4ZqCArS62j73xtumi7vGirjfV6mmtEZJDXGvpdhpLBa8ZY8XP4iSofONBKXabDjsnPWvCM2tAcrhgFuLJDzWVz5SV/bHn6W1VnSm0EOyfujhu6IztRtEJe78NNfP6qI25vEiH5phUcYMvF82BpSc8pA0NY3T316/oNbLDWqJwRa+Wa5M5BFgEne9qExI3y670up9WG8yJOZzsim04I8zjkiqdPvSuaiBIdZISsXOcV4fzWjw+VlK4OXXMNqe03nqPtnfUJ09Y/Xj2cCaCJ4Ge/sSP+BS+554Hxn1EAQKL5d9H

// @filename: helper.cts
export function helper() {
    console.log("hello world!");
}

// @filename: index.mts
import foo = require("./foo.cjs");

foo.helper()

jorZT7BeufliIqob6lg5wGRDrDBeWszaDXjmWZBcMID4IIwLcV/MxKtV0+LbJ6AibA29hUvC2vGumd5Jpq1QruCZvgf6fhZD1Q095ZRvMV79+Ity8aXkoU67u4dQmSSk2Cj3cYkbXHHejxO8dy05b4ebpw2KXDYHPLaF2IJVnOwITwfLdu7TYEGlojStf3FOvyCcIRPMnzclRWfLYEmyZtcphBdr8kNr/0d6cMZ3aiLN4dhOgrj/zSZtAisAxGD0

h9OjkBjg4gcpTrjpUpjLbZhH/J0Fu/huWLNeCK9c5r73DaohSzkg2iaSWzKeI0YKbWjZxAboOm3SBwZso2066MWCd+06YUqvNvwoxVWYq4PtB8GUx0py0CNBG2A+rITYLoUTDzbgr/ylFf0dfbIdH7BGE6cBPyu8on3cg3Yz/y2wQgQ07cWALavNh+Waa6Fu

package.json 导出、导入和自引用

LLDkm5dnOYOrelagjImYEflv+a+W+nD423ncAYzN1savKdQI9Wojs9PUhzy3Vl2AF2cUQSIEndQ9YKhEe1oCxVjCyuKFHPMgM6mKCHsiAMSZMX9VCUGNROYdDALIErNqQMHKjB5ojkgDQBrfKNKZbjSnp7rTZVq7J9wSgMfvVVNK+BxUjApb5ix320wlM52Vq0W503OaXNqoPLS6N8lxak6v8hbRn9ru1CyJc8LSCW6h5zNe8rGtL0T5mWm9v4Zpd3SuYvDVw/Sj2uW/ObAWgH+7q+EQHT4OuAtkKbOIT95mY9ZfJOX8Ekgm+/oTMCFp4xZXWJtaGS3MctZojXkW2XSIqGa7ZUk7N4cAqqLOQeC7sbYzdd8qC3JsdpoZFgREgmOXSO3iDOEeIeiRGR0xAz2EpLoDJpeJjqtaz5Mn1tAWKt8kGIcsboRtRiRSwAqn

rnn9IN6VOSZnUTUBACRYvq0KleSMB4OYNCYsGqA+Y8aqOsbdpcFeQVRCKkK734Vu7cBAjce+dblQnqOUDvtyGtb/HlcERQzuq7NKfeWUTUs02cgA8VOKxVfMcRJ8P1pv

// package.json
{
    "name": "my-package",
    "type": "module",
    "exports": {
        ".": {
            // Entry-point for `import "my-package"` in ESM
            "import": "./esm/index.js",

            // Entry-point for `require("my-package") in CJS
            "require": "./commonjs/index.cjs",
        },
    },

    // CJS fall-back for older versions of Node.js
    "main": "./commonjs/index.cjs",
}

fiA6qNVh7RJPvtJbJ2O5j+c+0iH7xXUSewNZa4OlMDa7U8guhCCrmo32FOtBB6jNqZ0wZN1umhK3I2LwTNWEi9uvORbCiIYfsGtKFyobn1hfaZhIUR9isKse2azIke056fw71YrAISS0Bq+GvNSsWBNcDBHrXmdlvqzgR/sQYE2VWAr/6XB+rxkbKXa65h70yPvsG1Q+Z3ZiH/SJkdFhS+va9bWBFktQtB8BuzXGgi3dp+oak5xYDiVXhbslh4AM

gBeg2hsCDQl8YrHSpT6u6TQ4/q5jTHKVqVcTLuGfEDSmJFbTEqRRDSPUzbUSPpRklZlgknn6+Z7DLsEzvPxpH2es1br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VYNOnzlsqjRyMTCsHlwmOQDgDlegHGrrocrrnS60rjmoSKO34wNAwc7Sf0tTjTwjPCXqeX3KwNN/qOs=

// package.json
{
    "name": "my-package",
    "type": "module",
    "exports": {
        ".": {
            // Entry-point for TypeScript resolution - must occur first!
            "types": "./types/index.d.ts",

            // Entry-point for `import "my-package"` in ESM
            "import": "./esm/index.js",

            // Entry-point for `require("my-package") in CJS
            "require": "./commonjs/index.cjs",
        },
    },

    // CJS fall-back for older versions of Node.js
    "main": "./commonjs/index.cjs",

    // Fall-back for older versions of TypeScript
    "types": "./types/index.d.ts"
}

x67KawQhC1CZvJRZyFMWhhtjiiiaBHNo7VhkTIMsrRBhy6bzC6kgSfTC42o7YUhM29AA3AGYwWaTl3t34DNjL2pQ5c/+6TVdcMOTU4pvHT4Ht7V4+lHMyVm0lg+SQd5Ukud3oywHbo1L5+sZoUncIiqniwXrnxyL9Q/BUSI2kMKAA5exoblbQvK5IK2JKdhj+Q51BDvuks9M1lNdr4YXZSx18iAG3Ip6H+jenmG0YL0hQ7OFVJG5vg6Fnj69bPbF8Cb8ZJlTaSl0MrvicJRDalMOuutrunag9Bk4lSq9H1xelF1N3GglTru2K9Kfi3QrN9DvWLAX630MYZXVd8uqkfz3BGki0RFiA02zfETgX/9fQr4Jj0p4xWbawsh20S5vqB+s+VYOunekzwKzNqAwqRiyj/qRNRmcEmehpYkre1oFXfK6Ia61s09GOa0zhWhB