Root directory in package.json

前端 未结 7 1679
臣服心动
臣服心动 2020-12-25 10:25

My question concerns an existing library that I wish to publish as an NPM module. The library is already in use, and currently required via the local file syste

7条回答
  •  无人及你
    2020-12-25 11:23

    UPDATE 2020

    The issue suggesting mainDir is now closed. Instead there is a new field called exports which can be used almost like es import maps to map a folder to an export alias:

    // ./node_modules/es-module-package/package.json
    {
      "exports": {
        "./my/": "./src/js/lib/my/"
      }
    }
    
    import thing from 'es-module-package/my/thing.js';
    // Loads ./node_modules/es-module-package/src/js/lib/my/thing.js
    

    As suggested in the issue linked in the original answer below it may be possible to map the root to a folder to access import thing from pkg/thing.js as so:

    {
      "type": "module",
      "main": "./dist/index.js",
      "exports": {
        "./": "./src/js/lib/my/"
      }
    }
    

    Original Answer

    For a native solution, see this node issue https://github.com/nodejs/node/issues/14970

    The feature request suggests a mainDir field in the package.json next to main.

    The more people that vote, the faster/more likely it will be implemented

提交回复
热议问题