How to build, test and release a node module in ES6

Posted on

Over the last couple of weeks I’ve had incredible fun building the random code generator live on Twitch. At the time of writing this post, I’ve also merged over 70 pull requests into the repository for the application on GitHub. This got me thinking — what if people could use the same silly random code on their own applications to create even more lols on the web?

I had never actually built or released a node module before @whitep4nth3r/random-code, and this blog post by Alec Mather was super helpful in understanding the concepts. The most important thing I learned was that if we want to write the code for the node module in ES6, we need a way to transpile the code from ES6 to ES5 so that it’s compatible with anyone’s code.

I don’t intend to recreate the tutorial in this post, but instead, write out the process for future me. I can see myself harnessing the power of building and releasing node modules much more as time goes on!

If you’d prefer to watch this process from start to finish, check out this five-minute quick fire-video on YouTube that shows me learning how to create my first node module and publish it to npm — live on Twitch!

Let’s go through step by step how to create, build and publish a package to npm that’s written in ES6.



Prerequisites

Ensure you’ve installed Node.js and npm on your machine.



Create an account on npm

You’ll need this to be able to publish your package. Sign up here.



Login to npm via your terminal

Run npm login in your terminal and enter your username, password and email. This will ensure you can publish your package later via the CLI.

A screenshot of the output in a terminal after running npm login



Set up your project

Create a new directory for your project that will contain the code for your npm package. Navigate to that directory. For unscoped modules (without the @scope-name prefix), run npm init. This will create the package.json file for your project.

For my first node module, I decided to create a scoped public package, so that it would include my brand name. To initialise a scoped module, run npm init --scope=@scope-name. Follow the instructions in the terminal to configure your project. Read more about scoped public packages here.

mkdir my-new-npm-package
cd my-new-npm-package
npm init
# or for scoped packages
npm init --scope=@scope-name
Enter fullscreen mode

Exit fullscreen mode

Here’s the package.json file that was created via the CLI for @whitep4nth3r
/random-code.

{
"name": "@whitep4nth3r/random-code",
"version": "1.0.0",
"description": "Need some code for your project? We've got you covered. Choose your language. Choose how much code. BÄM! You got code.",
"main": "index.js",
"scripts": {
"test": "echo "Error: no test specified"" &

Leave a Reply

Your email address will not be published.