Skip to content

Sharing node_modules folder with multiple projects #681

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
eliasuardi opened this issue Jun 19, 2017 · 30 comments
Closed

Sharing node_modules folder with multiple projects #681

eliasuardi opened this issue Jun 19, 2017 · 30 comments

Comments

@eliasuardi
Copy link

Hello.

As the title says I was wondering if it were possible to use the same modules folder for multiple projects so that I don't have to continuously install them using the command prompt.

Is it possible? If so how do I proceed?

Thanks.
Elia

@chungngoops
Copy link

chungngoops commented Jun 19, 2017

Yes, it possible>
I found the info in Node's document:
https://nodejs.org/api/modules.html#modules_loading_from_node_modules_folders

If the module identifier passed to require() is not a core module, and does not begin with '/', '../', or './', then Node.js starts at the parent directory of the current module, and adds /node_modules, and attempts to load the module from that location. Node will not append node_modules to a path already ending in node_modules.

If it is not found there, then it moves to the parent directory, and so on, until the root of the file system is reached.

For example, if the file at '/home/ry/projects/foo.js' called require('bar.js'), then Node.js would look in the following locations, in this order:

/home/ry/projects/node_modules/bar.js
/home/ry/node_modules/bar.js
/home/node_modules/bar.js
/node_modules/bar.js
This allows programs to localize their dependencies, so that they do not clash.

It is possible to require specific files or sub modules distributed with a module by including a path suffix after the module name. For instance require('example-module/path/to/file') would resolve path/to/file relative to where example-module is located. The suffixed path follows the same module resolution semantics.

@eliasuardi
Copy link
Author

So how should I structure the scripts element in the package.json of my projects?
This is one of them:

{
  "name": "stateless-functional-components",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
      "build": "webpack",
      "start": "webpack-dev-server"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "babel-core": "^6.25.0",
    "babel-loader": "^7.0.0",
    "babel-preset-es2015": "^6.24.1",
    "babel-preset-react": "^6.24.1",
    "react": "^15.6.1",
    "react-dom": "^15.6.1"
  },
  "devDependencies": {
    "html-webpack-plugin": "^2.28.0",
    "webpack": "^2.6.1",
    "webpack-dev-server": "^2.4.5"
  }
}

I tried executing npm run build but I get this error:

> webpack

'webpack' is not recognized as an internal or external command,
operable program or batch file.
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] build: `webpack`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] build script.
npm WARN Local package.json exists, but node_modules missing, did you mean to install?

@chungngoops
Copy link

I think you should create a project structure like this:

-Projects
--node_modules
--myproject1
---sub-project
--myproject2

then you install your npm package by run npm install with --save arg then it will be automatically append to your package.json.

@eliasuardi
Copy link
Author

eliasuardi commented Jun 19, 2017

Thank you for the response.
So what I've done is:

  1. execute npm init -y in my project folder
  2. added manually dependencies to package.json:
  • babel-core
  • babel-loader
  • babel-preset-es2015
  • babel-preset-react
  • react
  • react-dom
  • html-webpack-plugin
  • webpack
  • webpack-dev-server
  1. executed npm install --save arg
    I've noticed that all it adds to the package.json is "arg": "0.0.1".

After these steps I still get the error.
Is there something I'm doing wrong?

Thanks.
Elia

@chungngoops
Copy link

I think you misunderstood me.
should be like this:

  1. run npm init
  2. install your npm packages, like npm install --save webpack

then it should work.

@eliasuardi
Copy link
Author

eliasuardi commented Jun 19, 2017

Oh sorry about that.
I tried that but npm install --save <package> creates a local node_modules directory even though there is one in the parent directory.
So now my project structure is:

-Codecademy
--node_modules
--project1
---node_modules

@chungngoops
Copy link

chungngoops commented Jun 19, 2017

For me, I put the file package.json for all project1, project2.. under Codecademy, then install npm packages, so when I run npm install --save <npm-package> it creates node_modules under Codecademy folder only.

-Codecademy
--node_modules
--package.json
--project1
---app.js

@Opty1712
Copy link

Opty1712 commented Dec 21, 2017

chungngoops

How do you execute npm run xxxx inside project1?
I've installed node_modules in upper dir, but when I call npm run dev I get an error: can not find module 'webpack'

@ORESoftware
Copy link

You could also use symlinks, using npm link, etc.

@Opty1712
Copy link

as far as I know - npm link is only for 1 package, but I talk about folder with ~1000 modules...

@ORESoftware
Copy link

ORESoftware commented Dec 21, 2017

say you have this:

 HOME/
   projects/
     node_modules/
     project_a/package.json
     project_b/package.json
     project_c/package.json

The best thing to do is use NODE_PATH env variable to include the node_modules directory in the projects folder. It's simple:

cd "$HOME/projects/project_a"
NODE_PATH="$HOME/projects/node_modules"  node app.js
cd "$HOME/projects/project_b"
NODE_PATH="$HOME/projects/node_modules"  node app.js
cd "$HOME/projects/project_c"
NODE_PATH="$HOME/projects/node_modules"  node app.js

try that. NODE_PATH is an env variable that's empty by default. If NODE_PATH is not empty, it's safer to do:

NODE_PATH="$NODE_PATH:$HOME/projects/node_modules" node app.js

@ORESoftware
Copy link

ORESoftware commented Dec 21, 2017

that being said, babel dependencies should be shared by default.

see # 15 here:
https://medium.com/@the1mills/node-js-wicked-tricks-from-the-oss-battlefield-a9e71c5c791d

Given the directory structure I mentioned above, Babel will reach up the directory tree and will be able to see:

$HOME/projects/node_modules

@ORESoftware
Copy link

screenshot 2017-12-21 16 00 57

@Opty1712
Copy link

ORESoftware thank you for the assistance but may be it is not working under windows?

My structure is:

c:\WORK\tmp\test_folder\node_modules\
c:\WORK\tmp\test_folder\project1\package.json
c:\WORK\tmp\test_folder\project2\package.json

I've tried following:

LOCATION: sbt-edunov-ab@SBT-C0APQD8 MINGW64 /c/WORK/tmp/test_folder/project1
COMMAND: $ NODE_PATH="/C/Work/tmp/test_folder/node_modules" echo $NODE_PATH
RESULT: /C/Work/tmp/test_folder/node_modules

cd /C/Work/tmp/test_folder/node_modules - works good, it seems there are no problems with PATH

LOCATION: sbt-edunov-ab@SBT-C0APQD8 MINGW64 /c/WORK/tmp/test_folder/project1
COMMAND: $ NODE_PATH="/C/Work/tmp/test_folder/node_modules"  node ./webpack/configs/dev.middleware.js
RESULT:  module.js:471
    throw err;
    ^

Error: Cannot find module 'webpack'
    at Function.Module._resolveFilename (module.js:469:15)
    at Function.Module._load (module.js:417:25)
    at Module.require (module.js:497:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (C:\WORK\tmp\test_folder\project1\webpack\configs\dev.
middleware.js:3:15)
    at Module._compile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)

@ORESoftware
Copy link

The syntax for setting env variables on windows is different

try

set NODE_PATH=x & node foo.js

something like that

@ORESoftware
Copy link

I recommend downloading git-bash for Windows

https://git-scm.com/downloads

then you will have a bash command line on Windows

then you can use the original command

NODE_PATH=x node foo.js

@Opty1712
Copy link

I already use git bash, not CMD.
I tried different variants, but all of them returns the same error. That is strange

@ORESoftware
Copy link

humma

@ORESoftware
Copy link

maybe try windows shell then

@Opty1712
Copy link

Tried with SET in windows CMD but got the same error(

@the1mills
Copy link

Hmm i will take a look at it tomorrow

@Opty1712
Copy link

the1mills
Have you had a time to check it?

@ORESoftware
Copy link

@Opty1712 what exactly are you trying to do?

@Opty1712
Copy link

I want to have node-modules in the parent dir of of my identical projects
Just as described upper #681 (comment)

@ORESoftware
Copy link

@Opty1712 I created an example project for you

clone this: https://github.com/ORESoftware/node_path_example_project

follow the instructions in the readme file, let me know if it works for you

@Opty1712
Copy link

thank you, your project works in git bash under windows!
but your files do'nt work with my projects. I'll try to understand why, it seems that this is my local problem

@ORESoftware
Copy link

yeah I created those files on a mac

@vijaysridhara
Copy link

I just added a video on creating multiple projects under one workspace, mostly works for learning.
https://youtu.be/sGn453rC78g

@trev-dev
Copy link

trev-dev commented Sep 8, 2021

I was searching for a solution and found this: https://docs.npmjs.com/cli/v7/using-npm/workspaces - which may not have been available in 2018. Maybe it'll be good news for anyone here who hasn't figured this out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants