Thursday, July 11, 2019

NPM : 13 npm Tricks for Faster JavaScript Development


Every day, millions of developers reach for npm (or Yarn) for their JavaScript projects. Running commands like  or  has become the go-to way to begin almost any JavaScript project, whether you’re building code for the client-side or server-side — or even if you’re building a desktop app.
But there’s a lot more to npm than initialising projects or installing packages. In this article, we’ll discuss 13 tricks to get the most out of npm: from simple shortcuts to custom  scripts.
Since many of us use npm every day, even saving a small amount of time could make a significant difference in the long run. These tricks are aimed at beginner and intermediate developers, but even if you’re an experienced developer, I hope you still find one or two features that you hadn’t encountered before.
Finally, if you’re completely new to npm, it comes bundled with Node.js, which you can install at https://nodejs.org/en/. If you’re on Windows, I recommend installing Git Bash to follow along. Let’s dive in.

Contents

1. Learn the Essential Shortcuts

We’ll start with the basics. A short time spent learning the most common npm shortcuts will mean time-saved in the long run.
  • Installing — Regular:  . Shortcut:  .
  • Testing— Regular: . Shortcut: .
  • Getting Help— Regular:  . Shortcut:  .
  • The Global Flag — Regular:  . Shortcut  .
  • Saving as a Development Dependency — Regular:  . Shortcut:  .
  • Accepting npm init Defaults — Regular:  or  . Shortcut:  or  .
You no longer need to use  or  to save packages, as that is now the default. To install a package without saving it, use the  flag.

Less Common Shortcuts

There are also a handful of less common shortcuts, including:
  • Saving as an Optional Dependency — Regular:  . Shortcut:  .
  • Saving an Exact Version of a Package — Regular: . Shortcut:  .
If you ever need to preserve an npm package locally or have a selection of packages available through a single file download, you can bundle them together using  or  , and obtain the bundle by using  .

The Root Shortcut

The  symbol is commonly used to represent the root directory of an application or (depending on context) the app’s entry point — in npm terms, that’s whatever’s specified as the value of  in  :
{
  "main": "index.js"
}
This shortcut can also be used with commands like . So, instead of creating a new  directory with  , you can run  to set up React in the folder you’re already in.

2. Set Default npm init Properties

When running  to begin a new project, you’ll likely find yourself typing out some details again and again. For example, chances are you are the author of most of your projects. To save time, you can set default values for these fields like:
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"
To check that these properties were added correctly, type to bring up the configuration file. You can also edit details by typing values directly into this file. If you want to edit global npm setting, use  .
To go back to the default settings, you can use the following script. The first line replaces the config file with an empty string, and the second line re-fills it with the default settings.
The above script will reset user defaults, and the following one will reset global defaults:

3. Make Scripts Cross-Platform Compatible

Any code that runs on the command line risks compatibility issues, especially between Windows and Unix-based systems (including Mac and Linux). This not be a problem if you — and only you — are working on a particular project, but there are many cases where cross-platform compatibility is key: any open source or collaborative project, as well as example and tutorial projects, should work regardless of the operating system.
Thankfully, the solution is simple. There are a few options, but the one I’ve had best results with is  . Install it as a development dependency, using  . Then include the keyword  before any environmental variables, like this:
{
  "scripts": {
    "build": "cross-env NODE_ENV=production webpack --config build/webpack.config.js"
  }
}
I’ve found cross-env to be the most seamless way to achieve cross-platform compatibility, but it may be worth checking out two other popular tools that can help with this:
  • rimraf can be installed globally to run cross-platform scripts;
  • ShellJS is a portable implementation of Unix shell commands on top of the Node.js API.

4. Run Scripts in Parallel

You can use  to run two or more processes one after the other. But what about running scripts in parallel? To do this, we can choose from a variety of npm packages. concurrently and npm-run-all are the most popular solutions, and in this example, we’ll use concurrently.
First, install it as a development dependency:  . Then you can add it to your scripts in the following format:
{
  "start": "concurrently \"command1 arg\" \"command2 arg\""
}

5. Run Scripts in Different Directories

Sometimes, you’ll have an app with multiple  files in different folders. It would be convenient to access these scripts from the root directory, rather than navigating to the different folders every time you want to run a script, and there are two ways to do this.
First, you could use  to access the different folders automatically:
cd folder && npm start && cd ..
But there’s a more elegant solution, which is to use the  flag to specify the path:
Here’s an example of this solution in a working app, where we want to run  in both our front-end (in the ‘client’ directory) and our back-end (in the ‘server’ directory).
"start": "concurrently \"(npm start --prefix client)\" \"(npm start --prefix server)\"",

6. Delay Running Scripts Until a Port is Ready

Often, during the development of a full-stack app, you’ll probably want to start both a server and a client-side presentation layer. The  node module provides a handy way to make sure processes only take place when certain processes are ready: in our case, we a particular port.
For example, here’s a  script I’m using in an Electron project that uses a React front-end. Using , the script loads the presentation layer and the Electron window in parallel. But, using , the Electron window only opens once React’s presentation layer is ready on  .
"dev": "concurrently \"cross-env BROWSER=none npm run start\" \"wait-on http://localhost:3000 && electron .\"",
In addition, React opens a browser window by default, but for Electron development that’s unnecessary. We can disable this behaviour by passing the environmental variable  , preceded by  for cross-platform compatibility.

7. List and Select Available Scripts

Listing the scripts available in a  file is easy: just go to the root directory of your project and type  in the terminal.
But there’s an even more convenient way to get a list of scripts, which you can run immediately: to do this, install the NTL (npm Task List) module globally:
npm i -g ntl
Then run the  command in the project folder. You’ll get a list of available scripts, with the option of selecting one of them to run.


This can be handy if you’re not sure what scripts a project has, or if you’d rather type  than a longer script name!

8. Run Pre and Post Scripts

You may be familiar with scripts like  and  , which allow you to define code to run either before or after your  script. But in fact  and  can be added before any script, including custom ones.
Not only does this make your code cleaner, but it also allows you to run the  and  scripts in isolation.

9. Control Your App’s Version

Rather than manually changing your app’s version, npm comes with some useful shortcuts to do exactly that. To increase the version, type  plus  ,  or  :
// 1.0.0
npm version patch
// 1.0.1
npm version minor
// 1.1.0
npm version major
// 2.0.0
Depending on how regularly you update your app, you could save time by increase the version number every time you deploy, using a script such as:
{
  "predeploy": "npm version patch"
}

10. Edit package.json from the Command Line

The  is a regular  file, so it can be edited from the command line using the tool . This opens up new possibilities when it comes to modifying  , allowing you to create shortcuts beyond the defaults. Install it globally:
Then you can use it for in-place editing with  . For example, to add a new script “foo” with the value “bar”, type:
For a more practical example of the  module in action, see the next tip!

11. Set and Open Your Repository Automatically

If you have a  entry in your  file, you can open it in your default browser by typing  .
If your project is already connected to a remote repository and you’ve got a  installed on the command line, you can find out your repository with this command:
git config --get remote.origin.url
Even better, if you followed the tip above and installed the  node module, you can use the following script to automatically add in the correct repository to :
git config --get remote.origin.url)\""

12. Create a Custom npm init Script

Let’s take it a step further, with our own  script that takes a GitHub repository URL and automatically pushes our first commit. In this tip, we’ll discuss how to create a custom  script. In the next (and final) tip, we’ll incorporate git.
You can edit the  script by redirecting is to a  file in your home directory. (On Windows, that’s usually  , and on Mac, it’s  ).
Let’s begin by creating a  file in our home directory. To make sure that  is directed to the correct file, you can run:
npm config set init-module ~\.npm-init.js
Before integrating git, here’s a simple  file that mimics the questions of the default  :
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')
}
Each question follows the pattern  . To set a value by default without the question, simply remove the  method.
If you want to return to the default settings, simply delete  .

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

In order to incorporate  commands into our  file, we’ll need a way to control the command line. To do this, we can use the  module. We’ll require it at the top of our file and, as we only need the  function, we can grab that by itself using destructuring assignment syntax:
const { execSync } = require('child_process');
I also created a helper function, which prints the results of our function to the console:
function run(func) {
  console.log(execSync(func).toString())
}
Finally, we’ll make a prompt for a GitHub repository URL, and — if provided — we’ll generate a  file and push our first commit. This should be one of the items in our  object:
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;
})
Altogether, our  file should look something like this:

And that will give us a  file with the following fields:

You could take this even further by incorporating the GitHub API, so that you don’t even have to create a new repository — but I’ll leave that bit up to you!



Overall, I hope this article has opened your eyes to what can be achieved with npm, and demonstrated some of the ways in which you can boost your productivity — whether by making sure you know common shortcuts, by getting the most out of the scripts in , or by coding a custom version of  .
If you have any questions, feel free to leave a comment, and don’t forget to check out the official npm docs for more information. Thanks for reading!

https://medium.com/@bretcameron/13-npm-tricks-for-faster-javascript-development-4fe2a83f87a2