DEV Community

Ernesto Bellei
Ernesto Bellei

Posted on

MyNPM

Code πŸ“„: https://github.com/hund-studio/mynpm

As projects grow, being able to rely on reusable packages β€” and why not, local ones β€” can become very convenient.
MyNPM makes managing local packages easy in 2 steps:

  1. Add the paths containing your packages;
  2. Install your local packages as a priority, and then, if necessary, fallback to public ones.

An example: When I work on a UI package, I always use a CSS helper function that helps me manage different conditional classes:

const css = (...classNames: (string \| number \| boolean \| null \| undefined)[]) =>
  classNames.flatMap((i) => (!!i ? [i] : [])).join(" ");

export default css;
Enter fullscreen mode Exit fullscreen mode

It's just two lines of code, but repeating it in every project is a waste of time.
With MyNPM, I easily created a folder:

mkdir /home/ernesto/@packages/npm
Enter fullscreen mode Exit fullscreen mode

I added it to my MyNPM sources:

mynpm add /home/ernesto/@packages/npm
Enter fullscreen mode Exit fullscreen mode

And now I can install it in 2 ways:

  • Directly as a file: mynpm install css;
  • As a local package via the local package server: npm i css.

What’s the difference with a local package?

  • I can install packages from paths with unusual names or special characters;
  • The server will always provide the package updated with local changes, so I can test the latest code without bumping the version;
  • I can organize my packages under custom namespaces.

Installation

Currently, installation is quite basic:

  1. Clone the repository;
  2. Enter the package directory;
  3. Run npm i -g --force (the --force flag allows updating older versions that are already installed)

After that, the mynpm command is available.

Commands

Command Description
mynpm add <pathname> Adds a source to the local NPM server
mynpm start Starts the local NPM server
mynpm startup Configures automatic startup
mynpm help Shows the available commands
mynpm install Installs a package (tries to install from path first, then locally, then from the public registry)

Contributing

Feel free to contribute! I acknowledge this is a very rough tool, but I add features as I need them.

Top comments (0)