What is NPM Package ?
working with javascript/typescript; sometime we create some utility methods or useful code snippet which we copy in our several projects, here some npm package handy.
in this post we will create a very basic npm package and test and publish it, earlier it was in javascript but later added typescript.
Pre-requisite
- create an account on npmjs website, ite free.
- enable 2FA so it would be comfortable while publishing the package.
- a local project which is based on npm and with typescript support, where you can test and verify before publish the package.
- a IDE ( preferably, VS code editor)
- Typescript (
npm i typescript -g
will add it globally)
lets’ start
- create new folder ( in my case, its npm/demo ) and navigate it from terminal and run
- this will ask few questions and when finished , it will generate a package.json file in your folder,
alternatively you can run
npm init --yes
also
Tip: create a scoped package (i.e. name starts with @
) so that will be unique,
added few necessary scripts, you can copy it from below
package.json
here important properties are files, name and main
- files will contain files, which will be downloaded when someone do
npm install your package
- name is the name of package which is used to indicate when someone do npm install, in my case here it will be
npm install @xkeshav/day
- we are using typescript so main and types will be there and there file location will be from lib folder
- create tsconfig.json file
it will generate default tsconfig.json, you can remove many of rules. below is my tsconfig.json file content
tsconfig.json
Note: here outDir
will be same as files
in package.json
- now create src/index.ts file and add your own code which you want to include in the package, use
export
keyword; below is the content of file
here in my package , named export a constant.
- build the package before publish and check the code in lib folder
How to test locally?
before publish , if you want to verify whether our package will work as expected or not then we can test with 2 approach
npm link way
after the build, run below command on same folder
now go to another project (which is npm based and typescript supported ) and run below
and now in any typescript file write
you are good to check. if you find any issue in your package; then run
and do changes in your main package and again run npm link
and follow the same.
npm pack approach
go to your project where custom npm package is, and run
which will output looks like this
after executing this command successfully, a tar.gz
file will be created the same folder,
copy this file and paste it in another project you want to use it.
now navigate to the another project root in terminal and run
above command will add entry of the package in the package.json file as below
and now you can use the same way
if something went wrong while building the package, then do npm uninstall xkeshav-day-1.3.0.tgz
and fix the issue in the package and repeat the same process to install again.
Publish
now once you verified your package locally, its time to publish, for that
make sure you are logged in to your npm account prior to run below command
now go to your npm package project root and run
this command will pause on one place and asked to Enter and when you hit Enter, it will open npm OTP page in the browser and that page will ask to enter OTP ( if 2FA enabled ) and then after few seconds it will publish the package and you will the information on terminal
alternatively you can add this command in package.json script ( shown in earlier code snippet )and run npm run publish
Update the version of package
if you want to update your package then
- either update the version value in the package.json
or run
which automatically update version;
side note: minor
will update second number by +1; patch
will increase the third place number by +1
and major
will update first place number one by + 1
where to see package on npm
- login to npmjs.com you can see all your packages under your profile > packages in npm website.
Thanks.