简体   繁体   English

如何使用演示应用程序创建npm包?

[英]how to create npm package with a demo app?

It seems good practice for packages to provide some type of demo app, so I'm just wondering what's the cleanest way to organize the file structure? 包提供某种类型的演示应用程序似乎是一种很好的做法,所以我只是想知道组织文件结构的最简洁方法是什么?

I want to have one github repo that contains my published NPM module and a simple demo webapp. 我想要一个包含我发布的NPM模块和一个简单的演示webapp的github存储库。

Ideally I would like a top level like: 理想情况下,我想要一个顶级的:

package/
demo/

and have the code just in package/ get distributed to NPM. 并将代码package/分发到NPM。 I could use the package.json files option like 我可以使用package.json文件选项

files: [ 'package' ]

But then all the code will get distributed with that path prefix, eg 但是,所有代码都将使用该路径前缀进行分发,例如

node_modules/MyPackageName/package/index.js

Is there a way to modify the path prefix so it changes the top-level directory and removes the extra package/ I used to organize the files? 有没有办法修改路径前缀,以便更改顶级目录并删除用于组织文件的额外package/我?

Sure other people have ways to do this, but I'd prefer not to use two repos - one demo and one package. 当然其他人有办法做到这一点,但我宁愿不使用两个回购 - 一个演示和一个包。

Clarification I want to be able to install the package directly from github, as a kind of "poor-mans private NPM". 澄清我希望能够直接从github安装包,作为一种“穷人私人NPM”。 So I don't want to just publish from within the 'package' directory. 所以我不想只是从'package'目录中发布。 I think using github URLs you can specify a branch to use, but not a subdirectory. 我认为使用github URL可以指定要使用的分支,但不能指定子目录。

You can do this with the help of NODE_PATH env variable: 你可以在NODE_PATH env变量的帮助下完成这个:

export NODE_PATH='yourdir'/node_modules

Reference 参考

If the NODE_PATH environment variable is set to a colon-delimited list of absolute paths, then Node.js will search those paths for modules if they are not found elsewhere. 如果将NODE_PATH环境变量设置为以冒号分隔的绝对路径列表,则Node.js将在这些路径中搜索模块(如果在其他位置找不到它们)。

On Windows, NODE_PATH is delimited by semicolons (;) instead of colons. 在Windows上,NODE_PATH由分号(;)而不是冒号分隔。

NODE_PATH was originally created to support loading modules from varying paths before the current module resolution algorithm was frozen. 最初创建NODE_PATH是为了支持在当前模块解析算法被冻结之前从不同路径加载模块。

NODE_PATH is still supported, but is less necessary now that the Node.js ecosystem has settled on a convention for locating dependent modules. NODE_PATH仍然受支持,但由于Node.js生态系统已经确定了用于定位依赖模块的约定,因此不太必要。 Sometimes deployments that rely on NODE_PATH show surprising behavior when people are unaware that NODE_PATH must be set. 当人们不知道必须设置NODE_PATH时,有时依赖于NODE_PATH的部署会出现令人惊讶的行为。 Sometimes a module's dependencies change, causing a different version (or even a different module) to be loaded as the NODE_PATH is searched. 有时模块的依赖关系会发生变化,导致在搜索NODE_PATH时加载不同的版本(甚至是不同的模块)。

Additionally, Node.js will search in the following list of GLOBAL_FOLDERS: 此外,Node.js将搜索以下GLOBAL_FOLDERS列表:

1: $HOME/.node_modules 2: $HOME/.node_libraries 3: $PREFIX/lib/node Where $HOME is the user's home directory, and $PREFIX is Node.js's configured node_prefix. 1:$ HOME / .node_modules 2:$ HOME / .node_libraries 3:$ PREFIX / lib / node其中$ HOME是用户的主目录,$ PREFIX是Node.js配置的node_prefix。

These are mostly for historic reasons. 这些主要是出于历史原因。

It is strongly encouraged to place dependencies in the local node_modules folder. 强烈建议将依赖项放在本地node_modules文件夹中。 These will be loaded faster, and more reliably. 这些将加载更快,更可靠。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM