在`package.json`中,`main`用于CommonJS模块入口,`module`支持ESM标准,`exports`提供多入口点,`import`专注ESM项目。这些字段确保兼容性和灵活性,提升JavaScript包管理效率。
本文介绍了JavaScript中的模块化规范,包括ES6的`export`和Node.js的`exports`与`module.exports`。`export`用于声明可被其他模块访问的内容,而`exports`和`module.exports`用于Node.js模块,二者不可混用。
export exports module.exports 这三个命令中export是是ECMAScript 6(ES6)中引入的模块化规范,exports module.exports是Node.js中用于实现CommonJS模块规范的对象。
TypeScript 5.0 新增 bundler 模块解析策略,解决 Node 不支持 exports、node16/nodenext 强制写扩展名的问题。文章梳理模块解析策略演进(classic、node、node16、nodenext、bundler),重点讲解 package.json 的 main、module、typesVersions 与 exports 字段,包括子路径、通配符、条件导出、优先级及 types 条件。指出 exports 是模块解析最终标准,但社区实现常有优先级 bug,推荐新库使用 bundler 加 exports。
故事要从一次错误的编码开始。。。
Node.js 模块遵循 CommonJS 规范:module.exports 用于导出值,默认是对象,也可覆盖为原始值;require 导入时得到的就是 module.exports。exports 只是 module.exports 的引用,可添加属性,但不能直接赋值覆盖,否则导出失效。
On the heels of our announcement of free static deployments earlier today, we are excited to introduce a beta release of the upcoming Next.js 3.0, featuring next export, dynamic components and...
完成下面两步后,将自动完成登录并继续当前操作。