NPM package.json base / root property?

蓝咒 提交于 2019-12-11 15:24:51

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!