Let's open our package.json in our code editor and inside of our scripts, let's add a new script called pre-publish. Let's have this run npm run build. We need to make sure we add a comma above. Now what this will do is whenever we run the npm publish command it says, "before that's run, run this," which npm run build will run our build script.
This command is going to ensure that when we publish a package it has our latest build. Next, let's create an npm ignore file. Inside that file let's add the source directory. This tells npm to install everything except for what's listed in the gitignore and the npm ignore.
If we didn't have this source here, every time someone npm-installed our package in their node modules inside of their project they would also get all of our source files. Now let's save and close this file and go back to our terminal. Now let's run npm-adduser.
We type in our username and our password, our email. Now it says that we've been logged in to the npm registry. Now we can run npm publish. We can see that our build script was run and we can open up npm and view our package.
It shows our package name, description from our package.json as well as the example from our readme document. Now other programmers can run this command and use our package.