@samayer12/npm-package-demo
v0.0.9
Published
## Objectives
Downloads
41
Readme
Package Publishing Fundamentals I
Objectives
- Examine a basic shared component.
- Practice consuming specified versions of an existing component.
- Outline how to publish a component to the
npmpackage registry.
Motivation
Package registries streamline software development by serving as a one-stop-shop for libraries. They simplify dependency management, enabling developers to easily share code.
While you are not expected to maintain your own packages, this lesson provides an overview for how package managers like npm work.
This lesson can serve as a starting point for a discussion about creating a shared component library or other common code used in your organization.
Notes
We will use @samayer12/npm-package-demo for this exercise.
Instructions
There are three parts to this exercise:
- Examining the implementation of a basic shared component.
- Consuming different versions of that component in a separate project.
- Outlining how to publish to a package registry.
Examine the basic shared component
- Run
git clone https://github.com/samayer12/npm-package-consumer. - Find references to
ImportedComponentin the filesrc/App.jsx. - Run
npm listto see the dependencies. Observe that@samayer12/[email protected]is in the output. - Run
npm run devand open a browser to the URL specified in your terminal. - Observe the text "Imported Component" on the screen.
Consume a different version of an imported component
- Modify
package.jsonto use a different version of@samayer12/npm-package-demo(e.g.,0.0.8). - Run
npm install. - Restart the application.
- Observe that the
<ImportedComponent>renders differently.
Outline of publishing a component to a package registry
Given an account with your package registry, regardless of if it's self-hosted or public, there are some key concepts to publish an npm package:
- Configure
package.json. This file contains project metadata and information about its dependencies. There are three key fields:name: The name of your package, unique on the npm registry.version: The version number of your package following Semantic Versioning.main: This field points to the entry point of your package, typicallyindex.js.
- Configure
.npmrc. To avoid entering your credentials when you publish a package, store your authentication token in the.npmrcfile.npmautomatically does this when you runnpm login. Read the docs for more details.- NOTE: Since the authentication token is a secret value, do NOT store it in version control.
- Publish to the package registry.
- Create an account with the package registry.
- Run
npm loginin your terminal. - When you have a version of your package ready for release, run
npm publish.- NOTE: Releasing a package involves updating the
versionfield inpackage.jsonand should be part of an automated process that uses the project'sgithistory.
- NOTE: Releasing a package involves updating the
Troubleshooting
Common errors during the npm publish process include:
- Authentication Issues:
If you encounter authentication errors during
npm publishprocess, ensure that your credentials are correctly set up. Double-check the.npmrcfile and runnpm loginto reauthenticate if necessary. - Versioning Conflicts:
In case of versioning conflicts or unexpected behavior after updating dependencies, review your
package.jsonfile to verify the compatibility of versions. Ensure that you follow SemVer principles when updating versions. - Registry Connection Problems:
Check your network settings and ensure that
npmis configured to use the correct registry URL. You can adjust registry settings in the.npmrcfile.
Further Reading
- .npmrc docs
- npm login docs
- Creating a simple node.js module
- Updating packages downloaded from the npm registry
- Semantic Release
Summary
This lesson showed you how to consume different versions of a project dependency by modifying package.json.
By using different versions of shared code, you gained different functionality or appearance changes without much effort.
Finally, you were provided with an outline of how to publish your own software packages. You were also provided with detailed resources in case you want to dig deeper.
