xgqfrms™, xgqfrms® : xgqfrms's offical website of cnblogs! xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

Node.js & npm package.json exports field All In One

Node.js & npm package.json exports field All In One

CJS vs ESM

npm

main

The main field is a module ID that is the primary entry point to your program.
That is, if your package is named foo, and a user installs it, and then does require("foo"), then your main module's exports object will be returned.

This should be a module relative to the root of your package folder.

For most modules, it makes the most sense to have a main script and often not much else.

If main is not set it defaults to index.js in the package's root folder.

https://docs.npmjs.com/cli/v9/configuring-npm/package-json#main

Node.js

image

The "exports" field allows defining the entry points of a package when imported by name loaded either via a node_modules lookup or a self-reference to its own name.
It is supported in Node.js 12+ as an alternative to the "main" that can support defining subpath exports and conditional exports while encapsulating internal unexported modules.

Conditional Exports can also be used within "exports" to define different package entry points per environment, including whether the package is referenced via require or via import.

All paths defined in the "exports" must be relative file URLs starting with ./.

https://nodejs.org/api/packages.html#exports

webpack

https://webpack.js.org/guides/package-exports/

demos

image

{
  "exports": {
    "./common/package.json": "./dist/app/common/package.json",
    "./common/*": {
      "type": "./dist/app/common",
      "node": "./dist/app/common/*.js"
    },
    "./core/package.json": "./dist/app/core/package.json",
    "./core/*": {
      "type": "./dist/app/core",
      "node": "./dist/app/core/*.js"
    },
    "./infra/package.json": "./dist/app/infra/package.json",
    "./infra/*": {
      "type": "./dist/app/infra",
      "node": "./dist/app/infra/*.js"
    },
    "./port/package.json": "./dist/app/port/package.json",
    "./port/*": {
      "type": "./dist/app/port",
      "node": "./dist/app/port/*.js"
    },
    "./repository/package.json": "./dist/app/repository/package.json",
    "./repository/*": {
      "type": "./dist/app/repository",
      "node": "./dist/app/repository/*.js"
    }
  },
}

https://github.com/cnpm/cnpmcore/blob/master/package.json#LL10C8-L10C8

refs



©xgqfrms 2012-2021

www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!

原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!


posted @   xgqfrms  阅读(45)  评论(2编辑  收藏  举报
相关博文:
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
历史上的今天:
2022-03-21 window.atob lost spaces bug All In One
2022-03-21 Vue 3 API All In One
2022-03-21 Vue 3 Migration Guide All In One
2019-03-21 input & collapse & tags
2019-03-21 MBP & Power battery lost
2019-03-21 Front-end Job Interview Questions
点击右上角即可分享
微信分享提示