How to clean node_modules folder of packages that are not in package.json?

后端 未结 13 1207
别那么骄傲
别那么骄傲 2020-12-02 03:58

Assume I install project packages with npm install that looks into package.json for modules to be installed. After a while I see that I don\'t need

相关标签:
13条回答
  • 2020-12-02 04:26

    I have added few lines inside package.json:

    "scripts": {
      ...
      "clean": "rmdir /s /q node_modules",
      "reinstall": "npm run clean && npm install",
      "rebuild": "npm run clean && npm install && rmdir /s /q dist && npm run build --prod",
      ...
    }
    

    If you want to clean only you can use this rimraf node_modules or rm -rf node_modules.

    It works fine

    0 讨论(0)
  • 2020-12-02 04:30

    You could remove your node_modules/ folder and then reinstall the dependencies from package.json.

    rm -rf node_modules/
    npm install
    

    This would erase all installed packages in the current folder and only install the dependencies from package.json. If the dependencies have been previously installed npm will try to use the cached version, avoiding downloading the dependency a second time.

    0 讨论(0)
  • 2020-12-02 04:34

    Due to its folder nesting Windows can’t delete the folder as its name is too long. To solve this, install RimRaf:

    npm install rimraf -g
    
    rimraf node_modules
    
    0 讨论(0)
  • 2020-12-02 04:35

    The best article I found about it is this one: https://trilon.io/blog/how-to-delete-all-nodemodules-recursively

    All from the console and easy to execute from any folder point.

    But as a summary of the article, this command to find the size for each node_module folder found in different projects.

    find . -name "node_modules" -type d -prune -print | xargs du -chs
    

    And to actually remove them:

    find . -name 'node_modules' -type d -prune -print -exec rm -rf '{}' \;
    

    The article contains also instructions for windows shell.

    0 讨论(0)
  • 2020-12-02 04:37

    Remove/Edit the packages that are not needed in package-lock.json (package names will be written under dependencies & devDependencies) and then

    npm install

    0 讨论(0)
  • 2020-12-02 04:38

    Have you tried npm prune?

    it should uninstall everything not listed in your package file

    https://npmjs.org/doc/cli/npm-prune.html

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