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

$client is unknown #91

Open
robsel118 opened this issue May 6, 2023 · 19 comments
Open

$client is unknown #91

robsel118 opened this issue May 6, 2023 · 19 comments

Comments

@robsel118
Copy link

I've followed the recommend setup for V9, however I don't get the auto-completion because $client is unknown. This happens on my own project, but also on the playground for this project.

I am using VS code. With V3 it was working before but not on V9.

Would you know what the issue is?

@robsel118 robsel118 reopened this May 6, 2023
@robsel118
Copy link
Author

Seems a definition file is needed: https://nuxt.com/docs/guide/directory-structure/plugins#advanced

@kylegl
Copy link

kylegl commented Jun 27, 2023

hey did you solve this? I am getting this in vscode V1.79.2 in V1.78.x $client is defined.

@wobsoriano
Copy link
Owner

wobsoriano commented Jul 17, 2023

@kylegl is this with trpc v9?

@ncpleslie
Copy link

ncpleslie commented Nov 2, 2023

I was struggling with the same issue. Here's how I fixed it. I am using tRPC 10, by the way.

Updated plugins/client.ts by exporting the client as a type.

import { createTRPCNuxtClient, httpBatchLink } from "trpc-nuxt/client";
import superjson from "superjson";
import { loggerLink } from "@trpc/client";
import type { AppRouter } from "~/server/trpc/routers";

export default defineNuxtPlugin(() => {
  /**
   * createTRPCNuxtClient adds a `useQuery` composable
   * built on top of `useAsyncData`.
   */
  const client = createTRPCNuxtClient<AppRouter>({
    transformer: superjson,
    links: [
      loggerLink({
        enabled: (opts) =>
          process.env.NODE_ENV === "development" ||
          (opts.direction === "down" && opts.result instanceof Error),
      }),
      httpBatchLink({
        url: "/api/trpc",
      }),
    ],
  });

  return {
    provide: {
      client,
    },
  };
});

export type client = ReturnType<typeof createTRPCNuxtClient<AppRouter>>;

Then added an index.d.ts to the root of the application.
Note the import statement includes "type"

import { type client } from "./plugins/client";

declare module "#app" {
  interface NuxtApp {
    $client: client;
  }
}

export {};

@wobsoriano
Copy link
Owner

wobsoriano commented Nov 2, 2023

@ncpleslie that's weird. Nuxt should auto-infer the client type 🤔

@ncpleslie
Copy link

Yip, I agree. Its obviously an issue with our (@kylegl @robsel118) set up but didn't want to see a closed issue without a potential solution.

Alternatively, according to this stack overflow thread, delete your node_modules and .nuxt directories. Reinstall and rerun the application. Restarting the TypeScript server may help.

This isn't an issue with trpc-nuxt but an issue with TypeScript and Nuxt.

@Teygeta
Copy link

Teygeta commented Dec 6, 2023

I was struggling with the same issue. Here's how I fixed it. I am using tRPC 10, by the way.

Updated plugins/client.ts by exporting the client as a type.

import { createTRPCNuxtClient, httpBatchLink } from "trpc-nuxt/client";
import superjson from "superjson";
import { loggerLink } from "@trpc/client";
import type { AppRouter } from "~/server/trpc/routers";

export default defineNuxtPlugin(() => {
  /**
   * createTRPCNuxtClient adds a `useQuery` composable
   * built on top of `useAsyncData`.
   */
  const client = createTRPCNuxtClient<AppRouter>({
    transformer: superjson,
    links: [
      loggerLink({
        enabled: (opts) =>
          process.env.NODE_ENV === "development" ||
          (opts.direction === "down" && opts.result instanceof Error),
      }),
      httpBatchLink({
        url: "/api/trpc",
      }),
    ],
  });

  return {
    provide: {
      client,
    },
  };
});

export type client = ReturnType<typeof createTRPCNuxtClient<AppRouter>>;

Then added an index.d.ts to the root of the application. Note the import statement includes "type"

import { type client } from "./plugins/client";

declare module "#app" {
  interface NuxtApp {
    $client: client;
  }
}

export {};

insane workaround

@wobsoriano wobsoriano reopened this Dec 6, 2023
@Teygeta
Copy link

Teygeta commented Dec 9, 2023

Yip, I agree. Its obviously an issue with our (@kylegl @robsel118) set up but didn't want to see a closed issue without a potential solution.

Alternatively, according to this stack overflow thread, delete your node_modules and .nuxt directories. Reinstall and rerun the application. Restarting the TypeScript server may help.

This isn't an issue with trpc-nuxt but an issue with TypeScript and Nuxt.

I tried it and it still doesn't work

@Teygeta
Copy link

Teygeta commented Dec 9, 2023

I solved the problem by comparing this recommended nuxt-trpc guide to my project, and indeed the plugins directory I named it plugin incorrectly. After renaming it I removed .nuxt and node_modules and then launched pnpm i. Later I launched pnpm nuxt prepare and restarted volar with f1 and then >Volar: restart vue server

@Rednas83
Copy link
Contributor

I think it's better to use app.vue instead of pages/index.vue. I also had some strange issues when using pages/index.vue with useNuxtApp() and await

@estevanjantsk
Copy link

I had the same problem after I run in vscode (cmd+shift+p) Volar: Restart Vue Server it started to work.

@Rednas83
Copy link
Contributor

Rednas83 commented Jan 31, 2024

I also asked for adding trpc functionality in the todos demo from atinux. It could use some upvotes😉
atinux/atidone#15

@ambrizals
Copy link

At least don't modify tsconfig.json if you need some extra configuration on typescript.
Because on my side, this not happen.

image

@JollyBanny
Copy link

I had the same problem. I've tried to disable modules step by step and after removing @nuxtjs/seo module everything worked.

@brendonmatos
Copy link

I’ve encountered a similar issue, but when using a specific srdDir configuration. It might be related to type generation from Nuxt itself.

@gcom-ryan
Copy link

gcom-ryan commented Jul 23, 2024

import { type client } from "./plugins/client";

declare module "#app" {
  interface NuxtApp {
    $client: client;
  }
}

export {};

This fixed TRPC for our team. Thank you!

The whole app is built around TRPC, so if intellisense is broken, it leaves new team members in the dark about types and where to find them.

@razbakov
Copy link

httpBatchLink({
  url: "/api/trpc",
}),

throws this error: Object literal may only specify known properties, and 'url' does not exist in type 'HttpBatchLinkOptions'.

@wobsoriano
Copy link
Owner

Wha

httpBatchLink({
  url: "/api/trpc",
}),

throws this error: Object literal may only specify known properties, and 'url' does not exist in type 'HttpBatchLinkOptions'.

which trpc version are you on?

@razbakov
Copy link

@wobsoriano I had ^0.7.0 and update to ^0.10.22 fixed the issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests