arloliu / copy-node-modules

Fast deploy Node.js modules from local source folder to distination folder
26 stars 14 forks source link

copy-node-modules - fast Node.js modules deployment to destination directory

NPM version Downloads/month

Copy all modules listed in dependencies or/and devDependencies field of package.json to destination directory.

The procedure:

  1. Read package.json from source directory and read dependencies or devDependencies field.
  2. Search for existing modules and ther dependencies in source directory.
  3. Copy all modules to destination directory.

Modern applications use lots of modules, each module depends on more modules resulting in hundreds of modules being installed when typing npm install. This module will help you to save time from slow npm install when you want to pack/deploy your application to a directory which contains all needed modules.

It will save you a bunch of time to deploy a stand-alone application from existing development directory, no need to fetch all modules from remote repository.

Installation

yarn add copy-node-modules --dev

or

npm install copy-node-modules --save-dev

Programmatic Usage

ES6+ environment:

const copyNodeModules = require('copy-node-modules');

ES6+ environment with import support:

import copyNodeModules from 'copy-node-modules'; 

ES5

var copyNodeModules = require('copy-node-modules');

copyNodeModules(srcDir, dstDir, [options], callback)

Examples

const copyNodeModules = require('copy-node-modules');
const srcDir = '/somewhere/project';
const dstDir = '/somewhere/project/dist';
copyNodeModules(srcDir, dstDir, { devDependencies: false }, (err, results) => {
  if (err) {
    console.error(err);
    return;
  }
  Object.keys(results).forEach(name => {
    const version = results[name];
    console.log(`Package name: ${name}, version: ${version}`);
  });
});

Example with a filter method

const copyNodeModules = require('copy-node-modules');
const srcDir = '/somewhere/project';
const dstDir = '/somewhere/project/dist';

// Filter method that will ignore node_module folders in a node module
const filter = path => {
  const firstIndex = path.indexOf('node_modules');
  return v.indexOf('node_modules', firstIndex + 1) === -1;
}

copyNodeModules(srcDir, dstDir, { devDependencies: false, filter }, (err, results) => {
  if (err) {
    console.error(err);
    return;
  }
  Object.keys(results).forEach(name => {
      const version = results[name];
      console.log(`Package name: ${name}, version: ${version}`);
    });
});

CLI Usage

copy-node-modules src_dir dest_dir [-d|--dev] [-c|--concurrency] [-v|--verbose] [-f|--filter]

License

MIT