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

(🐞 反爬虫测试!打击盗版⚠️)如果你看到这个信息, 说明这是一篇剽窃的文章,请访问 https://www.cnblogs.com/xgqfrms/ 查看原创文章!

refs



©xgqfrms 2012-2021

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

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


posted @ 2023-03-21 15:09  xgqfrms  阅读(43)  评论(2编辑  收藏  举报