Gathering detailed insights and metrics for @npmcli/package-json
Gathering detailed insights and metrics for @npmcli/package-json
Gathering detailed insights and metrics for @npmcli/package-json
Gathering detailed insights and metrics for @npmcli/package-json
Programmatic API to update package.json
npm install @npmcli/package-json
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
69 Stars
135 Commits
9 Forks
11 Watching
1 Branches
74 Contributors
Updated on 27 Nov 2024
JavaScript (100%)
Cumulative downloads
Total Downloads
Last day
1.4%
1,075,306
Compared to previous day
Last week
7.2%
5,753,653
Compared to previous week
Last month
11.8%
23,202,612
Compared to previous month
Last year
111.2%
197,633,552
Compared to previous year
Programmatic API to update package.json
files. Updates and saves files the
same way the npm cli handles them.
npm install @npmcli/package-json
1const PackageJson = require('@npmcli/package-json') 2const pkgJson = await PackageJson.load(path) 3// $ cat package.json 4// { 5// "name": "foo", 6// "version": "1.0.0", 7// "dependencies": { 8// "a": "^1.0.0", 9// "abbrev": "^1.1.1" 10// } 11// } 12 13pkgJson.update({ 14 dependencies: { 15 a: '^1.0.0', 16 b: '^1.2.3', 17 }, 18 workspaces: [ 19 './new-workspace', 20 ], 21}) 22 23await pkgJson.save() 24// $ cat package.json 25// { 26// "name": "foo", 27// "version": "1.0.0", 28// "dependencies": { 29// "a": "^1.0.0", 30// "b": "^1.2.3" 31// }, 32// "workspaces": [ 33// "./new-workspace" 34// ] 35// }
There is also a helper function exported for opening a package.json file with no extra normalization or saving functionality.
1const { readPackage } = require('@npmcli/package-json/lib/read-package') 2const rawData = await readPackage('./package.json') 3// rawData will now have the package.json contents with no changes or normalizations
constructor()
Creates a new empty instance of PackageJson
.
async PackageJson.create(path)
Creates an empty package.json
at the given path. If one already exists
it will be overwritten.
async PackageJson.load(path, opts = {})
Loads a package.json
at the given path.
opts
: Object
can contain:
create
: Boolean
if true, a new package.json will be created if one does not already exist. Will not clobber ane existing package.json that can not be parsed.Loads contents of a package.json
file located at ./
:
1const PackageJson = require('@npmcli/package-json') 2const pkgJson = new PackageJson() 3await pkgJson.load('./')
Throws an error in case a package.json
file is missing or has invalid contents.
async PackageJson.load(path)
Convenience static method that returns a new instance and loads the contents of a package.json
file from that location.
path
: String
that points to the folder from where to read the package.json
fromLoads contents of a package.json
file located at ./
:
1const PackageJson = require('@npmcli/package-json') 2const pkgJson = await PackageJson.load('./')
async PackageJson.normalize()
Intended for normalizing package.json files in a node_modules tree. Some light normalization is done to ensure that it is ready for use in @npmcli/arborist
path
: String
that points to the folder from where to read the package.json
fromopts
: Object
can contain:
strict
: Boolean
enables optional strict mode when applying the normalizeData
stepsteps
: Array
optional normalization steps that will be applied to the package.json
file, replacing the default stepsroot
: Path
optional git root to provide when applying the gitHead
stepchanges
: Array
if provided, a message about each change that was made to the packument will be added to this arrayasync PackageJson.normalize(path, opts = {})
Convenience static that calls load
before calling normalize
path
: String
that points to the folder from where to read the package.json
fromopts
: Object
can contain:
strict
: Boolean
enables optional strict mode when applying the normalizeData
stepsteps
: Array
optional normalization steps that will be applied to the package.json
file, replacing the default stepsroot
: Path
optional git root to provide when applying the gitHead
stepchanges
: Array
if provided, a message about each change that was made to the packument will be added to this arrayasync PackageJson.prepare()
Like normalize
but intended for preparing package.json files for publish.
async PackageJson.prepare(path, opts = {})
Convenience static that calls load
before calling prepare
path
: String
that points to the folder from where to read the package.json
fromopts
: Object
can contain:
strict
: Boolean
enables optional strict mode when applying the normalizeData
stepsteps
: Array
optional normalization steps that will be applied to the package.json
file, replacing the default stepsroot
: Path
optional git root to provide when applying the gitHead
stepchanges
: Array
if provided, a message about each change that was made to the packument will be added to this arrayasync PackageJson.fix()
Like normalize
but intended for the npm pkg fix
command.
PackageJson.update(content)
Updates the contents of a package.json
with the content
provided.
content
: Object
containing the properties to be updated/replaced in the
package.json
file.Special properties like dependencies
, devDependencies
,
optionalDependencies
, peerDependencies
will have special logic to handle
the update of these options, such as sorting and deduplication.
Adds a new script named new-script
to your package.json
scripts
property:
1const PackageJson = require('@npmcli/package-json') 2const pkgJson = await PackageJson.load('./') 3pkgJson.update({ 4 scripts: { 5 ...pkgJson.content.scripts, 6 'new-script': 'echo "Bom dia!"' 7 } 8})
NOTE: When working with dependencies, it's important to provide values for all known dependency types as the update logic has some interdependence in between these properties.
A safe way to add a devDependency
AND remove all peer dependencies of an
existing package.json
:
1const PackageJson = require('@npmcli/package-json') 2const pkgJson = await PackageJson.load('./') 3pkgJson.update({ 4 dependencies: pkgJson.content.dependencies, 5 devDependencies: { 6 ...pkgJson.content.devDependencies, 7 foo: '^foo@1.0.0', 8 }, 9 peerDependencies: {}, 10 optionalDependencies: pkgJson.content.optionalDependencies, 11})
PackageJson.content
Getter that retrieves the normalized Object
read from the loaded
package.json
file.
1const PackageJson = require('@npmcli/package-json') 2const pkgJson = await PackageJson.load('./') 3pkgJson.content 4// -> { 5// name: 'foo', 6// version: '1.0.0' 7// }
async PackageJson.save()
Saves the current content
to the same location used when calling
load()
.
No vulnerabilities found.
Reason
security policy file detected
Details
Reason
no binaries found in the repo
Reason
17 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 10
Reason
0 existing vulnerabilities detected
Reason
no dangerous workflow patterns detected
Reason
license file detected
Details
Reason
SAST tool detected but not run on all commits
Details
Reason
Found 8/10 approved changesets -- score normalized to 8
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Score
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More