Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change the location of the node_modules folder

I'm currently trying to migrate an old ASP.NET WebSite project to Visual Studio 2015. I'd like to use NPM/Gulp to automatically compile LESS files to CSS (this task was done by WebEssentials in VS 2013).

I added a package.json file to the project to load the required components. This creates a node_modules folder in the root of the WebSite project, and this is where my problem starts:

Since WebSite projects don't have a project file, all files (and sub-directories) found in the project root folder, are automatically part of the project. Due to the deeply nested directory structure inside node_modules, this leads to errors because of too long path names.

An easy workaround is to set the hidden attribute on the node_modules folder (but this has to be done manually by each developer).

Is there a way to tell NPM to put the node modules into another directory e.g. one level above the project (..\node_modules) where the solution file is?

Or is it possible to set the hidden attribute on a folder from a gulp-task (which runs when the project is loaded)?

like image 542
M4N Avatar asked Oct 26 '15 15:10

M4N


People also ask

Can I move node_modules folder?

Yes you can copy whole node_modules (have done it multiple times) from one project to another and use same package. json and package-lock (Will only save time in dependencies installation/download)

Where is node_modules folder?

On Unix systems they are normally placed in /usr/local/lib/node or /usr/local/lib/node_modules when installed globally.

What is node_modules .bin folder?

The directory node_modules/.bin is where the binaries of the modules used by your project are stored, normally using symbolic links to the respective binaries in the corresponding module's directory.


2 Answers

Based on @Rik's answer, I was able to solve the problem:

Instead of adding the package.json and gulpfile.js into the WebSite project, I added them at the solution level (as solution items). This means, that the node_modules folder is now in the solution directory at the same level as the WebSite project(s).

The only other change was to modify the paths in gulpfile.js accordingly.

like image 68
M4N Avatar answered Oct 06 '22 11:10

M4N


You might want to check out npm 3.0+. It installs the modules in a maximally flat structure. It should reduce the paths lengths in the module directory.

From the release notes

Flat, flat, flat!

Your dependencies will now be installed maximally flat. Insofar as is possible, all of your dependencies, and their dependencies, and THEIR dependencies will be installed in your project's node_modules folder with no nesting. You'll only see modules nested underneath one another when two (or more) modules have conflicting dependencies.

  • #3697 This will hopefully eliminate most cases where windows users ended up with paths that were too long for Explorer and other
    standard tools to deal with.
  • #6912 (#4761 #4037) This also means that your installs will be deduped from the start.
  • #5827 This deduping even extends to git deps.
  • #6936 (#5698) Various commands are dedupe aware now.

This has some implications for the behavior of other commands:

  • npm uninstall removes any dependencies of the module that you specified that aren't required by any other module. Previously, it
    would only remove those that happened to be installed under it,
    resulting in left over cruft if you'd ever deduped.
  • npm ls now shows you your dependency tree organized around what requires what, rather than where those modules are on disk.
  • #6937 npm dedupe now flattens the tree in addition to deduping.

https://github.com/npm/npm/releases/tag/v3.0.0

For upgrading the windows installation check out this package npm-windows-upgrade

like image 41
Thomas Andersen Avatar answered Oct 06 '22 11:10

Thomas Andersen