how to create npm package with a demo app?

545 views Asked by At

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

There are 1 answers

2
Harshal Yeole On

You can do this with the help of NODE_PATH env variable:

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.

On Windows, NODE_PATH is delimited by semicolons (;) instead of colons.

NODE_PATH was originally created to support loading modules from varying paths before the current module resolution algorithm was frozen.

NODE_PATH is still supported, but is less necessary now that the Node.js ecosystem has settled on a convention for locating dependent modules. Sometimes deployments that rely on NODE_PATH show surprising behavior when people are unaware that NODE_PATH must be set. Sometimes a module's dependencies change, causing a different version (or even a different module) to be loaded as the NODE_PATH is searched.

Additionally, Node.js will search in the following list of 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.

These are mostly for historic reasons.

It is strongly encouraged to place dependencies in the local node_modules folder. These will be loaded faster, and more reliably.