EuberDeveloper
EuberDeveloper

Reputation: 1048

npm module - Is splitting bin and lib dependencies possible?

Let's say I've already published a nodejs module on npm. It's simple, you install and import it, and given a string and a config object it returns a string.

Now, I wanted to make it available both as local and global module: I added a script to bin in package.json. The script imports and uses the local function and prints the result. To handle the argument and the config object I used npm yarg as a dependency.

The problem is this: if I want to install my module as local, the yarg dependency is not needed, because it is used only when the module is used as global. So it's a waste of space.

Is there a way to make the yarg dependency be installed only when my module is installed as global module and not local?

Upvotes: 0

Views: 218

Answers (1)

Estus Flask
Estus Flask

Reputation: 223259

This is not a common practice by any means. There can be scenarios when a package installed locally can be usable as CLI executable, notably as a part of NPM script. All popular packages that are supposed to be used globally like npm or grunt can also be installed and run locally.

There is no reliable way to detect that a package was installed globally. This can be potentially done by detecting that a module resides in node_modules global location. There may be caveats that depend on specific system.

The footprint of yargs is 760 Kb, which is not crucial. If the footprint of CLI-specific functionality is considerable, CLI executable can be extracted to separate package that depends on original package.

Upvotes: 1

Related Questions