How to Start a Node.js Project
You may be surprised, as this author was, about just how easy it can be to start a Node.js project.
Join the DZone community and get the full member experience.Join For Free
Sometimes I write blog posts to remind myself what I’ve learned and sometimes I write them because someone else shares something and I want to remember that better. This post is one of the latter.
Starting a Node.js Project
Usually, when I start a new Node.js project I use
npm to generate my initial project.
npm then asks me some questions and builds a
package.json file for me. Then I start building the project.
Later I inevitably copy and paste a
.gitignore file from GitHub’s useful repo of
.gitignore templates. And if I remember I’ll actually create a
LICENSE file with the open source license that I intended to use.
This is not efficient.
Then this week I saw Tierney Cyren tweet this:
These four commands do everything that I was doing manually and more, setting up a project for success right from the start.
npx license mituses the license package to download a license of choice, in this case, the MIT license
npx gitignore nodeuses the gitignore package to automatically download the relevant
.gitignorefile from GitHub’s repo
npx covgenuses the covgen package to generate the Contributor Covenant and give your project a code of conduct that will be welcoming to all contributors
If you’ve not seen
npx before it looks locally to see if there is a command to run and executes it. If there is no local command, it will try to download and install the command from
npm, and run it. This is really useful when generating new projects and saves you from globally installing a bunch of
npm packages that are only used in this setup mode.
npm init -yaccepts all of the default options that
npm initasks you about
Tierney also suggested customizing your
npm init defaults so that the output of
npm init -y is correct.
You can see your current
npm config by entering
npm config list on the command line. To just see the config that affects
npm init you can
There are a number of defaults you can set: author name, author email, author URL, the license, and the version. To set them, you can enter them on the command line or use
npm config edit to open up the config file in your text editor. The command line is easy enough though, you can set all five defaults like so:
Once you have that customized to your liking,
npm init -y will always produce the right settings.
Building Your Own init Script
There are some improvements that I’d make to Tierney’s commands, though I appreciate he was constrained by Twitter. Here’s a bash script I have come up with inspired by his tweet.
To the original, I’ve added fetching the license type, the author name and email from the
npm init defaults. I’ve also initialized a new git repository and committed the results of this script as the “Initial commit.”
You can take this function and add it to your
~/.bash_profile. Then, either
source ~/.bash_profile or open a new command line window and run
node-project. Feel free to add or remove other bits as you see fit to create your perfect initialisation script.
Go Start a Project
Now you have the perfect script to start a Node.js project, why not go create a new one? I have a few small projects in mind that I plan to build this year and this is a nice basis to start from.
If you have any more suggestions to improve the script, let me know on Twitter at @philnash. Happy open sourcing!
Published at DZone with permission of Phil Nash, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.