Root directory in package.json

前端 未结 7 1664
臣服心动
臣服心动 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:29

    Now this is ugly workaround and it does pollute the root of your package. But until Jordan's answer works, this feels like the way to achieve what you ask.

    Just add a file in the root of your package for each of the modules you want to export using the require with slash notation. Such file will have the same name as the module being exported and it will simply reexport it.

    .
    ├── package.json
    ├── thing.js       <--
    ├── that.js        <--
    ├── src
    |   ├── js
    |   └────── lib
    |   └───────── my
    |   └───────────── thing.js
    |   └───────────── that.js
    

    For example file ./thing.js will contain:

    module.exports = require('./src/js/lib/my/thing');
    

    And so you could require it as:

    const thing = require('mypackage/thing');
    

    Also as stated in the bug about adding mainDir property into package.json you can just temporarily copy your sources and the package.json file into one directory and publish from there.

提交回复
热议问题