如何使用 package.json 中的“main"参数? [英] How to use the 'main' parameter in package.json?

查看:100
本文介绍了如何使用 package.json 中的“main"参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经做了很多搜索.但是,还是对Node项目的package.json中的'main'参数有疑问.

I have done quite some search already. However, still having doubts about the 'main' parameter in the package.json of a Node project.

  1. 填写此字段有何帮助?换一种方式问,如果出现这个字段,我可以以不同的风格启动模块吗?
  2. 我可以在主参数中填充多个脚本吗?如果是,它们会作为两个线程启动吗?如果不是,我如何在一个模块中启动两个脚本并让它们并行运行?

我知道第二个问题很奇怪.这是因为我在 OpenShift 上托管了一个 Node.js 应用程序,但该应用程序由两个主要组件组成.一个是 REST API,一个是通知传送服务.

I know that the second question is quite weird. It is because I have hosted a Node.js application on OpenShift but the application consists of two main components. One being a REST API and one being a notification delivering service.

如果将它们实现为单个线程,我担心通知传递过程会阻塞 REST API.但是,它们必须连接到同一个 MongoDB 盒式磁带.此外,如果可能的话,如果两个组件可以在同一档位中使用,我想节省一个档位.

I am afraid that the notification delivering process would block the REST API if they were implemented as a single thread. However, they have to connect to the same MongoDB cartridge. Moreover, I would like to save one gear if both the components could be serving in the same gear if possible.

欢迎提出任何建议.

推荐答案

来自 npm 文档:

主要字段是一个模块 ID,它是您的主要入口点程序.也就是说,如果您的包名为 foo,并且用户安装了它,然后确实需要(foo"),然后是您的主模块的导出对象将被返回.

The main field is a module ID that is the primary entry point to your program. That is, if your package is named foo, and a user installs it, and then does require("foo"), then your main module's exports object will be returned.

这应该是相对于包根目录的模块 ID文件夹.

This should be a module ID relative to the root of your package folder.

对于大多数模块来说,拥有一个主脚本和通常不会有太多其他事情.

For most modules, it makes the most sense to have a main script and often not much else.

简而言之:

  1. 如果包的入口点与其根目录中的 index.js 不同,则 package.json 中只需要一个 main 参数文件夹.例如,人们经常把入口点放在lib/index.jslib/.js,在这种情况下,对应的脚本必须描述为<package.json 中的 code>main.
  2. 不能有两个脚本作为 main,因为入口点 require('yourpackagename') 必须明确定义.
  1. You only need a main parameter in your package.json if the entry point to your package differs from index.js in its root folder. For example, people often put the entry point to lib/index.js or lib/<packagename>.js, in this case the corresponding script must be described as main in package.json.
  2. You can't have two scripts as main, simply because the entry point require('yourpackagename') must be defined unambiguously.

这篇关于如何使用 package.json 中的“main"参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆