Find unused npm packages in package.json

后端 未结 8 966
孤街浪徒
孤街浪徒 2020-12-12 08:52

Is there a way to determine if you have packages in your package.json file that are no longer needed?

For instance, when trying out a package and later commenting o

相关标签:
8条回答
  • 2020-12-12 09:37

    If you're using a Unix like OS (Linux, OSX, etc) then you can use a combination of find and egrep to search for require statements containing your package name:

    find . -path ./node_modules -prune -o -name "*.js" -exec egrep -ni 'name-of-package' {} \;
    

    If you search for the entire require('name-of-package') statement, remember to use the correct type of quotation marks:

    find . -path ./node_modules -prune -o -name "*.js" -exec egrep -ni 'require("name-of-package")' {} \;
    

    or

    find . -path ./node_modules -prune -o -name "*.js" -exec egrep -ni "require('name-of-package')" {} \;
    

    The downside is that it's not fully automatic, i.e. it doesn't extract package names from package.json and check them. You need to do this for each package yourself. Since package.json is just JSON this could be remedied by writing a small script that uses child_process.exec to run this command for each dependency. And make it a module. And add it to the NPM repo...

    0 讨论(0)
  • 2020-12-12 09:37

    if you want to choose upon which giant's shoulders you will stand

    here is a link to generate a short list of options available to npm; it filters on the keywords unused packages

    https://www.npmjs.com/search?q=unused%20packages

    0 讨论(0)
提交回复
热议问题