13 npm Tricks for Faster JavaScript Development

Speed up your JavaScript development workflow with npm and package.json

1. Learn the Essential Shortcuts

Less Common Shortcuts

The Root Shortcut

{
"main": "index.js"
}

2. Set Default npm init Properties

npm config set init.author.name  "Joe Bloggs"
npm config set init.author.email "joebloggs@gmail.com"
npm config set init.author.url "joebloggs.com"
npm config set init.license "MIT"
echo "" > $(npm config get userconfig)
npm config edit
echo "" > $(npm config get globalconfig)
npm config --global edit

3. Make Scripts Cross-Platform Compatible

{
"scripts": {
"build": "cross-env NODE_ENV=production webpack --config build/webpack.config.js"
}
}

4. Run Scripts in Parallel

{
"start": "concurrently \"command1 arg\" \"command2 arg\""
}

5. Run Scripts in Different Directories

cd folder && npm start && cd ..
npm start --prefix path/to/your/folder
"start": "concurrently \"(npm start --prefix client)\" \"(npm start --prefix server)\"",

6. Delay Running Scripts Until a Port is Ready

"dev": "concurrently \"cross-env BROWSER=none npm run start\" \"wait-on http://localhost:3000 && electron .\"",

7. List and Select Available Scripts

npm i -g ntl
Image for post
Image for post

8. Run Pre and Post Scripts

9. Control Your App’s Version

// 1.0.0
npm version patch
// 1.0.1
npm version minor
// 1.1.0
npm version major
// 2.0.0
{
"predeploy": "npm version patch"
}

10. Edit package.json from the Command Line

npm install -g json
json -I -f package.json -e 'this.scripts.foo="bar"'

11. Set and Open Your Repository Automatically

git config --get remote.origin.url
json -I -f package.json -e "this.repository=\"$(git config --get remote.origin.url)\""

12. Create a Custom npm init Script

npm config set init-module ~\.npm-init.js
module.exports = {
name: prompt('package name', basename || package.name),
version: prompt('version', '0.0.0'),
decription: prompt('description', ''),
main: prompt('entry point', 'index.js'),
repository: prompt('git repository', ''),
keywords: prompt(function (s) { return s.split(/\s+/) }),
author: prompt('author', 'Joe Bloggs <joe.bloggs@gmail.com> (joebloggs.com)'),
license: prompt('license', 'ISC')
}

13. Push Your First Commit to GitHub with a Custom npm init Script

const { execSync } = require('child_process');
function run(func) {
console.log(execSync(func).toString())
}
repository: prompt('github repository url', '', function (url) {
if (url) {
run('touch README.md');
run('git init');
run('git add README.md');
run('git commit -m "first commit"');
run(`git remote add origin ${url}`);
run('git push -u origin master');
}
return url;
})

Writer and developer based in London. On Medium, I write about JavaScript and web development 💻

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store