Javascript package.json 中的根目录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/30302747/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Root directory in package.json
提问by Drahcir
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 system.
我的问题涉及我希望作为 NPM 模块发布的现有库。该库已在使用中,目前require通过本地文件系统 d。
How can I specify the root directory of my module's files?
如何指定模块文件的根目录?
If I have a structure like:
如果我有这样的结构:
.
├── package.json
├── src
|   ├── js
|   └────── lib
|   └───────── my
|   └───────────── thing.js
|   └───────────── that.js
How do I specify that the root of my module, and accessible files is src/js/lib/my/?
如何指定我的模块和可访问文件的根目录src/js/lib/my/?
I would like to use as follows from an outside project:
我想从外部项目中使用以下内容:
var thing = require('my/thing'),
    that = require('my/that');
I saw the "files"property in package.json, is this the right way to go?
我"files"在 package.json 中看到了属性,这是正确的方法吗?
回答by Jordan Whitfield
UPDATE 2020
2020 年更新
The issue suggesting mainDir is now closed. Instead there is a new field called exportswhich can be used almost like es import maps to map a folder to an export alias:
提示 mainDir 的问题现已关闭。相反,有一个名为的新字段exports,可以像使用 es 导入映射一样将文件夹映射到导出别名:
// ./node_modules/es-module-package/package.json
{
  "exports": {
    "./my/": "./src/js/lib/my/"
  }
}
import thing from 'es-module-package/my/thing.js';
// Loads ./node_modules/es-module-package/src/js/lib/my/thing.js
As suggestedin the issue linked in the original answer below it may be possible to map the root to a folder to access import thing from pkg/thing.jsas so:
正如下面原始答案中链接的问题中所建议的那样,可以将根映射到文件夹以进行访问import thing from pkg/thing.js:
{
  "type": "module",
  "main": "./dist/index.js",
  "exports": {
    "./": "./src/js/lib/my/"
  }
}
Original Answer
原答案
For a native solution, see this node issue https://github.com/nodejs/node/issues/14970
对于本机解决方案,请参阅此节点问题https://github.com/nodejs/node/issues/14970
The feature request suggests a mainDirfield in the package.json next to main.
功能请求建议mainDirpackage.json 中 main 旁边的一个字段。
The more people that vote, the faster/more likely it will be implemented
投票的人越多,实施的速度就越快/更有可能
回答by Yann Bertrand
As the docsays:
正如文档所说:
The main field is a module ID that is the primary entry point to your program.
main 字段是一个模块 ID,它是程序的主要入口点。
So you'll have something like "main": "src/js/lib/my/app.js"in your package.json file.
所以你"main": "src/js/lib/my/app.js"的 package.json 文件中会有类似的内容。
I would suggest you to create an app.jsfile and module.exportsyour different children. For example:
我建议你创建一个app.js文件和module.exports你不同的孩子。例如:
 module.exports.thing = require('./thing');
 module.exports.that = require('./that');
And use them like this:
并像这样使用它们:
var mylib = require('mylib')
  , thing = mylib.thing
  , that = mylib.that;
回答by kub1x
Now this is ugly workaround and it does pollute the root of your package. But until Jordan's answerworks, 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 slashnotation. Such file will have the same name as the module being exported and it will simply reexport it.
只需使用带斜杠符号的require 为要导出的每个模块在包的根目录中添加一个文件。此类文件将与要导出的模块具有相同的名称,并且只会重新导出它。
.
├── package.json
├── thing.js       <--
├── that.js        <--
├── src
|   ├── js
|   └────── lib
|   └───────── my
|   └───────────── thing.js
|   └───────────── that.js
For example file ./thing.jswill contain: 
例如文件./thing.js将包含:
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 mainDirproperty into package.jsonyou can just temporarily copy your sources and the package.json file into one directory and publish from there.
同样如关于向mainDirpackage.json您添加属性的错误所述,您可以将源代码和 package.json 文件临时复制到一个目录中,然后从那里发布。
回答by Morgan
Another possibility is to use ECMAScript modules (ES modules), particularly the package exportsfield in your package.jsonfile.
另一种可能性是使用 ECMAScript 模块(ES 模块),特别是package.json文件中的包导出字段。
Given a package.jsonfile with this config:
给定具有此配置的package.json文件:
{
  "name": "my",
  "exports": {
    "./": "./src/js/lib/my/"
  }
}
You should be able to import modules from the library like:
您应该能够从库中导入模块,例如:
import thing from 'my/thing'
import that from 'my/that'
This is enabled by default since node 13.0.0, but was behind the --experimental-exportsflag from 12.13.0.
自 node 起默认启用此功能13.0.0,但位于--experimental-exportsfrom的标志后面12.13.0。
Note, that the ES Module spec is in the Stability:1 - Experimentalstage and subject to change. I have no idea the extent to which this might be compatible with CommonJS modules.
请注意,ES 模块规范处于稳定性:1 - 实验阶段,可能会发生变化。我不知道这在多大程度上与 CommonJS 模块兼容。
回答by Jerome WAGNER
package.jsonis mainly a file used by npmto install and manage dependencies.
package.json主要是用于npm安装和管理依赖项的文件。
the requireconstruct does not care a lot about package.jsonso you will not be able to use it to subvert the way requireworks and make it believe that packages are not where the requireloading scheme expects them.
该require构造并不关心太多,package.json因此您将无法使用它来破坏require工作方式并使其相信包不在require加载方案所期望的位置。
See the documentation on https://nodejs.org/api/modules.htmland the loading scheme here: https://nodejs.org/api/modules.html#modules_all_together
请参阅https://nodejs.org/api/modules.html上的文档和此处的加载方案:https: //nodejs.org/api/modules.html#modules_all_together
you could maybe use the technique that the documentation calls 'Loading from the global folders' and define the NODE_PATHenvironment variable.
您可以使用文档中称为“从全局文件夹加载”的技术并定义NODE_PATH环境变量。
but I advise you to stick to a more standard way : - put your modules in a node_modules directory - or start your module hierarchy in the same directory where your app.js or index.js is located
但我建议您坚持使用更标准的方式: - 将模块放在 node_modules 目录中 - 或者在 app.js 或 index.js 所在的同一目录中启动模块层次结构
回答by u9075221
In webpack, you can specify resolve.aliaslike this:
在 webpack 中,你可以这样指定resolve.alias:
{
  resolve: {
    alias: {
      'my': 'my/src'
    }
  }
}
or you can specify directionsoption in package.json
或者你可以directions在 package.json 中指定选项
{
  directions: {
    'lib': 'src/lib'
  }
}

