I know I can do:
npm config set init.author.email [email protected]
npm config set init.license UNLICENSED
To set the defaults used to create a new package.json
with npm init
. But how can I set the default value for the test command
? I've tried
npm config set init.scripts.test "mocha"
But it doesn't work. The npm docs don't seem to help.
Is there a list of all the init defaults?
There is a list of all config defaults npm config list -l
.
As you can see there isn't anything like init.scripts. But there is another way to do it. If you first npm install mocha
and then npm init
you will get a package.json like:
{
"name": "asd",
"version": "1.0.0",
"description": "",
"main": "index.js",
"dependencies": {},
"devDependencies": {
"mocha": "^2.4.5"
},
"scripts": {
"test": "mocha"
},
"author": "",
"license": "UNLICENSED"
}
Although you can't (currently) configure default scripts via npm settings, you can customise the entire npm init
output by providing your own init-module. The default is stored at ~/.npm-init.js
- whatever you export becomes the result of running npm init
.
To make this useful, you'd probably want to hijack the existing default npm init module, which lives in <your npm install directory>/node_modules/init.js
. If you only care about providing a default test script, the simplest option is to use init-module, which provides a configuration parameter init-scripts-test
.
I was searching for a way to programmatically create a package.json
with dynamically defined values. I noticed creating a package.json
file before calling npm init -y
works as expected.
// Require: fs
const fs = require('fs')
// Variables
const values = {
"scripts": {
"test": "mocha"
}
// as well as any other values you want
}
// Create package.json
fs.writeFileSync('package.json', values)
Running npm init -y
after running the above code would generate a package.json
with scripts.test = "mocha"
defined.
If you're looking for a way to run npm init -y
from within code as well, I recommend using shelljs: shell.exec('npm init -y')
.
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