node package manager
Easy collaboration. Discover, share, and reuse code in your team. Create a free org »

dynamic-import-support

dynamic-import-support

enable dynamic imports in node.js

import('./whatever').then(function (exports) {
  console.log(exports)
})

npm travis standard

Install

npm install dynamic-import-support

Usage

Use dynamic-import-support/register to enable import() in all modules loaded after the current one:

require('dynamic-import-support/register')

Use dynamic-import-support to transform some source code containing import() calls:

var dynamicImport = require('dynamic-import-support')
 
dynamicImport(`
  import('./whatever').then(function (exports) {
    console.log(exports)
  })
`) === `
function _import(p){return Promise.resolve().then(function(){return require(p)})}
 
  _import('./whatever').then(function (exports) {
    console.log(exports)
  })
`

It uses js-tokens instead of a full parser, so it's very quick. Some patterns aren't supported though. For example, import() inside a template string won't be transformed. If this is a problem, please open an issue and we'll figure out how to make it work :)

License

MIT