Skip to content

Element type invalid when importing module via commonjs' require #136

@gpbl

Description

@gpbl

@LeoAref reported in this message that requiring the component via commonjs would throw an invariant violation:

var DayPicker = require('react-day-picker');

console output:

Warning: React.createElement: type should not be null, undefined, boolean, or number. It should be a string (for DOM elements) or a ReactClass (for composite components).
Uncaught Invariant Violation: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object.

A temporary workaround is to use the exported default object:

var DayPicker = require('react-day-picker').default;

The way of exporting in the main script doesn't indeed consider this case, so instead of writing

"use strict";

module.exports = require("./lib/DayPicker");
module.exports.DateUtils = require("./lib/DateUtils");
module.exports.LocaleUtils  = require("./lib/LocaleUtils");

I'd put

"use strict";

var DayPicker = require("./lib/DayPicker");
var DateUtils =  require("./lib/DateUtils");
var LocaleUtils = require("./lib/LocaleUtils");

module.exports = DayPicker.default || DayPicker;
module.exports.DateUtils = DateUtils.default || DateUtils;
module.exports.LocaleUtils  = LocaleUtils.default || LocaleUtils;

...considering we must support webpack, commonjs, babel 5 and babel 6 😅

I tag this as breaking change since users may already require using the above workaround.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions