npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

dynavers

v0.3.1

Published

Install multiple versions of a module and switch between them

Downloads

134

Readme

npm version Dependency Status Build status js-semistandard-style

NPM

Install multiple versions of a module then dynamically switch between them.

const setModuleVersion = require('dynavers')('dynavers.json')
const versions = ['1.13.1', '2.1.0-beta.24']
for(const version of versions) {
  setModuleVersion('webpack', version);
  // ...do stuff
}

As the above example hints at, this is primarily designed for testing.

Installation

npm install --save-dev dynavers

Usage

Specifying which versions to install

Create a JSON spec of modules to be installed, e.g. at dynavers.json:

{
  "path": "dynavers_modules",
  "versions": {
    "webpack": ["1.13.1", "2.1.0-beta.24"]
  }
}

Do not use fuzzy versions ("^0.16.0") - this will cause problems.

Installing the modules from npm

Next, run

./node_modules/.bin/install-module-versions dynavers.json

In this example, it will create dynavers_modules and install webpack 1.13.1 and 2.1.0-beta.24 inside the dynavers_modules directory.

To run the install automatically before your test suite (when you run npm test), add it as a "pretest" hook to your package.json:

{
  "scripts": {
    "pretest": "install-module-versions dynavers.json",
    "test": "..."
  }
}

install-module-versions will not redownload existing modules. If something went wrong, delete its directory first: rm -r dynavers_modules

setModuleVersion: Specifying module version

The API is a single function setModuleVersion(moduleName, moduleVersion). This will ensure any subsequent require calls to that module from anywhere will use the specified version. Note this will also ensure any exports within that module will also use the specified version:

const setModuleVersion = require('dynavers')('dynavers.json')
setModuleVersion('webpack', '1.13.1')
var webpack = require('webpack')          // version 1.13.1
var Chunk = require('weback/lib/Chunk')   // will also be the 1.13.1 version

Purging the module cache

Please read the Caveats below before opting to purge the cache

You may also want to purge the Module cache so that all modules are reloaded after you have set a version. This is easily done by adding an additional boolean argument:

const setModuleVersion = require('dynavers')('dynavers.json')
const purgeCache = true
setModuleVersion('webpack', '1.13.1', purgeCache)

Alternatively you can make this the default behaviour by adding the purgeCache value in the configuration:

{
  "path": "dynavers_modules",
  "versions": {
    "webpack": ["1.13.1", "2.1.0-beta.24"]
  },
  purgeCache: true
}

In this case, you need not specify it in the setModuleVersion call.

Caveats

  1. This works by monkey-patching node's Module load mechanism. You may not like this.
  2. The load mechanism has not been tried with all the exotic forms of package identifier that require will accept. Please let me know if any this fails.
  3. Think carefully about using the purgeCache option. In most cases it is a sledgehammer to crack a nut as it forces all future require'd modules to be reloaded. Moreover, and this is important, it will not clear any in-memory modules, so you run the risk of simutaneously running multiple versions of a module. Eek!

Alternatives

multidep offers an alternative that specifies the version for a single require call via its multidepRequire.

Acknowledgements

This tool is based on multidep - thanks Jo Liss!
(I would have submitted a Pull Request but by the time I had changed the external API and the core mechanism, I don't think it would have been accepted).

Change History

v0.3.x

  • windows support (thanks to @snadn)
  • updated dependencies
  • tests run node 4.x to 11.x

v0.2.x

  • add purge cache options

v0.1.x

  • initial release