Ember CLI Version Checker

5.1.2 · active · verified Wed Apr 22

The `ember-cli-version-checker` package is a fundamental utility for Ember CLI addon authors. It provides a robust mechanism to programmatically determine the installed versions of NPM or (historically) Bower packages within a consuming Ember application's dependency tree. This allows addons to implement conditional logic, such as providing different features, templates, or polyfills, based on the host application's Ember CLI or Ember.js version, thus ensuring broad compatibility. The current stable version is 5.1.2, with releases occurring on an as-needed basis for bug fixes and minor enhancements rather than a fixed cadence. Its core differentiator lies in its deep integration with the Ember CLI project context, simplifying complex semver comparisons for specific dependencies.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates initializing `VersionChecker` within an Ember CLI addon's hooks, asserting minimum dependency versions, and conditionally loading resources based on the host application's Ember.js version.

const path = require('path');
const VersionChecker = require('ember-cli-version-checker');

// This code would typically reside in an Ember CLI addon's index.js file
// inside the module.exports object.
module.exports = {
  name: 'my-awesome-addon',

  init() {
    this._super.init.apply(this, arguments);
    const checker = new VersionChecker(this.project);

    // Assert a minimum Ember CLI version
    checker.for('ember-cli').assertAbove('2.13.0', '`my-awesome-addon` requires Ember CLI >= 2.13.0');

    const emberDep = checker.for('ember');
    if (!emberDep.exists()) {
      console.warn('Ember.js not found in project dependencies. This addon may not function correctly.');
    }

    console.log(`Ember version: ${emberDep.version || 'N/A'}`);
  },

  treeForAddonTemplates(tree) {
    const checker = new VersionChecker(this.project);
    const emberDep = checker.for('ember');

    const baseTemplatesPath = path.join(this.root, 'addon/templates');

    // Conditionally provide templates based on Ember version
    if (emberDep.satisfies('>= 3.0.0')) {
      return this.treeGenerator(path.join(baseTemplatesPath, 'modern'));
    } else if (emberDep.satisfies('>= 2.0.0')) {
      return this.treeGenerator(path.join(baseTemplatesPath, 'classic'));
    } else {
      // Fallback for older Ember versions or non-existent Ember
      return this.treeGenerator(path.join(baseTemplatesPath, 'legacy'));
    }
  }
};

view raw JSON →