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.
package.json
?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
{"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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With