Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

Getting Cannot find module '' or its corresponding type declarations. when importing in Next.js project.

This happens on every single import. Preview

Yarn version : 3.1.0-rc.2
Next version: 11.1.2

tsconfig.json:

"compilerOptions": { "target": "es6", "lib": [ "dom", "dom.iterable", "esnext" "allowJs": true, "skipLibCheck": true, "strict": true, "forceConsistentCasingInFileNames": true, "noEmit": true, "esModuleInterop": true, "module": "esnext", "moduleResolution": "node", "experimentalDecorators": true, "resolveJsonModule": true, "isolatedModules": true, "importHelpers": true, "jsx": "preserve", // "baseUrl": "src" "include": [ "next-env.d.ts", "**/*.ts", "**/*.tsx" "exclude": [ "node_modules" Really just in the directory where your package.json is, run yarn in your terminal / commandline. The only cause for the messages you describe is the packages either not being installed or VSCode not having indexed the node_modules directory. geisterfurz007 Sep 18, 2021 at 22:17 Well, the packages are installed by yarn install , yet i don't have node_module folder. Only .yarn and yarn.lock asobirov Sep 18, 2021 at 22:19 Hmmm, you mentioned that you are using Yarn 3, which uses PnP by default so it won't generate a node_modules folder. Have you looked at the editor support section of their migration guide ? Maybe that helps get VSCode up to speed indexing the installed dependencies geisterfurz007 Sep 18, 2021 at 22:24

If you're using yarn 3 and VSCode, then you need to follow these steps to set up your editor:

  • Run yarn dlx @yarnpkg/sdks vscode
  • Open any TypeScript file
  • Press ctrl+shift+p
  • Choose "Select TypeScript Version"
  • Pick "Use Workspace Version"
  • Read here for other editors

    I'm using Neovim, not VS Code, but the link to Yarn's documentation on editor SDKs was what I needed. Richard Turner Oct 1, 2021 at 14:24

    I tried solution suggested by Cuong Vu and many others and it didn't work. For me what worked was the following solution:

    In Yarn 2/3, support for modules in the node_modules folder is removed because of PnP. Follow the steps below to resolve the issue.

  • Create a .yarnrc.yml file in the project root,
  • Insert in the file nodeLinker: node-modules .
  • Run the command yarn in the terminal to recreate the node_modules folder.
  • Following all 3 steps the problem will be solved.

    @URogel Yarn PNP (Plug'n'Play) is amazing, it doesn't use the node_modules folder, it has quick dependency installation and you use much less disk space. But it is necessary to ensure that ALL project dependencies have PnP support. Although I have already read somewhere that next.js has standard support for PnP, I realize that it doesn't always work. nodeLinker: node-modules ensures that the node_module folder is recreated and yarn 2/3 behaves like yarn 1 or npm. Vagnerlandio Nunes Jan 6 at 19:55

    I wanted to provide more context and additional notes for others who might appreciate them.

  • Just reread @Vagnerlandio Nunes's answer, and it was spot on. Problem solved!

  • Recap/Solution #2: I was simply bootstrapping a new project using npx create-next-app , following the Next.js' documentation with npx create-next-app@latest --typescript (using yarn ) or ran yarn create next-app --typescript . I got errors relating to ts(2307): Cannot find module 'next/app' or its corresponding type declarations.

  • With @warfield's answer, I noticed I did not have a node_modules folder for some reason, and it was not created even after I ran yarn . I had to run npm i to create the node_modules folder, even though I planned to use yarn .

    This is the log I got when I ran yarn .

    % yarn
    ➤ YN0000: ┌ Resolution step
    ➤ YN0000: └ Completed
    ➤ YN0000: ┌ Fetch step
    ➤ YN0000: └ Completed
    ➤ YN0000: ┌ Link step
    ➤ YN0000: │ ESM support for PnP uses the experimental loader API and is therefore experimental
    ➤ YN0000: └ Completed
    ➤ YN0000: Done with warnings in 0s 244ms
    

    This is the log I got when I ran npm i.

    % npm i  
    added 235 packages, and audited 236 packages in 10s
    78 packages are looking for funding
      run `npm fund` for details
    found 0 vulnerabilities
    

    Now the problem is solved, and the error messages have gone away!

  • But out of curiosity, I ran yarn again, and this is the resulting output.
  • % yarn
    ➤ YN0070: Migrating from Yarn 1; automatically enabling the compatibility node-modules linker 👍
    ➤ YN0000: ┌ Resolution step
    ➤ YN0000: └ Completed in 3s 864ms
    ➤ YN0000: ┌ Fetch step
    ➤ YN0013: │ word-wrap@npm:1.2.3 can't be found in the cache and will be fetched from the remote registry
    ➤ YN0013: │ wrappy@npm:1.0.2 can't be found in the cache and will be fetched from the remote registry
    ➤ YN0013: │ yallist@npm:4.0.0 can't be found in the cache and will be fetched from the remote registry
    ➤ YN0013: │ yocto-queue@npm:0.1.0 can't be found in the cache and will be fetched from the remote registry
    ➤ YN0019: │ ms-npm-2.1.3-81ff3cfac1-aa92de6080.zip appears to be unused - removing
    ➤ YN0000: └ Completed in 0s 303ms
    ➤ YN0000: ┌ Link step
    ➤ YN0007: │ core-js-pure@npm:3.25.5 must be built because it never has been before or the last one failed
    ➤ YN0000: └ Completed in 4s 288ms
    ➤ YN0000: Done in 8s 486ms
    
  • In addition, if you're like me, you tried to modify your tsconfig.json file from
  • "include": [ "next-env.d.ts", "**/*.ts", "**/*.tsx" "include": [ "next-env.d.ts", "pages/**/*", "src/**/*",

    because you thought it was going to help based on this GitHub comment. You would now be seeing a new error message ts(2686): 'React' refers to a UMD global, but the current file is a module. Consider adding an import instead.. This is not necessary if you simply did the above step. So I just changed it back.

  • Changing your tsconfig.json's "compilerOptions": { ... } from "jsx": "preserve" to "jsx": "react-jsx" is also not necessary (reference to another attempted solution).
  • At any point if you want to make sure that the changes you've made (in regards to tsconfig) has gone into effect, try command + shift + p and enter > TypeScript: Restart TS server, and you should see some refreshing going on.
  • Double check you have a node_modules folder
  • If not, run yarn (or npm i) to (re)install the modules
  • Accidentally deleting the node_modules folder is the simplest explanation for the TS2307 Cannot find module '' error.

    It's also supported by OP comments

    "i don't have node_module folder. Only .yarn and yarn.lock"

    h/t @geisterfurz007's comments.

    I added this to my tsconfig.json file I can see you have it already, but it was not part of my solution

    "moduleResolution": "node",
            

    Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.

  •