Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bare minimum package.json

When I run npm init -y I get the following package.json file:

{
  "name": "myapp",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC"
}

This however, includes several things that doesn't actually seem to be required.

  1. What is actually the bare minimum package.json?
  2. And, what I really want to know, is there a command or flag I can use to have that generated instead?
like image 219
Svish Avatar asked Aug 05 '19 20:08

Svish


2 Answers

The documentation states that the only required fields are name and version.

Required name and version fields

A package.json file must contain "name" and "version" fields:

The "name" field contains your package’s name, and must be lowercase and one word, and may contain hyphens and underscores.

The "version" field must be in the form x.x.x and follow the semantic versioning guidelines.

According to the documentation for init, there seems to be no way to initialize a package with only these fields though, looks like you would need to do this yourself (or create a bash script that can generate it for you).

You could create a simple script like this:

#!/bin/bash

printf "{\n\t\"name\": \"$1\",\n\t\"version\": \"$2\"\n}" > package.json

And call it like this:

./init.sh test 1.0.0

Which would generate a file looking like this:

{
    "name": "test",
    "version": "1.0.0"
}

If you want a script that replicated the behaviour of npm init -y (using the directory name and setting the version to 1.0.0), try this instead:

#!/bin/bash

CURRENT=`pwd`
BASENAME=`basename "$CURRENT"`

printf "{\n\t\"name\": \"$BASENAME\",\n\t\"version\": \"1.0.0\"\n}" > package.json
like image 72
André Ekeberg Avatar answered Sep 17 '22 14:09

André Ekeberg


{"private": "true"}

Is an effective solution if you never intend to publish the project. This keyword will stop the warnings and errors for all those irrelevant fields

like image 34
Tristan Avatar answered Sep 17 '22 14:09

Tristan