Search code examples
javascriptnode.jsamdinternnode-modules

What does the "!" character do in nodejs module names?


I've started using the intern library to write functional tests in js, and I realized that I couldn't understand this syntax:

var assert = require('intern/chai!assert');
var registerSuite = require('intern!object');

What is the purpose of the ! character in the argument of the require() method?


Solution

  • Short answer

    It identifies a resource that is part of a plugin.
    The structure of the identifier is: [plugin]![resource].

    Long answer

    In the documentation, you can find that:

    Intern is built on top of a standard amd loader, which means that its modules are also normally written in the AMD module format.

    That's why and how the require function is actually injected, so it's clear that you are not using the require module provided along with Node.JS.

    It states also that:

    [AMD format] Allows modules and other assets to be asynchronously or conditionally resolved by writing simple loader plugins

    If you follow the link provided with the documentation when it cites the loaders, you find that:

    Loader plugins extend an AMD implementation by allowing loading of resources that are not traditional JavaScript dependencies.

    In particular, you can find that a dependency has the following form:

    [Plugin Module ID]![resource ID]
    

    It goes without saying that the default implementation of the loader you get by using intern adheres to the above mentioned standard.

    That said, it follows that, if we consider:

    intern/chai!assert
    

    You can read it as inter/chai as plugin module and assert as actually required resource.

    The purpose of the ! character in the argument of the require() method is to satisfy the requirements of the syntax used to identify a resource that is itself part of a plugin.