Installing project's dependencies globally and running the project without `node_modules/` — how?

StackOverflow https://stackoverflow.com/questions/19141614

  •  30-06-2022
  •  | 
  •  

Question

I decided to try learning DerbyJS and this is my first acquaintance with NodeJS either.

I create a new Node/Derby project with derby new foo. This also creates a node_modules/ folder which contains a copy of all packages the project depends on.

The node_modules/ subdir of a blank Derby project is 144 MB large and contains 12967 files (sic!). As a person familiar with Ruby's RubyGems, RVM and Bundler, i find this insane. I can't express how wrong it is (actually i've got some solid argumentation against that craziness but StackOverflow is not a place for debate).

I thought that npm's -g flag would help me. I could install all packages globally, i told myself. So i did:

derby new -n foo
cd foo
sudo npm install -g

Now my project weighs 152 KB and contains 24 files. Now that's reasonable.

But i fail to run it. When i do npm start, i get "Cannot find module 'express'":

lolmaus@sandy:~/hello_derby2$ npm start

> hello_derby2@0.0.0 start /home/lolmaus/hello_derby2
> node server.js

Master pid  29884

module.js:340
    throw err;
    ^
Error: Cannot find module 'express'
    at Function.Module._resolveFilename (module.js:338:15)
    at Function.Module._load (module.js:280:25)
    at Module.require (module.js:364:17)
    at require (module.js:380:17)
    at Object.<anonymous> (/home/lolmaus/hello_derby2/lib/server/index.js:1:77)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Module.require (module.js:364:17)
    at require (module.js:380:17)

So the question is: how do install project's dependencies into a central local repository and run the project without beating the f#@k out of my Dropbox account?

Was it helpful?

Solution

Check out this link to npm's faq. Basically, you want to use the npm-link command. Go through your package.json and, for each dependency, do a sudo npm install -g <packagename>. Then link that package to your local project( see npm help link ).

The reason this is not the default behavior is that managing dependencies for multiple projects is a headache. Space is assumed to be cheap (and it is); having copies of dependencies is considered a low price to pay for fewer package version conflicts.

OTHER TIPS

If you want to keep your project in Dropbox, I'd create a bare git repo in Dropbox and use it as the upstream repo for your project.

Run your project out of a non-Dropbox folder, and add the node_modules folder to .gitignore.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top