Skip to content
This repository has been archived by the owner on Mar 19, 2021. It is now read-only.

Module objects cannot be functions (should use a named export) #74

Closed
JoshuaKGoldberg opened this issue Sep 6, 2018 · 3 comments
Closed

Comments

@JoshuaKGoldberg
Copy link

This is not valid JavaScript:

import * as AxeBuilder from "axe-webdriverjs";

AxeBuilder(browser);

See this SO answer:

An ES6 module namespace object cannot be invoked as a function or with new.

...so what should happen instead is:

import { AxeBuilder } from "axe-webdriverjs";

AxeBuilder(browser);

🙃.

Fortunately, this can be a purely additive change: somewhere in lib/index.js, you can add:

AxeBuilder.AxeBuilder = AxeBuilder;

...so that both the CommonJS-style const AxeBuilder = require("axe-webdriverjs"); and ES-style above or const AxeBuilder = require("axe-webdriverjs").AxeBuilder; will work.

@stephenmathieson
Copy link
Member

I think I'm missing context here. Why are you importing this package that way?

@JoshuaKGoldberg
Copy link
Author

@stephenmathieson sorry, I don't follow your question. Are you wondering why I'm importing the module like import { AxeBuilder } from "axe-webdriverjs" instead of import * as AxeBuilder from "axe-webdriverjs"? If that's what you intend to ask, it's because the spec for modules explicitly doesn't allow it. An ES6 module namespace object cannot be invoked as a function or with new.

@stephenmathieson
Copy link
Member

@JoshuaKGoldberg right... We don't strictly support ES modules here. Instead, we use Node's funky interpretation of CommonJS (where a function/class can take the place of the pseudo "default" export). This was the standard for years, and is still the way many packages are written to date. I think you're probably looking for a TypeScript (or alike) compatible export, which would require us to provide AxeBuilder on exports.default. I'm happy to make this change and can do so tomorrow.

On a side note, wouldn't import Builder = require('axe-builder') work?

stephenmathieson added a commit that referenced this issue Nov 26, 2018
Closes #74


## Reviewer checks

**Required fields, to be filled out by PR reviewer(s)**
- [x] Follows the commit message policy, appropriate for next version
- [x] Has documentation updated, a DU ticket, or requires no documentation change
- [x] Includes new tests, or was unnecessary
- [x] Code is reviewed for security by: @WilcoFiers
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants