Skip to content

Instantly share code, notes, and snippets.

@nolanlawson
Last active September 20, 2016 05:01
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save nolanlawson/83ba5862bd719925d9cd to your computer and use it in GitHub Desktop.
Save nolanlawson/83ba5862bd719925d9cd to your computer and use it in GitHub Desktop.
Setting up local-npm as a launch daemon on OS X

Setting up local-npm as a launch daemon on OS X

Update! There is now a script to do these steps for you.

These instructions will set up local-npm so that it runs as a launch daemon, meaning that it will start up whenever you log in.

First, install local-npm and pm2:

npm install -g local-npm
npm install -g pm2

Then choose a directory to store all the local-npm data:

mkdir ~/.local-npm

Next, make a plist file and save it as ~/Library/LaunchAgents/com.nolanlawson.localnpm.plist:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
  <dict>
    <key>Label</key>
    <string>com.nolanlawson.localnpm</string>
    <key>ProgramArguments</key>
    <array>
      <string>/Users/YOUR_USERNAME/.local-npm/run.sh</string>
    </array>
    <key>RunAtLoad</key>
    <true/>
    <key>KeepAlive</key>
    <false/>
    <key>StandardErrorPath</key>
    <string>/tmp/localnpm.err</string>
    <key>StandardOutPath</key>
    <string>/tmp/localnpm.out</string>
    <key>UserName</key>
    <string>YOUR_USERNAME</string>
    <key>WorkingDirectory</key>
    <string>/Users/YOUR_USERNAME/.local-npm</string>
  </dict>
</plist>

Make sure to replace YOUR_USERNAME above with your username.

Create a run script and save it as ~/.local-npm/run.sh. I also use nvm to choose my Node version:

#!/usr/bin/env bash

export PATH=$PATH:/usr/local/bin

# optional: set up nvm
export NVM_DIR="/Users/YOUR_USERNAME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"  # This loads nvm
nvm use 4.0.0

pm2 start `which local-npm`

Make it runnable: chmod a+x ~/.local-npm/run.sh.

Now, restart your machine or run:

launchctl load -w ~/Library/LaunchAgents/com.nolanlawson.localnpm.plist

Done! you can see the local-npm logs by running:

pm2 logs

You can also see the PouchDB Server Fauxton UI at http://localhost:16984/_utils, which is pretty fun.

Now just set npm to use your local-npm registry:

npm set registry http://127.0.0.1:5080

Or you can use npmrc to manage this.

@jimpick
Copy link

jimpick commented Oct 4, 2015

It's also a good idea to go into Fauxton at http://localhost:16984/_utils and disable the Admin party.

@nolanlawson
Copy link
Author

@jimpick: Since PouchDB Server binds by default to 127.0.0.1, it's perfectly safe to run with the default settings. Unless you're worried someone will SSH tunnel into your machine, in which case it's already compromised. ;)

@saeidzebardast
Copy link

Thank you for this gist. I just updated it for Ubuntu users:
Setting up local-npm on OS X and Ubuntu

@IngwiePhoenix
Copy link

Would be nice to know how we can pass custom parameters - such as --remote - to local-npm using this demon. I had to google it, but it turns out pm2 allows that:

pm2 start `which local-npm` -- --remote http://npmjs.org

For whatever reason, using NPM over HTTPS and trying to punch 100+ requests to it, kills it O.o. That is why I need this flag.

@rex
Copy link

rex commented Jan 19, 2016

@nolanlawson This is EPIC. Thank you so much for this write up. Following these steps exactly got me up and running in ~2 minutes. You rock.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment