How to publish a package to npm?
There are a few steps to publish a package to npm:
- Make sure the package code is in a Git repository
- Create an account on npmjs.com if you don’t already have one
- From the package directory, run
npm init
to initialize npm configuration - Update
package.json
with metadata like name, version, description etc - Run
npm publish
to publish the package to npm - Use
npm version patch/minor/major
to bump version before re-publishing updates
What is the value of Lerna in managing monorepos?
Lerna provides commands to:
- Manage dependencies between packages in a monorepo
- Publish packages independently with a single command
- Create/remove packages within a monorepo
- Run scripts on all packages like test, build etc
So it simplifies publishing and versioning of packages in a monorepo.
How does yarn workspace help in managing dependencies in Lerna?
Yarn workspace manages dependencies intelligently across all packages in a monorepo. It:
- Installs all dependencies in a single root node_modules folder
- Allows cross-dependency linking between packages
- Avoids duplication of dependencies across packages
This avoids the need for Lerna’s --hoist
flag and improves dependency management.
How to build UMD packages for publishing?
To build UMD packages for publishing to npm:
- Set
module
andmain
fields in package.json pointing to ES module and CommonJS builds - Add
unpkg
field pointing to UMD build - Build UMD version with webpack adding UMD library export
- Output UMD build to
dist/index.umd.js
The UMD build can then be consumed via script tag in browsers.
How does Lerna versioning and publishing work?
lerna version
bumps version based on semver keywords in commit messagelerna publish
publishes changed packages to npmlerna publish from-package
publishes only specified package- Use
--independent
flag for independent versioning
Lerna handles versioning and publishing of multiple packages in a monorepo.
How can Bit help in managing and publishing components?
Bit allows:
- Isolating components into re-usable units with encapsulated dependencies
- Organizing components into Scopes with search and versioning
- Publishing components to Bit’s npm registry
- Importing components into projects from Bit registry
Bit provides an alternative lightweight approach to monorepos for component management.
References
ref: