Skip to content

Instantly share code, notes, and snippets.

@jackgill
Last active March 25, 2024 13:53
Show Gist options
  • Star 47 You must be signed in to star a gist
  • Fork 20 You must be signed in to fork a gist
  • Save jackgill/7687308 to your computer and use it in GitHub Desktop.
Save jackgill/7687308 to your computer and use it in GitHub Desktop.
A node.js script to create a bundle containing an npm package, and all of its dependencies.
/*
* This script will download a package (and all of its dependencies) from the
* online NPM registry, then create a gzip'd tarball containing that package
* and all of its dependencies. This archive can then be copied to a machine
* without internet access and installed using npm.
*
* The idea is pretty simple:
* - npm install [package]
* - rewrite [package]/package.json to copy dependencies to bundleDependencies
* - npm pack [package]
*
* It is necessary to do this (intead of using pac) because when npm installs
* a module, it will actually strip out the node_modules folder from the
* tarball unless bundleDependencies is set.
*
* Author: Jack Gill (https://github.com/jackgill)
* Date: 11/27/2013
* License: MIT License (see end of file)
*/
var fs = require('fs');
var cp = require('child_process');
var path = require('path');
if (process.argv.length != 3) {
console.log("Usage: %s [package name]", process.argv[1]);
process.exit(0);
}
var packageName = process.argv[2];
// Copy dependencies to bundleDependencies
function rewritePackageJSON(fileName) {
var contents = fs.readFileSync(fileName);
var json = JSON.parse(contents);
if (json.dependencies) {
json.bundleDependencies = Object.keys(json.dependencies);
fs.writeFileSync(fileName, JSON.stringify(json, null, 2));
}
}
// Install the package from the online registry
cp.exec('npm install ' + packageName, function(err, stdout, stderr) {
console.log(stdout);
console.error(stderr);
if (err) {
console.log("Error executing npm install: ", err);
process.exit(0);
}
// Set bundleDependencies for the package
rewritePackageJSON(path.join('node_modules', packageName, 'package.json'));
// Create a new .tgz file which bundles all dependencies
cp.exec('npm pack ' + path.join('node_modules', packageName), function(err, stdout, stderr) {
console.log(stdout);
console.error(stderr);
if (err) {
console.log("Error executing npm pack: ", err);
}
else {
console.log("Bundled package " + packageName);
}
});
});
/*
The MIT License (MIT)
Copyright (c) 2013 Jack Gill
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
@thomson02
Copy link

Exactly what I was after. Thank you!

@vspassion007
Copy link

Great Work. Thanks

@InamTaj
Copy link

InamTaj commented Oct 30, 2017

Awesome!
I needed this to include crypto-js as a JS script to be loaded from local machine into the JSP page.

@dkblay
Copy link

dkblay commented Aug 17, 2018

How do I use this please? Thanks :)

@IKyriazis
Copy link

How do I use this please? Thanks :)

  1. Install Node.JS on your computer.
  2. Download this file (bundler.js)
  3. Navigate to the directory that you downloaded bundler.js to.
  4. Open a command prompt and execute the command node bundler.js <name of package you want downloaded> without the "<" and ">"

@milichev
Copy link

Thanks for the tool!

A small addition: to support the package argument with the semver specified, following changes to apply:

line 30:

var packageSpec = process.argv[2];
var packageName = packageSpec.split('@')[0];

line 43:

cp.exec('npm install ' + packageSpec, function(err, stdout, stderr) {

@sendev1
Copy link

sendev1 commented Jun 6, 2023

When I try node bundle.js forever it creates forever-4.0.3.tgz file and i ran node install forever-4.0.3.tgz -g and it said this

added 1 package, and audited 2 packages in 927ms

found 0 vulnerabilities```

when I try `forever -h` I get this error 

node:internal/modules/cjs/loader:936
throw err;
^

Error: Cannot find module 'eventemitter2'
Require stack:

  • C:\ProgramData\npm\npm\node_modules\forever\lib\forever.js
  • C:\ProgramData\npm\npm\node_modules\forever\bin\forever
    �[90m at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)�[39m
    �[90m at Function.Module._load (node:internal/modules/cjs/loader:778:27)�[39m
    �[90m at Module.require (node:internal/modules/cjs/loader:1005:19)�[39m
    �[90m at require (node:internal/modules/cjs/helpers:102:18)�[39m
    at Object. (C:\ProgramData\npm\npm\node_modules\�[4mforever�[24m\lib\forever.js:11:17)
    �[90m at Module._compile (node:internal/modules/cjs/loader:1105:14)�[39m
    �[90m at Object.Module._extensions..js (node:internal/modules/cjs/loader:1159:10)�[39m
    �[90m at Module.load (node:internal/modules/cjs/loader:981:32)�[39m
    �[90m at Function.Module._load (node:internal/modules/cjs/loader:822:12)�[39m
    �[90m at Module.require (node:internal/modules/cjs/loader:1005:19)�[39m {
    code: �[32m'MODULE_NOT_FOUND'�[39m,
    requireStack: [
    �[32m'C:\ProgramData\npm\npm\node_modules\forever\lib\forever.js'�[39m,
    �[32m'C:\ProgramData\npm\npm\node_modules\forever\bin\forever'�[39m
    ]
    }```

looks like dependencies are included/installed. Any help would be appreciated.

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