#4422·nightwatch

Loading custom commands: "unhandledRejection: There was an error while trying to load the file"

Author: thelyCreated Oct 28, 2025Updated Dec 6, 2025

Description of the bug/issue

This is another instance of #3699, but several years later. When I attempt to load any custom command, I get the (unhelpful) error message below. Just want to get some helper functions together to make my tests look a little less crazy! I've tried writing the tests as classes and as classless function exports, but I get the same error regardless.

I've tried reinstalling nightwatch and upgrading from 3.9 to 3.12; neither helped.

Steps to reproduce

  1. Create a commands folder, and put the custom command below in it.
  2. Run nightwatch command (see below).
  3. Error appears.

Example of custom command:

javascript
module.exports = class FlatpickrPickTime {
  async command() {
    const time = this.element.find(
      "[id='event.date_time.time_bounds.start_time']"
    );
    await time.click();

    await time.getNextElementSibling().find(".arrow-up").click();
  }
};

Sample test

node
describe('Ecosia.org Demo', function() {
  before(browser => browser.navigateTo('https://www.ecosia.org/'));

  it('Demo test ecosia.org', function(browser) {
    browser
      .waitForElementVisible('body')
      .assert.titleContains('Ecosia')
      .assert.visible('input[type=search]')
      .setValue('input[type=search]', 'nightwatch')
      .assert.visible('button[type=submit]')
      .click('button[type=submit]')
      .assert.textContains('.layout__content', 'Nightwatch.js');
  });

  after(browser => browser.end());
});

Command to run

bash
npx nightwatch .nightwatch/examples/basic/ecosia.js --verbose

Verbose Output

fundamental
Error
   unhandledRejection: There was an error while trying to load the file /Users/admin/project/formtests/commands/pickTime.js:
Error: There was an error while trying to load the file /Users/admin/project/formtests/commands/pickTime.js:
    at BaseLoader.handleModuleError (/Users/admin/project/formtests/node_modules/nightwatch/lib/api/_loaders/_base-loader.js:26:19)
    at CommandLoader.requireModuleAsync (/Users/admin/project/formtests/node_modules/nightwatch/lib/api/_loaders/_base-loader.js:248:18)
    at async CommandLoader.loadModuleAsync (/Users/admin/project/formtests/node_modules/nightwatch/lib/api/_loaders/_base-loader.js:230:5)
    at async ApiLoader.addCommandDefinitionAsync (/Users/admin/project/formtests/node_modules/nightwatch/lib/api/index.js:366:5)
    at async Promise.all (index 0)

Nightwatch Configuration

node
module.exports = {
  src_folders: ["test"],

  // page_objects_path: ["nightwatch/page-objects"],

  custom_commands_path: ["./commands"],

  // custom_assertions_path: ["nightwatch/custom-assertions"],

  plugins: [],

  globals_path: "",

  webdriver: {},

  test_workers: {
    enabled: true,
  },

  test_settings: {
    default: {
      disable_error_log: false,
      launch_url: "http://localhost:8080",

      screenshots: {
        enabled: false,
        path: "screens",
        on_failure: true,
      },

      desiredCapabilities: {
        browserName: "firefox",
        acceptInsecureCerts: true,
      },

      webdriver: {
        start_process: true,
        server_path: "",
      },
    },

    firefox: {
      desiredCapabilities: {
        browserName: "firefox",
        alwaysMatch: {
          acceptInsecureCerts: true,
          "moz:firefoxOptions": {
            args: [
              // '-headless',
              // '-verbose'
            ],
          },
        },
      },
      webdriver: {
        start_process: true,
        server_path: "",
        cli_args: [
          // very verbose geckodriver logs
          // '-vv'
        ],
      },
    },

    chrome: {
      desiredCapabilities: {
        browserName: "chrome",
        "goog:chromeOptions": {
          // More info on Chromedriver: https://sites.google.com/a/chromium.org/chromedriver/
          args: [
            //'--no-sandbox',
            //'--ignore-certificate-errors',
            //'--allow-insecure-localhost',
            //'--headless=new'
          ],
        },
      },

      webdriver: {
        start_process: true,
        server_path: "",
        cli_args: [
          // --verbose
        ],
      },
    },
  },
};

Nightwatch.js Version

3.12.2 and 3.9.0 (not simultaneously; tried both because previous issues mentioned up or downgrading Nightwatch to fix it)

Node Version

18.20.7

Browser

Firefox 144.0

Operating System

Mac OS 12.6.5

Additional Information

No response