Closed RockyStrongo closed 9 months ago
the error usually happens when encountered a mismatched import
vs. export
. In your case:
export function Select (){...}
the import should be like
import {Select} from "mylib";// not!: import Select from ...
the error usually happens when encountered a mismatched
import
vs.export
. In your case:export function Select (){...}
the import should be like
import {Select} from "mylib";// not!: import Select from ...
I proprely imported the component in the target project, as below :
import { Select } from '@mylibrary/components'
can you provide a repro, thanks! 🙏
can you provide a repro, thanks! 🙏
Working on it 🙂
@aladdin-add please find below the reproduction:
when running the target project, you get the error :
Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object.
Check the render method of `Select`.
react-select:
Object.defineProperty(exports, '__esModule', { value: true });
...
exports["default"]=xxx;
the problem is:
var ReactSelect = require('react-select'); // ❌should be: require('react-select').default;
I think one of the causes is the next bundler does not know pkg.module
.
module
field in package.json:The module field in package.json
is used to specify the entry point for a module when it is imported using the ES6 module syntax (import and export). This field allows you to define a different file that will be used when the module is imported with the import statement. For example:
{
"name": "my-module",
"main": "main.js",
"module": "main-esm.js"
}
The "exports" feature was introduced in Node.js to provide a more flexible way to specify entry points for modules. Instead of relying solely on the main and module fields, the "exports" field allows you to define different entry points based on conditions like the Node.js version or the requested file extension.
For example:
{
"name": "my-module",
"exports": {
".": {"require": "./main.js", "import": "./main.mjs"}
}
}
I suggest adding the exports
in your lib. I think we can also update dts templates to support bundlers like next.js. 🤔
Thanks ! It fixed my issue.
In case it can help somebody, the reproduction repo is here : https://github.com/RockyStrongo/dts-issue-reproduction/tree/resolution (issue reproduction in main, solution in branch solution)
Current Behavior
For some components, I get the error below when using my library built with dts-cli from a react project (react 18.2.0, NextJS 13.5.5)
Error Message
I suspect the issue is occuring when the component is using packages that are not ES modules.
For example, a component using react-select will generate this error, while a component using @headlessui/react works fine.
I noticed that @headlessui/react includes
"type"="module"
in its package.json while react-select does not.When copying the component code directly in the target project or testing it from storybook, it works as expected. So I would guess the issue is related to bundling - packaging.
Example of component not working
Expected behavior
The component is usable from a react project when imported from the library built with dts.
Suggested solution(s)
Additional context
Library built and published via gitlabCI.
I tried adding a .babelrc file as below, it did not solve the issue :
the component is exported as below in the entry point file index.tsx :
export * from './components/Select';
Your environment
dts-cli library :
target project :