Search code examples
npmnpm-package

`npm pkg set` command does not reflect variable type properly


How to make a property remain its type when using npm pkg set command? We are using latest npm currently. We just followed the docs, https://docs.npmjs.com/cli/v9/commands/npm-pkg?v=true

Commands:

npm pkg set 'prettier.printWidth'=100
npm pkg set 'prettier.singleQuote'=true

Expected result:

// package.json
{
  // ...

  "prettier": {
    "singleQuote": true,
    "printWidth": 100
  }
}

Actual result:

// package.json
{
  // ...

  "prettier": {
    "singleQuote": "true",
    "printWidth": "100"
  }
}

Solution

  • By adding --json flag, the value will be added after parsing as json. Therefore type would be remained.

    It's also possible to parse values as json prior to saving them to your package.json file, for example in order to set a "private": true property

    npm pkg set 'prettier.printWidth'=100 --json
    npm pkg set 'prettier.singleQuote'=true --json