Autoplay

    Publish npm packages using npm publish

    Trevor MillerTrevor Miller

    In this lesson we will publish our package. We will first add a prepublish script that runs our build script; this will ensure the built folder gets added to npm when published. We will also add a .npmignore so that only our built files get installed. We need to run npm adduser to log in to an npm account. We can then run npm publish to publish our package and view it on npm.

    Note: if you want to actually publish the package from this course, you'll need to rename it since sensitive-words is already taken. You can use a scoped package name for this as well.

    npmnpm
    Code

    Code

    Become a Member to view code

    You must be a Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson
    Transcript

    Transcript

    00:00 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.

    00:26 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.

    00:47 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.

    01:03 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.

    01:17 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.

    Discuss

    Discuss