One package.json to rule them all
Tags: npm, packagejson, javascript
Published: 2019-03-18 10:05:28

---
title: One package.json to rule them all
published: true
description: How to create a useful default package.json for all your future projects
tags: npm, package.json, javascript
cover_image: https://images.unsplash.com/photo-1531777319985-9dca28eeae64?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1023&q=80
---

>I am going to start this article with a disclaimer: the title is a clickbait, or maybe not - depends on how you look at it. 

Haven gotten that out of the way, let's get to the meat of the matter: creating a useful default package.json.


### __What you need to know (WYNTK)__
If you have gone through the [official npm docs](https://docs.npmjs.com/), you might know that you can create a default package.json configuration; thereby skipping the CLI questionnaire that follows whenever you run the `npm init` command. 
In fact, you can skip the CLI questionnaire by: opening a terminal somewhere on your PC right now; running `npm init -y`; if you do not have a `.npm-init.js` file in your home directory, npm will create a package.json in the folder your terminal is pointing to, the created package.json file will contain this default content:

```json
{
  "name": "",
  "version": ""
}
```

These are the only required properties for a npm package. However, the thoughtful guys at npm allow us, devs, to change the default configuration or CLI questionnaire. Here's how.

As you might have guessed, to create a default package.json configuration, you need to create a `.npm-init.js` file in the home directory of your PC.


### __Creating the `.npm-init.js` file__

Open a terminal(preferably git-bash on windows)anywhere on your PC , and run `cd ~` this will point the terminal to your home directory. 
Next, run `echo "const path = require('path')" >.npm-init.js`. This will create a `.npm-init.js` file in the home directory.
In the same terminal window, type `start ".npm-init.js"` and hit enter. This will open the npm-init file in your default application for opening Javascript files.

>_Note that some of these commands might not work on versions of Windows prior to Windows 10_


### __Configuring `.npm-init.js`__

To configure npm's default package.json configuration, we need to expose an object to `module.exports` from the `.npm-init.js` file in your home directory. Presently, here's what our npm-init file contains:
```javascript
const path = require('path')
```

Edit it, so that it looks like so:
```javascript
const path = require('path')

module.exports = {
}
```

#### Add a default `author` property
The email and website details are optional in the author property.

```javascript
const path = require('path')

module.exports = {
  'author': 'Promise Tochi <your email@domain.com> (https://your-website.com)',
}
```

#### Add a default `version` property
```javascript
const path = require('path')

module.exports = {
  'author': 'Promise Tochi <your email@domain.com> (https://your-website.com)',
  'version': '0.0.1'
}
```

#### Add a default `description` property
```javascript
const path = require('path')

module.exports = {
  'author': 'Promise Tochi <your email@domain.com> (https://your-website.com)',
  'version': '0.0.1',
  'description': 'My awesome new project'
}
```

#### Add a default `license` property
```javascript
const path = require('path')

module.exports = {
  'author': 'Promise Tochi <your email@domain.com> (https://your-website.com)',
  'version': '0.0.1',
  'description': 'My awesome new project',
  'license': 'MIT'
}
```

#### Add a default `name` property 
```javascript
const path = require('path')

module.exports = {
  'name': 'my_new_project',
  'author': 'Promise Tochi <your email@domain.com> (https://your-website.com)',
  'version': '0.0.1',
  'description': 'My awesome new project',
  'license': 'MIT'
}
```

You might be thinking why we have to go through all these trouble and end up with a static default name; "why are you wasting my time man?!"

The good thing is we can improve on this by using the name of the folder where you are running `npm init` from. We can change the contents of the npm-init file to:

```javascript
const path = require('path')

module.exports = {
  'name': path.basename(process.cwd()),
  'author': 'Promise Tochi <your email@domain.com> (https://your-website.com)',
  'version': '0.0.1',
  'description': 'My awesome new project',
  'license': 'MIT'
}
```

If you are wondering:
>process.cwd() method returns the path to the current working directory of the Node.js process
>

>path.basename() methods returns the last portion of a path

We can improve this a little because npm requires that package names be URL friendly, that is, they shouldn't contain spaces or any special charaters. So let's change our `.npm-init.js` to:

```javascript
const path = require('path')
const projectName = slugify(path.basename(process.cwd()))

module.exports = {
  'name': projectName,
  'author': 'Promise Tochi <your email@domain.com> (https://your-website.com)',
  'version': '0.0.1',
  'description': 'My awesome new project',
  'license': 'MIT'
}

function slugify(text) {
  return text.toString().toLowerCase()
    .replace(/\s+/g, '-')           // Replace spaces with -
    .replace(/[^\w\-]+/g, '')       // Remove all non-word chars
    .replace(/\-\-+/g, '-')         // Replace multiple - with single -
    .replace(/^-+/, '')             // Trim - from start of text
    .replace(/-+$/, '')             // Trim - from end of text
}
```

#### Adding in some more properties

Next we'll add in default `repository`, `bugs` and `homepage` properties. 

Here, I'm assuming that you use git and github for version control. If you are, you can guarantee that your project repository will be at `https://github.com/{user or organization name}/{project name}`. This leads us to:

```javascript
const path = require('path')
const projectName = slugify(path.basename(process.cwd()))
const projectUrl = `https://github.com/{your github user name}/${projectName}`

module.exports = {
  'name': projectName,
  'author': 'Promise Tochi <your email@domain.com> (https://your-website.com)',
  'version': '0.0.1',
  'description': 'My awesome new project',
  'license': 'MIT',
  'repository': {
    'type': 'git',
    'url': projectUrl
  },
  'bugs': {
    'url': `${projectUrl}/issues`
  },
  'homepage': projectUrl,
}

function slugify(text) {
  return text.toString().toLowerCase()
    .replace(/\s+/g, '-')           // Replace spaces with -
    .replace(/[^\w\-]+/g, '')       // Remove all non-word chars
    .replace(/\-\-+/g, '-')         // Replace multiple - with single -
    .replace(/^-+/, '')             // Trim - from start of text
    .replace(/-+$/, '')             // Trim - from end of text
}
```

### __Trying out our default package.json configuration__

Create a new folder on your PC and point your terminal at the folder. In the terminal, run `npm init`, you should get a CLI prompt similar to this:

![npm prompt after running npm init](https://thepracticaldev.s3.amazonaws.com/i/4ubkplklbtnmg7avtstl.png "npm init prompt")

To use the default configuration, type `yes` in the terminal and hit the `Enter` key.

Hopefully, that should save you a few minutes when creating a new npm package in the future, if not I hope you learnt something new. Thanks for reading all the way, happy coding.

> This is my first article in 2019 and I hope to write a lot more.
> 
> Cover Photo by William Krause on Unsplash 
><a style="background-color:black;color:white;text-decoration:none;padding:4px 6px;font-family:-apple-system, BlinkMacSystemFont, &quot;San Francisco&quot;, &quot;Helvetica Neue&quot;, Helvetica, Ubuntu, Roboto, Noto, &quot;Segoe UI&quot;, Arial, sans-serif;font-size:12px;font-weight:bold;line-height:1.2;display:inline-block;border-radius:3px" href="https://unsplash.com/@williamk?utm_medium=referral&amp;utm_campaign=photographer-credit&amp;utm_content=creditBadge" target="_blank" rel="noopener noreferrer" title="Download free do whatever you want high-resolution photos from William Krause"><span style="display:inline-block;padding:2px 3px"><svg xmlns="http://www.w3.org/2000/svg" style="height:12px;width:auto;position:relative;vertical-align:middle;top:-2px;fill:white" viewBox="0 0 32 32"><path d="M10 9V0h12v9H10zm12 5h10v18H0V14h10v9h12v-9z"></path></svg></span><span style="display:inline-block;padding:2px 3px">William Krause</span></a>