npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@alexgaiser/publish-npm-tutorial

v0.0.4

Published

A tutorial for how to publish a project to NPM

Downloads

7

Readme

publish-to-npm

Description

This is a step-by-step guide for how to publish your first package to NPM. THe NPM registry is an amazing tool, and every Javascript developer should learn to use it for both their personal and professional projects.

For the sake of this guide I am assuming you already have npm (at least version 6) and node (at least version 14) installed on your computer. If you do not, please see this guide for more informaion on how to install node and npm onto your environment.

Note: while this guide should be platform agnostic, it was written and tested using an Apple Computer running MacOS. If you have an issue with this guide on another platform, please create an issue here and I will make sure to add the necessary information.

If you have any questions or would like more information on this guide, please create an issue here or feel free to message me on LinkedIn

Guide

Setting up our project

First things first we need to set up our project so that we have something to publish. The specifics of how you set up the project are not important except for the following:

  1. You will need a package.json (more details on how this will be set up will be provided below, for now a basic package.json is all that is necessary)
  2. You will need a directory (lib/ in this example) at the root of your project where all business logic related to the application will be stored
  3. You will need an output directory (dist/ in this example) at the root of your project for all your compiled files (the need for this directory will be clear later)
  4. Everything you do not wish to publish to needs to stay outside of your lib/ and dist/ folders. This includes all of your config files, git files, ReadMe and package.json.

This repository is set up to serve as a template for the project structure I have found most convenient.

Setting up an NPM account

Next we will need to create an account and set up our environment so that we can publish to NPM

  1. Go to this page and follow the instructions for creating an account
  2. Make a note of your password because you will need to log in through your command line
  3. Through NPM's website you can see all of your packages and create/manage organizations which allow you to namespace your packages.
  4. After you have set up your project open the terminal at your project's root and log in using the command npm login. You will need to enter your username, password, and the email you used to create the account.
  5. Enter npm whoami into the terminal to verify that you have successfully logged in.

Setting up our package.json

There are several properties of our package.json that we will need to configure in order to be ready to publish our package.

  1. name: this is the name that will be used when we publish our project. It needs to be unique in the npm registry. If you have set up an organization, the convention for naming your package is @organization/package-name.
  2. version: Set this to 0.0.1 to start. As you publish versions you will need to increment this each time. You cannot publish the same version twice.
  3. main: this is the main entry point for your source code. We will point it to dist/cjs.js because this is the entry point we will use when the require keyword for our project is used and we have compiled this version to use CommonJS.
  4. browser: this is the entry point used when the package is loaded into the browser. In our example it should point to dist/index.js.
  5. types: If you have followed this guide and set up your project as a Typescript project, you will need to point to the source of your type definitions so that JS and TS environments can read them. In our example it will point to dist/index.d.ts.
  6. files These are all the files that will be included when your project is installed as a dependency. In our case we want to include dist/, because we have compiled all our logic to this directory. This field is important because it reduces bundle size end helps ensure that no sensitive information is published to the npm registry.
  7. scripts: (Optional) if you do not want to run testing, building or publishing manually, you can create a publish script that will run all of your publishing workflow at once. I personally use the script "publish:prod": "npm run test && npm run ci:lint && npm run build && npm publish --access public" for projects with simple publishing workflows.