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

"Bun is not defined" - when trying to read a JSON file in NextJS component #5393

Closed
everspader opened this issue Sep 14, 2023 · 1 comment
Closed
Labels
bug Something isn't working node.js Compatibility with Node.js APIs

Comments

@everspader
Copy link

What version of Bun is running?

1.0.1+31aec4ebe325982fc0ef27498984b0ad9969162b

What platform is your computer?

Darwin 23.0.0 arm64 arm

What steps can reproduce the bug?

I'm trying to fetch data from a JSON file stored locally to populate inputs and selects in a form with NextJS. The server component of the page uses the useEffect to fetch the data with a helper function like so:

'use client'

async function getOptionsData() {
  const filePath = path.resolve(process.cwd(), 'data.json');
  const file = Bun.file(filePath);

  const contents = file.json();
  return contents;
}

export default function Home() {
  ...
  useEffect(() => {
    getOptionsData()
      .then((data) => {
        setOptions(data);
      })
      .catch((err) => {
        alert('Failed to fetch data: ' + err);
      });
  }, []);
  ...

What is the expected behavior?

Be able to read a file locally as if it was being read from a script or using the fs module like so:

const filePath = path.resolve(process.cwd(), 'data.json');
const data = fs.readFileSync(filePath, 'utf8');
const fileContents = JSON.parse(data);

Running the script with bun run script.ts works like a charm.

What do you see instead?

Running the dev server with yarn run dev yields in the following error:
Returns the following error:

Failed to fetch data: ReferenceError: Bun is not defined

Then, attempting with bun --bun run dev I get this error which I believe has some relation with this #5363:

ConnectionClosed: The socket connection was closed unexpectedly. For more information, pass `verbose: true` in the second argument to fetch()
 path: "http://localhost:54867/"

[0.02ms] ".env.local"
- error 29 |     let ErrorType = Error;
30 |     if (serializedErr.name === "PageNotFoundError") {
31 |         ErrorType = _utils.PageNotFoundError;
32 |     }
33 |     const err = new ErrorType(serializedErr.message);
34 |     err.stack = serializedErr.stack;
                                 ^
error: Invariant: Render expects to have requestAsyncStorage, none found
      at /Users/xxx/xxx/xxx/node_modules/next/dist/server/lib/server-ipc/request-utils.js:34:30

Additional information

I also tried to use a server component instead and fetching the data using a route handler in an /api/ endpoint and the error is always the same

@everspader everspader added the bug Something isn't working label Sep 14, 2023
@Electroid Electroid added the node.js Compatibility with Node.js APIs label Sep 14, 2023
@Electroid
Copy link
Contributor

@Electroid Electroid closed this as not planned Won't fix, can't repro, duplicate, stale Oct 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working node.js Compatibility with Node.js APIs
Projects
None yet
Development

No branches or pull requests

2 participants