简体   繁体   中英

Installing a Node.js app from GitHub without installing to node_modules

I'm trying to setup our Node.js server app so that we can automate updates to the app when there are pushes / releases on GitHub. At the moment, part of the app provides a server which listens for GitHub events and will fetch the new version from GitHub and replace the current version, using recluster to restart the main server app when the new version is installed. The issue I'm having is that I figured I could use npm install with a GitHub URL to download the new version and its dependencies. The problem is that the app is downloaded to a subdirectory of node_modules, so it's installed alongside the rest of the modules, instead of in the root, like you'd normally have a Node.js app. Ie, instead of the usual:

/
|-index.js (the main server app)
|-deploy.js (the wrapper which runs index.js using recluster and listens for GitHub webhooks to update the app)
|-package.json
|-node_modules/
    |-module1
    |-module2
    |-etc.

Doing npm install git://github.com/user/repo installs the app like this:

/
|-node_modules/
    |-app_name/
    |     |-index.js
    |     |-deploy.js
    |     |-package.json
    |-module1
    |-module2
    |-etc

What's the correct way to get what I want? Ie install / update a Node.js app from GitHub. For the updating, the deploy.js is currently set up to run the npm install command for the latest commit into a temporary folder, then on success it deletes the code in the current folder and copies the new code from the temporary folder. I'm also not sure if this is the best way to do it, so any pointers here would be good too.

I would probably go for a git pull && npm update approach. npm install git://github.com/user/repo will probably fetch your source code and all of its dependencies from scratch each time. On the other hand, git pull && npm update will only fetch the changes made on your code, and will install / update the dependencies that have changed. So it seems a more efficient way to self-update, and you won't have the node_modules subdirectory issue.

You can take a look at how hubot self-updates to get an idea on how to do it: https://github.com/github/hubot-scripts/blob/master/src/scripts/update.coffee

However, a drawback of this approach is that if you push a buggy update and the server cannot start after it self-updates, it will get stuck at that version as it will not be able to self-update anymore.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM