how to create npm package with a demo app?

时间:2018-06-05 05:02:52

标签: javascript npm package-development

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.

Ideally I would like a top level like:

package/
demo/

and have the code just in package/ get distributed to NPM. I could use the package.json files option like

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?

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". So I don't want to just publish from within the 'package' directory. I think using github URLs you can specify a branch to use, but not a subdirectory.

1 个答案:

答案 0 :(得分:1)

您可以在NODE_PATH env变量的帮助下完成此操作:

export NODE_PATH='yourdir'/node_modules

Reference

  

如果NODE_PATH环境变量设置为以冒号分隔的绝对路径列表,那么Node.js将搜索这些路径中的模块(如果在其他位置找不到它们)。

     

在Windows上,NODE_PATH由分号(;)而不是冒号分隔。

     

最初创建NODE_PATH是为了支持在当前模块解析算法被冻结之前从不同路径加载模块。

     

仍然支持NODE_PATH,但由于Node.js生态系统已经确定了用于定位依赖模块的约定,因此不太必要。当人们不知道必须设置NODE_PATH时,有时依赖于NODE_PATH的部署会出现令人惊讶的行为。有时模块的依赖关系会发生变化,导致在搜索NODE_PATH时加载不同的版本(甚至是不同的模块)。

     

此外,Node.js将搜索以下GLOBAL_FOLDERS列表:

     

1:$ HOME / .node_modules   2:$ HOME / .node_libraries   3:$ PREFIX / lib / node   其中$ HOME是用户的主目录,$ PREFIX是Node.js配置的node_prefix。

     

这些主要是出于历史原因。

     

强烈建议将依赖项放在本地node_modules文件夹中。这些将加载更快,更可靠。