Offline modules in Nodejs with npmbox

1

I know that it is possible to manage the modules of the node while being offline through the npmbox. But how do I do that? Do I need to download the modules before? Do I need to install them in a directory other than the project?

    
asked by anonymous 02.02.2015 / 10:00

1 answer

0

The npmbox comes with two executables:

  • npmbox <nome-do-pacote> <nome-de-outro-pacote>…
  • npmunbox <nome-do-box>

Creating .npmbox files with NPMBOX

To create a bundle of a package, with all its built-in dependencies, you must execute:

npmbox <pacote>

For example:

npmbox express

Search for all expressions dependencies and combine them into a express.npmbox file in the current directory.

You can pass multiple package names at the same time and multiple .npmbox files will be created, one for each package.

Using .npmbox files with NPMUNBOX

Once you have a .npmbox file, run:

npmunbox <box>

It will extract the box into the node_modules folder in the current directory. For example, if we run:

npmbox express
# ^ O arquivo express.npmbox é criado 
npmunbox express.npmbox
# ^ O arquivo express.npmbox é extraído e o express é instalado no node_modules

We will see that a node_modules directory is created, with express installed inside it.

Automating the creation / extraction of boxes for a local package

There is no way today to create a single box with multiple packages, nor to create a box for an entire application automatically. This is not too difficult to fix. Here is a script that would generate a box for each dependency in a project:

#!/usr/bin/env node
var child_process = require('child_process');
var fs = require('fs');
var path = require('path');

var pkgJson = require(
  process.argv[2] ||
  path.join(process.cwd(), 'package.json')
);

var pkgName = pkgJson.name || 'package';
var pkgDeps = Object.keys(pkgJson.dependencies || {})
  .concat(Object.keys(pkgJson.devDependencies || {}));
console.log(pkgDeps);

var targetDir = path.join(process.cwd(), pkgName + '-npmboxes');

if(!fs.existsSync(targetDir)) {
  fs.mkdirSync(targetDir);
}

var c = child_process.spawn('npmbox', pkgDeps, {
  stdio: 'inherit',
  cwd: targetDir,
});

c.on('close', function(code) { process.exit(code); });

It could be used as follows:

./script.js <localização de um package.json>

What would create a <nome-do-pacote-no-package.json>-npmboxes directory with all the boxes for this package. Unpacking them later would not be much harder than:

cd <nome-do-pacote-no-package.json>-npmboxes
for b in *; do npmunbox $b; done
    
24.02.2015 / 18:09