When you start building an app it is very likely you will have some requirement and you can't find any existing NPM package. If you have to write the code anyway... why not built it within a module, publish and share it with the world? Sound good, read on?
Create a new GIT Repo and Login To NPM
In Github create a new repository and clone it locally.
Open a new terminal and ensure the current path is the same as your repo. In the terminal, register your details like so:
Doing this will save your details within a file called ~/.npmrc. Doing this means you won’t have to re-enter them each time you create a new NPM package. Next, log into NPM, using this command:
Create Your NPM Package
Next, you need to create a new NPM package. Anyone who has been using JS should be familiar with this process. Type, add in the project name, version, license etc..:
Next, we will create a very simple function that returns a string 'helloworld', using this code within index.js:
Update the README.md
Commit and push your code to GitHub
Next, commit your code and push it:
Publish Your NPM Package
After committing you can push your package to NPM so other people can access it:
Log into https://www.npmjs.com and you should now be able to see your module. Congrats! You’ve just published your first Node module.
Test out your module
Create a new folder on your PC. Run an npm unit to create a new app, adding in all your details. Next, import your package using:
That's it! If everything has gone correctly, you have now published and consumed your first NPM package. After you have published and updated a few times the process is fairly pain-free. Also, remember when you update your package, to up the version number! If you want to see my first NPM package, you can see it over here: https://github.com/jondjones/https-status-lookup