nuxt / nuxt

The Intuitive Vue Framework.
https://nuxt.com
MIT License
54.68k stars 5k forks source link

Cannot find module 'nitropack' or its corresponding type declarations. #26175

Closed martinszeltins closed 7 months ago

martinszeltins commented 7 months ago

Environment

Stackblitz

------------------------------
- Operating System: Linux
- Node Version:     v18.18.0
- Nuxt Version:     3.10.3
- CLI Version:      3.10.1
- Nitro Version:    -
- Package Manager:  npm@10.2.3
- Builder:          -
- User Config:      devtools, typescript
- Runtime Modules:  -
- Build Modules:    -
------------------------------

Reproduction

https://stackblitz.com/github/martinszeltins/nuxt-nitropack?file=utils%2Fapi.ts

Describe the bug

I wanted to use InternalApi type from nitropack so I imported it but it is throwing me this error:

import type { NitroFetchRequest, AvailableRouterMethod as _AvailableRouterMethod, InternalApi } from 'nitropack'

Cannot find module 'nitropack' or its corresponding type declarations.ts(2307)

I was looking at another Nuxt project where someone had done this and it resolved to this path but it is not working for me in my project.

/nodemodules/.pnpm/nuxt@3.9.3@types+node@20.5.6_eslint@8.56.0_rollup@4.9.5_typescript@5.3.3_vite@5.0.11_vue-tsc@1.8.27/node_modules/nuxt/types.d.ts

/// <reference types="nitropack" />

declare module 'nitropack' {

I also tried importing form #imports but that did not work either. I am creating my own wrapper for useFetch() so I need to get access to nitro InternalApi type.

Additional context

No response

Logs

No response

manniL commented 7 months ago

Most likely dupe of #23984

martinszeltins commented 7 months ago

@manniL in my case I have shamefully-hoist=true and adding public-hoist-pattern[]=*nitropack* did not help. It still gives me this error even after deleting node_modules and pnpm-lock file and reinstalling dependencies.

Cannot find module 'nitropack'. Did you mean to set the 'moduleResolution' option to 'node', or to add aliases to the 'paths' option?

martinszeltins commented 7 months ago

I see that with Nuxt 3.10 the "moduleResolution" has been changed from "Node" to "Bundler" which seems to be causing the issue for me. What can I do about this?

martinszeltins commented 7 months ago

Finally found the solution to my problem, had to this this:

nuxt.config.ts

export default defineNuxtConfig({
  future: {
    typescriptBundlerResolution: false
  }
})

https://nuxt.com/blog/v3-10#bundler-module-resolution

manniL commented 7 months ago

@martinszeltins Weird, I can't reproduce the problem with bundler resolution + PNPM on my end 🤔

martinszeltins commented 7 months ago

@martinszeltins Weird, I can't reproduce the problem with bundler resolution + PNPM on my end 🤔

Honestly, no idea but I'm glad the typescriptBundlerResolution setting fixed it. With Bundler resolution all these 3 lines were showing errors, when switching to Node the errors are gone.

import type { FetchResult } from 'nuxt/dist/app/composables/fetch.d.ts'
import type { KeysOf, PickFrom } from 'nuxt/dist/app/composables/asyncData'
import type { NitroFetchRequest, AvailableRouterMethod as _AvailableRouterMethod } from 'nitropack'
JvanderHeide commented 5 months ago

Using regular npm and running in to the same issue as Martins. Though for me it was for defineNuxtConfig, import {createResolver} from "@nuxt/kit"; and more. Workaround seems to work for now.

jonasfroeller commented 3 months ago

I got this yesterday:

[02:10:37]  WARN  [plugin node-resolve] Could not resolve import "nitropack/runtime" in A:\xxx\node_modules\.pnpm\nuxt-auth-utils@0.1.0_magicast@0.3.4_rollup@4.18.1\node_modules\nuxt-auth-utils\dist\runtime\server\plugins\oauth.js using exports defined in A:\xxx\node_modules\.pnpm\nitropack@2.7.2_ioredis@5.4.1_magicast@0.3.4\node_modules\nitropack\package.json.

[02:10:38]  WARN  [plugin node-resolve] Could not resolve import "nitropack/runtime" in A:\xxx\node_modules\.pnpm\nuxt-auth-utils@0.1.0_magicast@0.3.4_rollup@4.18.1\node_modules\nuxt-auth-utils\dist\runtime\server\plugins\oauth.js using exports defined in A:\xxx\node_modules\.pnpm\nitropack@2.7.2_ioredis@5.4.1_magicast@0.3.4\node_modules\nitropack\package.json.

[02:10:38]  WARN  "nitropack/runtime" is imported by "node_modules/.pnpm/nuxt-auth-utils@0.1.0_magicast@0.3.4_rollup@4.18.1/node_modules/nuxt-auth-utils/dist/runtime/server/plugins/oauth.js", but could not be resolved – treating it as an external dependency.

  [worker reload] [worker init] Cannot find package 'nitropack' imported from A:\xxx\.nuxt\dev\index.mjs                                    02:13:16  

  at __node_internal_captureLargerStackTrace (node:internal/errors:496:5)
  at new NodeError (node:internal/errors:405:5)
  at packageResolve (node:internal/modules/esm/resolve:916:9)
  at moduleResolve (node:internal/modules/esm/resolve:973:20)
  at defaultResolve (node:internal/modules/esm/resolve:1193:11)
  at ModuleLoader.defaultResolve (node:internal/modules/esm/loader:403:12)
  at ModuleLoader.resolve (node:internal/modules/esm/loader:372:25)
  at ModuleLoader.getModuleJob (node:internal/modules/esm/loader:249:38)
  at ModuleWrap.<anonymous> (node:internal/modules/esm/module_job:76:39)
  at link (node:internal/modules/esm/module_job:75:36)

I tried a lot of things, like deleting .nuxt and node_modules and reinstalling them, or installing Nitropack as a dev dependency, but what ended up fixing it was me restarting my PC. Man, I hate Windows so much. Every third problem I have is caused by it. I really have to switch to Linux soon. I cannot stand this anymore.

Yeah, the moral of the story is that you should always ask yourself this question: "Have you tried turning it off and on again?"