Git Product home page Git Product logo

copy-node-modules's Introduction

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)

  • srcDir: source directory containing package.json file.

  • dstDir: destination directory to copy modules to (modules will be copied to dstDir/node_modules directory).

  • options:

    • devDependencies: boolean value, defaults to false, showing whether modules in devDependencies field of package.json should also be copied (when it's set to true).
    • concurrency: integer value, max number of root packages whose files are being copied concurrently.
    • filter: RegExp or function that accepts one value (the full path) and returns a boolean (copy on true).
  • callback(err, results): A callback which is called when all copy tasks have finished or error occurs, results is an array contains copied modules, each item is an object as {name: 'xxx', version: 'xxx'}

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]
  • src_dir: source directory containing package.json file.
  • dest_dir: destination directory to copy modules to (modules will be copied to dest_dir/node_modules directory).
  • -d|--dev: whether modules in devDependencies field of package.json should be also copied.
  • -c|--concurrency: max number of root packages whose files are being copied concurrently.
  • -v|--verbose: verbose mode.
  • -f|--filter: regular Expression, files that match this expression will be copied; it also matches directories fi: -f index.html matches path/index.html but not path/ and because of this index.html is not copied.

License

MIT

copy-node-modules's People

Contributors

ezze avatar arloliu avatar sirmomster avatar estaub avatar bessonov avatar drsensor avatar krzysztof-miemiec avatar mbjedk avatar sanmoo avatar dependabot[bot] avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.