Offline modules in Nodejs with npmbox

Asked

Viewed 166 times

1

I know it is possible to manage Node modules while offline through npmbox. But how do I do that? Is it necessary to download the modules first? You need to install them in a different project directory?

1 answer

0


The npmbox comes with two executables:

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

Creating files .npmbox with the NPMBOX

To create a package Bundle, with all its built-in dependencies, you must run:

npmbox <pacote>

For example:

npmbox express

Searches all express dependencies and combines them into one file express.npmbox in the current directory.

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

Using the files .npmbox with the NPMUNBOX

Once you have a file .npmbox, execute:

npmunbox <box>

Will extract the box box into the folder node_modules 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 verify that a directory node_modules is created, with the express installed inside it.

Automating the creation/extraction of boxes for a local package

There is no way today to create a single box box with multiple packages, or creating a box for an entire application automatically. This is not very difficult to fix. Follows a script that would generate a box box for each dependency on 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 directory <nome-do-pacote-no-package.json>-npmboxes with all the pits for that 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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.