1.0.0 • Published 5 years ago

import-package v1.0.0

Weekly downloads
443
License
ISC
Repository
github
Last release
5 years ago

import-package

npm version Build Status Coverage Status

Import an npm package from the disk

const importPackage = require('import-package');

(async () => {
  const tar = importPackage('tar');
  //=> {create: [Function], update: [Function], extract: [Function], ...}
})();

Installation

Use npm.

npm install import-package

API

const importPackage = require('import-package');

importPackage(id)

id: string (a module ID)
Return: Promise<any>

It imports a module with the given module ID from either of the following directories:

  1. node_modules in the current working directory
  2. node_modules in the directory where npm CLI is installed

If the module ins't installed in CWD but included in the npm CLI dependencies, it imports the module from npm CLI directory.

// $ npm ls npm-packlist
// > └── (empty)

(async () => {
  // However, npm CLI contins `npm-packlist` package.
  const npmPacklist = await importPackage('npm-packlist'); //=> {[Function: walk], sync: [Function: walkSync], ...}
})();

If the module is not included in the npm CLI dependencies but installed in CWD, it imports the module from CWD.

// $ npm ls eslint
// > └── eslint@5.9.0

(async () => {
  // npm CLI doesn't contain `eslint` package.
  const eslint = await importPackage('eslint'); //=> {Linter: [Function: Linter], ...}
})();

If the module exists in both directories, it compares their package versions and imports the newer one.

// $ npm ls tar
// > └── tar@0.1.0

(async () => {
  // Loaded from the npm CLI directory as the CWD version is older
  const tar = await importPackage('tar');
})();

The returned Promise will be fulfilled with the imported module, or rejected when it fails to find the module.

Each imported modules are stored in the internal cache and they will be loaded faster at the next time on.

importPackage.preload(id)

id: string (a module ID)
Return: boolean (whether the package is already imported or not)

Try to find the given package and store it to the internal cache when it exists.

This method is useful when a user knows which package will be needed and want it to be loaded faster when it's actually imported.

const importPackage = require('import-package');

module.exports = function main() {
  const uuid = await importPackage('uuid');
  return uuid();
}

// The author know `uuid` package needs to be imported in the future.
importPackage.preload('uuid');

License

ISC License © 2018 Shinnosuke Watanabe