Skip to content

Instantly share code, notes, and snippets.

@jpommerening
Created June 28, 2013 23:14
Show Gist options
  • Save jpommerening/5888867 to your computer and use it in GitHub Desktop.
Save jpommerening/5888867 to your computer and use it in GitHub Desktop.
Bundle any node package into a standalone package. At work, we need to be able to install node modules without network access. I decided to write a tiny script that bundles any npm package and all its dependencies into a TGZ that can be installed without access to the npm registry. Usage: npmbundle <package> It's just a quick hack, but it does t…
#!/usr/bin/env sh
set -e
tmpdir=`mktemp -d -t bundle`
file="package.json"
pushd "$tmpdir"
cat > "$file" <<EOF
{
"name": "bundle",
"version": "0.0.1"
}
EOF
touch README.md
npm install --save "$1"
json=`cat "$file"`
pkg=`node <<EOF
var json=$json;
process.stdout.write(Object.keys(json.dependencies)[0]);
EOF
`
pushd "node_modules/$pkg"
node <<EOF
var fs = require('fs');
var json = JSON.parse(fs.readFileSync('$file'));
json.bundleDependencies = Object.keys(json.dependencies);
fs.writeFileSync('$file', JSON.stringify(json));
EOF
bundle=`npm pack`
popd
popd
mv "$tmpdir/node_modules/$pkg/$bundle" "$bundle"
echo
echo "created $bundle"
rm -rf "$tmpdir"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment