share node_modules directory between multiple projects

Use pnpm instead of npm.

From the pnpm project's website:

pnpm uses hard links and symlinks to save one version of a module only ever once on a disk. When using npm or Yarn for example, if you have 100 projects using the same version of lodash, you will have 100 copies of lodash on disk. With pnpm, lodash will be saved in a single place on the disk and a hard link will put it into the node_modules where it should be installed.

  1. To install using npm in a command window use:

     npm install -g pnpm
    
  2. To update your existing package installations (and all subdirectories) use:

     pnpm install --recursive
    

or, use the shortcut command for recursive install:

    pnpm i -r

Use pnpm anywhere you would typically use npm. (This commands safely falls back to npm functions when not supported by pnpm.


Inorder to get npm to install in a directory of custom location,you need to run:

npm install --prefix path_to_node_modules_location

Edit : it is not possible to have the local folder without the package.json with it.

The only 'common location' for all projects is the global one. The global location contains all common packages and local contains specific ones for the project.

However, I am not sure it is such a good idea to have a common local node_modules folder for different projects as you end up with having to make sure their dependencies dont clash because of difference in package versions. It would mean trying to maintain package.json for multilple projects.

In order to configure webpack to look at new location check github link here

The loaders here are resolved relative to the resource which they are applied to. This means they are not resolved relative to the configuration file. If you have loaders installed from npm and your node_modules folder is not in a parent folder of all source files, webpack cannot find the loader. You need to add the node_modules folder as an absolute path to the resolveLoader.root option. (resolveLoader: { root: path.join(__dirname, "node_modules") })