Skip to content

Incorrect README example to import components directly #190

Closed

Description

@wozzo

From the readme

// Or you can import components directly
import Button from 'nhsuk-react-components/lib/components/button';

But the typescript definitions are in the dist directory so they cannot be found by tsc leading to this error

Could not find a declaration file for module 'nhsuk-react-components/lib/components/button'. '/nodebox/node_modules/.store/nhsuk-react-components@3.0.0/node_modules/nhsuk-react-components/lib/components/button/index.js' implicitly has an 'any' type.
  Try `npm i --save-dev @types/nhsuk-react-components` if it exists or add a new declaration (.d.ts) file containing `declare module 'nhsuk-react-components/lib/components/button';`typescript(7016)

See example sandbox.

If the rollup operation was modified to output to the lib folder then this would mean the types will all be present, but I'm not familiar enough with rollup to know what other effects this could have.

Activity

  1. KaiSpencer commented on Jan 20, 2024

    @KaiSpencer
    Contributor

    Thanks @wozzo for this and the reproduction.

    @Tomdango Can you take a look at this?

  2. changed the title [-]Can't import components directly if using TypeScript.[/-] [+]Incorrect README example to import components directly[/+] on Sep 29, 2025
  3. colinrotherham commented on Sep 29, 2025

    @colinrotherham
    Collaborator

    I've renamed the title as this issue is a typo in the README

      // Or you can import components directly
    - import Button from 'nhsuk-react-components/lib/components/button';
    + import Button from 'nhsuk-react-components/dist/esm/components/form-elements/button/Button.js';

    As an enhancement we might want to map lib/* into dist/esm/* using package exports?

        "files": [
    -     "dist",
    -     "lib"
    +     "dist"
        ],
    +   "sideEffects": false,
    +   "exports": {
    +     ".": {
    +       "types": "./dist/index.d.ts",
    +       "import": "./dist/esm/index.js",
    +       "require": "./dist/cjs/index.js",
    +       "default": "./dist/cjs/index.js"
    +     },
    +     "./*": "./*",
    +     "./lib/*": {
    +       "types": "./dist/esm/*",
    +       "import": "./dist/esm/*"
    +     },
    +     "./lib/*.js": "./dist/esm/*.js",
    +     "./package.json": "./package.json"
    +   },
        "main": "dist/cjs/index.js",
    -   "module": "dist/esm",
    +   "module": "dist/esm/index.js",
        "types": "dist/index.d.ts",
  4. colinrotherham commented on Oct 14, 2025

    @colinrotherham
    Collaborator

    The old lib path was fixed (now dist) in ee8bb81 via #271

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions