Storing multiple NPM libraries in single Git repo

2019-08-28 21:49发布

问题:

We can reference NPM deps on Github like so:

"dependencies":{
   "foo":"github.com/org/root#commit"
}

But I have a Git repo with multiple NPM libraries in it, like so:

root/
  nodejs/
    foo/
      package.json
    bar/
      package.json

is there a way to install foo from this Github repo directly? Something like this?

"dependencies":{
   "foo":"github.com/org/root/nodejs/foo#commit"
}

I tried installing using that url, and it didn't work, I got this error:

npm ERR! code ENOPACKAGEJSON
npm ERR! package.json Non-registry package missing package.json: https://raw.githubusercontent.com/org/root/master/nodejs/foo/package.json.
npm ERR! package.json npm can't find a package.json file in your current directory.

npm ERR! A complete log of this run can be found in:
npm ERR!     /home/oleg/.npm/_logs/2018-12-05T09_15_28_666Z-debug.log

I also tried using raw.githubusercontent.com like so:

"dependencies":{
   "foo":"raw.githubusercontent.com/org/root/nodejs/foo#commit"
}

And I got the same error. Surely this must be possible somehow?

回答1:

Ok so one solution is to use tarballs. So you can use this command:

 npm i -S 'https://raw.githubusercontent.com/org/root/master/nodejs/foo/foo-0.0.1001.tgz'

it worked..and I get this in package.json:

 "dependencies": {
    "foo": "https://raw.githubusercontent.com/org/root/master/nodejs/foo/foo-0.0.1001.tgz"
  }

basically all you need to do is npm install with a url that points to a tarball.