Next.js Discord

Discord Forum

'client-only' cannot be imported from a Server Component module.

Unanswered
Tri Denda posted this in #help-forum
Open in Discord
I'm currently developing a Next.js (v14) app with Tina CMS. I want to use databaseClient which I created to fetch data from database but it turns out build failed due to cannot import the package in a server component module.

'client-only' cannot be imported from a Server Component module. It should only be used from a Client Component.

The error was caused by importing 'tinacms/dist/index.mjs' in './tina/config.ts'.

Import trace for requested module:
./tina/config.ts
./tina/database.ts
./tina/__generated__/databaseClient.ts
./src/lib/utils/getter.ts
./src/app/page.tsx


This is the 'databaseClient' being imported:
// src/lib/dataFetcher.ts

import { client } from "@/tina/__generated__/databaseClient";
import { unstable_cache } from "next/cache";
import { notFound } from "next/navigation";

export const getPageContent = unstable_cache(
  async (slug) => {
    try {
      return await client.queries.page({
        relativePath: `${slug}.mdx`,
      });
    } catch (e) {
      console.log(e);
      return notFound();
    }
  },
  ["content"],
  {
    revalidate: 1, // update every 1 seconds
  }

Repository: [My Github Repo](https://github.com/tridenda/tinacms)

Any help would be appreciated.

12 Replies

@gin is the datafetching important to keep on the server?
I have also tried to fetch the data on the client side, but it shows a different error.
fs is a node only module
cant be run on clientside
Inside databaseClient, I used an existing function built by Tina to make a connection. I didn't know it uses the fs module.
It's a bit weird, though; according to the documentation, it should be used in a server-side component.
@gin fs is a node only module
If you want to run the project locally, I'd be happy to share the .env file for a quick setup.
i can do that?
i tink its better if we hop on anydesk and i just fix it live for u
@gin i tink its better if we hop on anydesk and i just fix it live for u
That will be a great help. I'm struggling this for a week, still no idea how to fix it.