Create and Publish NPM Package
Steps
- Create a github repository for your package and clone to your local machine
- Go to the repository and create package.json file with run
npm init
and set your options whatever you want - Create js file for your package
- Run
npm link
when on your package folder.npm link : First, npm link in a package folder will create a symlink in the global folder {prefix}/lib/node_modules/the-package that links to the package where the npm link command was executed. It will also link any bins in the package to {prefix}/bin/{name}. Note that npm link uses the global prefix (see npm prefix -g for its value). Next, in some other location, npm link package-name will create a symbolic link from globally-installed package-name to node_modules/ of the current folder. Note that package-name is taken from package.json, not from the directory name.
- Create test directory in a different directory than your package directory
- Go to the test directory and run
npm link <your-package-name>
to install your package on the test folder and make your tests - Go back to your package folder to publish the package
- Run
npm login
if you not already logged in npm - Run
npm publish
to publish your package
Publish Scoped Package
You may want to publish packages associated with your username or other custom group names. While there can be many reasons for this, the most basic reason is that the package name you want to create already exists.
- Run
npm init --scope=your-scope-name
. It will seem like this on package.json name option :@your-scope-name/your-package-name
- Or create package.json file with
npm init
and go to the file to change your package name like this@zumrudu-anka/is-zumrudu-anka
. - Scoped packages are published private by default. But private use requires a licensed account. Run
npm publish --access=public
command to publish scoped package public