@alexgaiser/publish-npm-tutorial
v0.0.4
Published
A tutorial for how to publish a project to NPM
Downloads
7
Maintainers
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:
- 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)
- 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 - 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) - Everything you do not wish to publish to needs to stay outside of your
lib/
anddist/
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
- Go to this page and follow the instructions for creating an account
- Make a note of your password because you will need to log in through your command line
- Through NPM's website you can see all of your packages and create/manage organizations which allow you to namespace your packages.
- 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. - 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.
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
.version
: Set this to0.0.1
to start. As you publish versions you will need to increment this each time. You cannot publish the same version twice.main
: this is the main entry point for your source code. We will point it todist/cjs.js
because this is the entry point we will use when therequire
keyword for our project is used and we have compiled this version to use CommonJS.browser
: this is the entry point used when the package is loaded into the browser. In our example it should point todist/index.js
.types
: If you have followed this guide and set up your project as aTypescript
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 todist/index.d.ts
.files
These are all the files that will be included when your project is installed as a dependency. In our case we want to includedist/
, 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.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.