Latest news about Bitcoin and all cryptocurrencies. Your daily crypto news habit.
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 init
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:
body[data-twttr-rendered="true"] {background-color: transparent;}.twitter-tweet {margin: auto !important;}
How to start any new Node.js project: $ npx license mit > LICENSE $ npx gitignore node $ npx covgen YOUR_EMAIL_ADDRESS $ npm init -y You're ready to start coding.
āāā@bitandbang
These four commands do everything that I was doing manually and more, setting up a project for success right from theĀ start.
- npx license mit uses the license package to download a license of choice, in this case the MITĀ license
- npx gitignore node uses the gitignore package to automatically download the relevantĀ .gitignore file from GitHubāsĀ repo
- npx covgen uses 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, 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 -y accepts all of the default options that npm init asks youĀ about
Tierney also suggested customising your npm init defaults so that the output of npm init -y isĀ correct.
Customising npmĀ init
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 grep forĀ āinitā:
npm config list | grep init
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:
npm set init.author.name "Your name"npm set init.author.email "your@email.com"npm set init.author.url "https://your-url.com"npm set init.license "MIT"npm set init.version "1.0.0"
Once you have that customised 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.
function node-project { git init npx license $(npm get init.license) -o "$(npm get init.author.name)" > LICENSE npx gitignore node npx covgen "$(npm get init.author.email)" npm init -y git add -A git commit -m "Initial commit"}
To the original Iāve added fetching the license type, the author name and email from the npm init defaults. Iāve also initialised 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!
Originally published at philna.sh on January 10,Ā 2019.
How to start a Node.js project was originally published in Hacker Noon on Medium, where people are continuing the conversation by highlighting and responding to this story.
Disclaimer
The views and opinions expressed in this article are solely those of the authors and do not reflect the views of Bitcoin Insider. Every investment and trading move involves risk - this is especially true for cryptocurrencies given their volatility. We strongly advise our readers to conduct their own research when making a decision.