Publish
Publishing your npm package to the npm registry involves a few straightforward steps. Additionally, you can test your package locally before publishing using npm link
. Follow the guide below to get your package live:
Prepare Your Package
Before publishing, ensure your package is ready:
-
Build Your Package: Compile your TypeScript files and generate any necessary type declarations.
npm run build
-
Verify
package.json
: Ensure yourpackage.json
file has all necessary information, including:name
: Your package name.version
: The current version of your package.description
: A short description of what your package does.repository.url
: The URL of your project's repository.homepage
: The URL for your package's homepage.bugs.url
: The URL where users can report issues.
Test Locally with npm link
Before publishing, it's a good idea to test your package in a real project environment. Here's how you can do that using npm link
:
Link Your Package Globally
In the root directory of your package, run:
npm link
This command creates a global symlink to your package, allowing you to use it in other projects as if it were installed from npm.
Link Your Package in a Test Project
Navigate to a test project where you want to try out your package:
cd /path/to/your/test/project
Then link the globally installed package:
npm link your-package-name
Replace your-package-name
with the name specified in your package.json
. This command links your package to the test project, allowing you to import and use it as if it were a normal npm package.
Test Your Package
Now, you can import your package in the test project and test it as you normally would:
import { exampleFn, YourComponent, } from 'your-package-name';
// Use exampleFn, YourComponent in your test project
Unlink After Testing
After testing, you can remove the symlink from your test project:
npm unlink your-package-name
And if you want to remove the global link:
npm unlink -g your-package-name
Login to npm
If you're not already logged in to npm, authenticate with your npm credentials:
npm login
You will be prompted to enter your npm username, password, and email address. This step ensures that you have the correct permissions to publish packages.
Update Your Package Version
For subsequent updates to your package, you need to update the version number in your package.json
file. Use npm's versioning commands to automate this:
-
Patch Version: For minor updates or bug fixes, increment the patch version.
npm version patch
This command updates the patch version in
package.json
, commits the change, and tags the new version. -
Minor Version: For adding new features that are backwards-compatible, increment the minor version.
npm version minor
-
Major Version: For breaking changes, increment the major version.
npm version major
Rebuild Your Package
After updating the version number:
-
Rebuild Your Package:
npm run build
Publish Your Package
To publish your package to the npm registry, run:
npm publish
This command uploads your package to npm. If it's the first time you're publishing the package, it will be available for others to install. For updated versions, the existing package will be replaced with the new version.
By following these steps, your package will be published and available for other developers to use in their projects. Happy coding! 🚀