Distill package.json's dependencies to top-level dependencies only

Viewed 141

After I ejected from create-react-app, I was left with a zoo of dependencies inside of package.json. Is there a npm command or something else that would help me distill the dependencies down and get rid of any package mentions that are only dependencies of some other package. For example, if package.json looks like

package.json

{
  "name": "mypackage",
...
  ],
  "dependencies": {
    "A": "1",
    "B": "1",
    "C": "1",
...
}

and both A and B depend on C and I don't care to specify C's version, is there a command to simply remove C from package.json and thus clean up this file?

2 Answers

npm prune should do the job

http://doc.codingdict.com/npm-ref/cli/prune.html

From the docs:

This command removes "extraneous" packages. If a package name is provided, then only packages matching one of the supplied names are removed.

Extraneous packages are packages that are not listed on the parent package's dependencies list.

If the --production flag is specified or the NODE_ENV environment variable is set to production, this command will remove the packages specified in your devDependencies. Setting --no-production will negate NODE_ENV being set to production.

If the --dry-run flag is used then no changes will actually be made.

If the --json flag is used then the changes npm prune made (or would have made with --dry-run) are printed as a JSON object.

In normal operation with package-locks enabled, extraneous modules are pruned automatically when modules are installed and you'll only need this command with the --production flag.

If you've disabled package-locks then extraneous modules will not be removed and it's up to you to run npm prune from time-to-time to remove them.

You can delete it manually or use npm uninstall dependency name to delete it

Related