Vercel cannot find module or type declaration when building Next.js app

Viewed 2494

I'm trying to deploy a Next.js app to Vercel and react-swipeable seems to have its own type declarations but:

Running "npm run build"
...
> next build
...
Failed to compile.
./components/SwipeBox.tsx:5:30
Type error: Cannot find module 'react-swipeable' or its corresponding type declarations.
  3 | 
  4 | import { NextPage } from "next";
> 5 | import { useSwipeable } from "react-swipeable";
    |                              ^
  6 | 
  7 | interface SwipeBoxProps {
  8 |   children: React.ReactNode;
...
Error: Command "npm run build" exited with 1

Do I need to add something to tsconfig.json? It currently looks like this:

{
  "compilerOptions": {
    "target": "es2020",
    "lib": ["dom", "dom.iterable", "esnext"],
    "allowJs": true,
    "checkJs": true,
    "skipLibCheck": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "noEmit": true,
    "esModuleInterop": true,
    "module": "esnext",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "jsx": "preserve"
  },
  "include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", "next.config.js"],
  "exclude": ["node_modules", "next.config.js"]
}

Builds perfectly locally as usual. Am I missing something?

1 Answers

Following @humanbean's suggestion in the comment I ran npm i --save-dev @types/react-swipeable and it worked.

I thought the library having built-in types meant it didn't need anything else but I was wrong.

Related