43

I need to know if the following behavior is normal.

When I npm install, each package from my package.json and the dependencies, don't get installed nested anymore, but each dependency is installed in the node_modules directory. That makes my node_modules directory blown and look like this:

Screenshot of node_modules directory

This happened since I updated npm and node.

Now I run:

npm -v 3.3.6
node -v 4.2.1
python 2.7
windows 7
wamp

My package.json file looks like this:

{
  "private": true,
  "devDependencies": {
    "gulp": "^3.8.8"
  },
  "dependencies": {
    "laravel-elixir": "^3.0.0",
    "bootstrap-sass": "^3.0.0"
  }
}

It's the standard laravel package.json file.

Is there a way to have nested directories again, because I don't like such a blown article with over 100 sub directories.

2 Answers 2

61

Update: As Erik Pukinskis mentioned in the comments: As of npm 3.5, support for --legacy-bundling has been dropped.


Yes, there is a way to have nested directories again by changing npm's (version 3 as of this writing) default behaviour:

  1. Delete the currently present node_modules folder.

  2. Tell npm to install with legacy bundling for this one install:

    npm install --legacy-bundling

A "permanent" alternative:

  1. Set your npm config to always use legacy bundling...

    npm set legacy-bundling=true

  2. .. and run as usual:

    npm install

Note: fetching dependencies with legacy bundling will take a lot more time because many several different versions of the same dependencies will be installed.

Disclaimer: As a non-Windows user I have no need for flat dependencies and want to find self-declared dependencies with ease in favour of automatic deduping. Since installing npm dependencies without legacy bundling already takes an incredible amount of time I'm usually willing to spend those extra minutes install time. It gets back down to 5 directories from previously 700+ (...) in a Laravel Elixir setup with bootstrap (non-sass), font-awesome and jquery added.

4
  • 2
    This answer was highly useful to me for my specific case. Describing here for posterity: I have been trying to build package.json with dependencies for an existing node project. using npm install <list of packages I require> --legacy-bundling --save wrote only the direct dependencies into package.json. I then did rm -rf node_modules && npm install, to get back the new flat node_modules directory. This is the best of both worlds for me.
    – Reed Spool
    Jul 18, 2016 at 18:25
  • 3
    I also consider this the correct answer, as it directly answers OP in the affirmative
    – Reed Spool
    Jul 18, 2016 at 19:05
  • 2
    As of npm 3.5, support for --legacy-bundling has been dropped. Feb 10, 2018 at 22:32
  • I can this option it in npm docs and github repository github.com/npm/cli/blob/…
    – guillem
    Oct 16, 2020 at 22:00
36

That's the new behavior of npm 3 as per this npm blog.

4
  • Thanks sagie, I guess there is no way to make it nested again to avoid a bloated directory, is there? (Also have to wait 5 min, before being able to accept your answer as the correct one) Oct 15, 2015 at 17:35
  • 9
    Flat is better as I see it. As a windows user, it is really horrible to delete too much nested modules as it passes the 256 max charaters really fast (or was it 1024,can't remember). Also i helps when you upgrade modules as now it is able to better reuse existing installations and not replace everything under the module you upgraded as maybe its dependencies didn't change. bottom line, its much faster and cleaner this way.
    – sagie
    Oct 15, 2015 at 17:38
  • I kept trying to troubleshoot NPM lol...glad I ran across this or I would still be hunting for a solution =)
    – afreeland
    Jan 11, 2016 at 14:04
  • 1
    @LoveAndHappiness In fact, there is. See my answer.
    – luchaos
    Feb 5, 2016 at 15:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.