javascript - How to set relative path resolution based on 'dist' folder -
when creating npm package, quite common , convenient structure so:
root / dist / src / package.json
where package.json
:
{ "main": "dist/index.js" "files": [ "dist" ] }
the drawback of approach is, when consumer wants require files using relative path, needs include dist
folder. e.g. const abc = require('my-package/dist/x/y/abc');
is there way tell nodejs resolve relative path based on path.dirname(main)
or similar?
update: clarify, related relative/deep resolution, not export hoisting in es6. controversial subject should done @ all, consumer coupled internal folder structure of package.
update 2: want achieve concept of "sub-module" (similar namespacing). example, folder structure looks this:
root / dist / testutil.js / index.js
testutil.js
contains useful functions testing. since not used in normal use, don't want export them @ top-level. i.e., instead of:
// index.js export * './testutil'
i do:
// index.js import * testutil './testutil' export { testutil }
however, still expose testutil
module namespace @ top-level , hard use:
// consuming.js import { testutil } 'my-package' const { funca, funcb } = testutil
it better if can "tug" under relative path:
// consuming.js import { funca, funcb } 'my-package/testutil'
currently, without solving 'dist' issue, have instead:
// consuming.js import { funca, funcb } 'my-package/dist/testutil'
you can set node_path
environment variable, , should trick:
export node_path=./dist
there other patterns try out here.
Comments
Post a Comment