Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeScript errors due to missing @types/react-dom and @types/webpack when using "strict": true and "skipLibCheck": false #16219

Closed
elliottsj opened this issue Aug 15, 2020 · 9 comments
Assignees
Labels
please verify canary The issue should be verified against next@canary. It will be closed after 30 days of inactivity TypeScript Related to types with Next.js.
Milestone

Comments

@elliottsj
Copy link
Contributor

Bug report

Describe the bug

When setting tsconfig.json options "skipLibCheck": false and "strict": true, TypeScript errors are raised due to missing dependencies @types/react-dom and @types/webpack.

As a workaround, a Next.js project can declare these as dependencies in package.json, but this shouldn't be necessary.

To Reproduce

First, initialize a Next.js app with TypeScript:

$ npx create-next-app@9.5.2 my-app
...
$ cd my-app
$ touch tsconfig.json
$ npm run build

> my-app@0.1.0 build /Users/spencerelliott/Dev/elliottsj/my-app
> next build

It looks like you're trying to use TypeScript but do not have the required package(s) installed.

Please install typescript, @types/react, and @types/node by running:

	npm install --save-dev typescript @types/react @types/node

If you are not trying to use TypeScript, please remove the tsconfig.json file from your package root (and any TypeScript files in your pages directory).

$ npm install --save-dev typescript @types/react @types/node
$ npm run build
... (success)

Then edit tsconfig.json: set "strict": true and "skipLibCheck": false. Try building again:

$ npm run build

> my-app@0.1.0 build /Users/spencerelliott/Dev/elliottsj/my-app
> next build

Failed to compile.

./node_modules/next/types/index.d.ts:3:23
Type error: Cannot find type definition file for 'react-dom'.

  1 | /// <reference types="node" />
  2 | /// <reference types="react" />
> 3 | /// <reference types="react-dom" />
    |                       ^
  4 |
  5 | import React from 'react'
  6 | import { ParsedUrlQuery } from 'querystring'

Install @types/react-dom as a workaround and this error is resolved, but there's a new error:

$ npm install --save-dev @types/react-dom
...
$ npm run build

> my-app@0.1.0 build /Users/spencerelliott/Dev/elliottsj/my-app
> next build

Failed to compile.

./node_modules/next/dist/build/webpack/plugins/pages-manifest-plugin.d.ts:1:34
Type error: Could not find a declaration file for module 'webpack'. '/Users/spencerelliott/Dev/elliottsj/my-app/node_modules/webpack/lib/webpack.js' implicitly has an 'any' type.
  Try `npm install @types/webpack` if it exists or add a new declaration (.d.ts) file containing `declare module 'webpack';`

> 1 | import { Compiler, Plugin } from 'webpack';
    |                                  ^
  2 | export declare type PagesManifest = {
  3 |     [page: string]: string;
  4 | };

Install @types/webpack, then we get a successful build:

$ npm install --save-dev @types/webpack
...
$ npm run build

> my-app@0.1.0 build /Users/spencerelliott/Dev/elliottsj/my-app
> next build

info  - Creating an optimized production build
info  - Compiled successfully
info  - Collecting page data
info  - Generating static pages (2/2)
info  - Finalizing page optimization

Page                                                           Size     First Load JS
┌ ○ /                                                          3.42 kB        61.1 kB
├   └ css/a181cbd139209b03ddcc.css                             716 B
├   /_app                                                      0 B            57.7 kB
├ ○ /404                                                       3.45 kB        61.2 kB
└ λ /api/hello                                                 0 B            57.7 kB
+ First Load JS shared by all                                  57.7 kB
  ├ chunks/f6078781a05fe1bcb0902d23dbbb2662c8d200b3.e68a13.js  10 kB
  ├ chunks/framework.085e84.js                                 40 kB
  ├ chunks/main.270a02.js                                      6.69 kB
  ├ chunks/pages/_app.81bed2.js                                281 B
  ├ chunks/webpack.ccf5ab.js                                   751 B
  └ css/a7d1b6e4789f5d2be852.css                               202 B

λ  (Server)  server-side renders at runtime (uses getInitialProps or getServerSideProps)
○  (Static)  automatically rendered as static HTML (uses no initial props)
●  (SSG)     automatically generated as static HTML + JSON (uses getStaticProps)
   (ISR)     incremental static regeneration (uses revalidate in getStaticProps)

Redirects

┌ source: /:path+/
├ destination: /:path+
└ permanent: true

Expected behavior

The Next.js app should build successfully without having to install @types/react-dom and @types/webpack.

System information

  • OS: macOS
  • Version of Next.js: 9.5.2
  • Version of Node.js: 14.8.0
elliottsj added a commit to elliottsj/elliott.dev that referenced this issue Aug 15, 2020
elliottsj added a commit to elliottsj/elliott.dev that referenced this issue Aug 15, 2020
@joshmanders
Copy link

Any updates on this? I'm getting same error and find it weird that I have to have types available to build in production.

@Timer Timer modified the milestones: 10.x.x, backlog Dec 31, 2020
@JavierMartinz
Copy link

I'm getting the same error and had to disable NPM_ONLY_PRODUCTION

@mohd-akram
Copy link
Contributor

This is still happening with Next 11, although only @types/react-dom is required.

@timneutkens timneutkens added the TypeScript Related to types with Next.js. label Nov 17, 2021
@AshConnolly
Copy link

Still happening in Next 12. Any ideas?

@JavierMartinz

This comment has been minimized.

@AshConnolly
Copy link

For others my solution was to install all packages, but prune dev dependencies after build like so:

npm ci 

npm run build 

npm prune --production 

npm run start

It works fine, but it It would be better if this error didn't occur and we could just do npm ci --production followed by build, without the error occurring.

@jankaifer jankaifer self-assigned this Dec 1, 2022
@jankaifer jankaifer added the please verify canary The issue should be verified against next@canary. It will be closed after 30 days of inactivity label Dec 1, 2022
@github-actions
Copy link
Contributor

github-actions bot commented Dec 1, 2022

Please verify that your issue can be recreated with next@canary.

Why was this issue marked with the please verify canary label?

We noticed the provided reproduction was using an older version of Next.js, instead of canary.

The canary version of Next.js ships daily and includes all features and fixes that have not been released to the stable version yet. You can think of canary as a public beta. Some issues may already be fixed in the canary version, so please verify that your issue reproduces by running npm install next@canary and test it in your project, using your reproduction steps.

If the issue does not reproduce with the canary version, then it has already been fixed and this issue can be closed.

How can I quickly verify if my issue has been fixed in canary?

The safest way is to install next@canary in your project and test it, but you can also search through closed Next.js issues for duplicates or check the Next.js releases.

My issue has been open for a long time, why do I need to verify canary now?

Next.js does not backport bug fixes to older versions of Next.js. Instead, we are trying to introduce only a minimal amount of breaking changes between major releases.

What happens if I don't verify against the canary version of Next.js?

An issue with the please verify canary that receives no meaningful activity (e.g. new comments that acknowledge verification against canary) will be automatically closed and locked after 30 days.

If your issue has not been resolved in that time and it has been closed/locked, please open a new issue, with the required reproduction, using next@canary.

I did not open this issue, but it is relevant to me, what can I do to help?

Anyone experiencing the same issue is welcome to provide a minimal reproduction following the above steps. Furthermore, you can upvote the issue using the 👍 reaction on the topmost comment (please do not comment "I have the same issue" without repro steps). Then, we can sort issues by votes to prioritize.

I think my reproduction is good enough, why aren't you looking into it quicker?

We look into every Next.js issue and constantly monitor open issues for new comments.

However, sometimes we might miss one or two due to the popularity/high traffic of the repository. We apologize, and kindly ask you to refrain from tagging core maintainers, as that will usually not result in increased priority.

Upvoting issues to show your interest will help us prioritize and address them as quickly as possible. That said, every issue is important to us, and if an issue gets closed by accident, we encourage you to open a new one linking to the old issue and we will look into it.

Useful Resources

@balazsorban44
Copy link
Member

This issue has been automatically closed because it wasn't verified against next@canary. If you think it was closed by accident, please leave a comment. If you are running into a similar issue, please open a new issue with a reproduction. Thank you.

@github-actions
Copy link
Contributor

github-actions bot commented Feb 5, 2023

This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 5, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
please verify canary The issue should be verified against next@canary. It will be closed after 30 days of inactivity TypeScript Related to types with Next.js.
Projects
None yet
Development

No branches or pull requests

9 participants