问题
Is there a package.json
property that can be used to specify the root folder that module resolution should start?
For example suppose we have an install in node_modules/mypackage/src/file1
. And all the files we want to import start under the src
directory. Can we specify something like:
{
root: ./src/
}
And then require('mypackage/file1');
Thoughts?
回答1:
ECMAScript modules support an experimental exports property in package.json. You can try something like:
{
"exports": {
"./": "./src/"
}
}
Then imports like:
import file1Module from 'mypackage/file1'
Should load from ./node_modules/mypackage/src/file1.js
. If its not possible to alias the root you would have to do:
{
"exports": {
"./file1": "./src/file1.js"
}
}
For every module or directory you wanted to alias.
As noted on your GitHub issue.
来源:https://stackoverflow.com/questions/51313753/npm-package-json-base-root-property