Create npm package



A typical situation is that a certain module exists in the project. The module develops, becomes an independent element and is copied to another project. Then another project. And further.
In each project, the module is updated and improved, and, at some point, it becomes unclear where the current version is. C'mon, "it’s not clear where the current version is!" It is possible that in each project the module will have its own modifications, which will be quite difficult to put in order for use in the next project.
It is quite possible to limit yourself to creating a separate repository for the module, for example on GitHub or Bitbucket. But adding a repository as a submodule for GIT is an adventure .
But you can go further and create an npm package based on such a module. Working with such packages is very simple. You can install the npm package with the command npm install, and update with npm update.


Package creation


Creating npm packages is not at all difficult. Below I will briefly describe the issues that I encountered during the publication of my first module.

File structure

The file structure can be completely different. It is reasonable to use the grunt-init and grunt-init-node modules to create the FS .

Repository on GitHub or Bitbucket

It is logical (but not necessary) to create a repository on GitHub or Bitbucket.
Pros:
  • A bug tracker for storing errors that users encountered, and possibly their solutions
  • You can get a useful pull request

Package.json

In order to publish the module, you need to correctly fill in package.json. How to do this is described in detail in the documentation for npm , or more interactively here .

Publish to npm

In order to publish the package - you need to log in to npm. If you have not registered yet:
npm adduser

After that, you will need to enter a username, password and email. After registration, npm will authorize you under the created account.
In case you have already been registered in npm before - you need to log in,
npm login

Now you can publish the package by typing in the console:
npm publish

After the package is published, it can be installed using the command
npm install

To remove a package from npm, there is a command:
npm unpublish [@]


PS


Of course, the creation of npm packages is not a panacea, and it is likely that this is not suitable for everyone, but I would call it “another way to modularity”.

Also popular now: