npm is like a giant library of code building blocks. There are tons of these “blocks”, aka “packages” that we can compose together to build cool things. We can use packages by other developers, but we can also write and publish our own.
We can create a package.json automatically by running
npm init and answering the questions it gives. This becomes the “manifest” file for our package; it is used to populate the published npm page as well as to help the package manager to know how to install the package in consuming projects.
babel-preset-latest and wire up a
Adding tests to our package can ensure it works as expected. We will install a test framework
jest. We will then create a
test script to run our tests. Then we will create a
test:watch script to keep tests running while we develop.
We will run our
test:watch scripts to develop; we will create some tests for the expected functionality of our package; it should conceal any words in a string that are in a blacklist of words. Then we will write the logic to make those tests pass.
We will import our newly published package into a new project locally to make sure everything is working as expected. We can do this locally before publishing with
npm link. This creates a symbolic link in our
node_modules folder, so our unpublished local package is used like an installed published package. This is important because it lets us test making changes to our package and using them immediately without publishing and updating a package with each change we want to test. This is good practice to do before publishing a new version of a package.
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.
When we want to update our package we need to do a few things: pull latest from our git remote, bump the npm version and git tag, push to our remote, push tags to our remote, and then run
npm publish. Phew, that’s a lot. In this lesson, we will set up a
release script using the
np package to simplify this process.