3

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?

Andrey Mikhaylov - lolmaus
  • 23,107
  • 6
  • 84
  • 133
  • 1
    Don't fight the system. Installing dependencies locally is a very good thing, and [it's done for good reasons](https://npmjs.org/doc/faq.html#Why-can-t-npm-just-put-everything-in-one-place-like-other-package-managers). – josh3736 Oct 02 '13 at 16:45

2 Answers2

3

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.

AlexMA
  • 9,842
  • 7
  • 42
  • 64
  • 2
    Unfortunately Dropbox dereferences symlinks instead of just syncing the symlink, so this doesn't solve the problem. – Daniel Lucraft Feb 24 '14 at 13:58
  • 1
    That's really a Dropbox-specific problem (albeit the question does mention it, though it's more about saving space). I suppose you could whip together a script which makes Dropbox behave properly/relinks the files automatically. – AlexMA Feb 24 '14 at 14:32
  • It's two pieces of software that don't play well together :) I've been enjoying my automatic and pain-free syncing between machines for a while now, and I don't want to give it up! Looks like I'm going to have to to work with Node though. – Daniel Lucraft Feb 24 '14 at 14:40
3

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.

josh3736
  • 139,160
  • 33
  • 216
  • 263