Published on
·
Time to read
2 minute read

One NPM Package, Many Exports

Blog post image
Authors

Back in March when I covered slimming down your lodash bundle, a discerning reader might have taken a look at the lodash source, saw that the root directory is not littered with individual files for each function, and questioned how the require('lodash/get') works. At the time, I waved my hands over the magic that made this possible, but sick of my own sad require('my-package/src/lib/dist/out/my-volatile-filename.js') statements, I've condensed that same lodash magic to the generate-export-aliases package and am now sharing it with the eagerly waiting world.

If we inspect the node_modules/lodash folder, we see that the published module contains a file per function that references shared libraries that are generated by a prepublish build step. All we really need to do then to make the function in src/lib/dist/out/my-volatile-filename.js available at a more stable location like my-package/foobar is to create a file foobar.js in the root of the project that exports the same module! This is exactly what generate-export-aliases does and only needs a few lines in your package.json to get started.

1. Install

npm install --save-dev generate-export-aliases

2. Configure

{
  "name": "my-package",
  "scripts": {
    "prepublish": "generate-export-aliases"
  },
  "config": {
    "exportAliases": {
      "foobar": "./src/lib/dist/out/my-volatile-filename.js"
    }
  }
}

3. Profit

const foobar = require('my-package/foobar')