1

I am trying to make a NPM package using Typescript and React. (TSX)

I am following this blog post, but trying to make multiple components instead of just one.

When I try and import my module like this

import { HelloWorld } from 'tsx_lib'

It shows up as undefined

console.log(HelloWorld) //undefined

My folder structure looks like this

src /
   GoodbyeWorld.tsx
   HelloWorld.tsx
   index.ts
index.d.ts

Here are the files

GoodbyeWorld.tsx

import * as React from 'react';
import { GoodbyeWorldProps } from '../index';

export default class GoodbyeWorld extends React.Component<any, any> {
  render() {
    return <div style={{ color: this.props.color }}>Goodbye world!</div>;
  }
}

HelloWorld.tsx

import * as React from 'react';
import { HelloWorldProps } from '../index';

export default class HelloWorld extends React.Component<any, any> {
  render() {
    return <div style={{ color: this.props.color }}>Hello world!</div>;
  }
}

index.ts

export * from './HelloWorld';
export * from './GoodbyeWorld';

index.d.ts

import * as React from 'react';

export interface HelloWorldProps extends React.Props<HelloWorld> {
  color: string;
}

export interface GoodbyeWorldProps extends React.Props<HelloWorld> {
  color: string;
}

declare module 'hello-world' {

}

declare module 'goodbye-world' {

}

export class HelloWorld extends React.Component<HelloWorldProps, any> {}
export class GoodbyeWorld extends React.Component<HelloWorldProps, any> {}

What am I doing wrong?

2
  • try import HelloWorld from 'tsx_lib' without brackets, if you export it as default module, you need to import it without brackets Commented Oct 28, 2019 at 17:13
  • But I'm not trying to export it as default, I want to be able to access both HelloWorld and GoodbyeWorld Commented Oct 28, 2019 at 17:16

1 Answer 1

1

export default class HelloWorld in HelloWorld.tsx is a default export.

export * from './HelloWorld' will not pass through default exports.

You have two options:

  1. export named from HelloWorld.tsx
export class HelloWorld extends Component { 
...
  1. map the default export to a named export
export { default as HelloWorld } from './HelloWorld'
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.