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

Building elysia v1.2.2 via next build crashes with "Can't resolve 'fs'" #971

Closed
EvHaus opened this issue Dec 24, 2024 · 12 comments
Closed
Labels
bug Something isn't working

Comments

@EvHaus
Copy link

EvHaus commented Dec 24, 2024

What version of Elysia is running?

1.2.2

What platform is your computer?

Darwin 24.1.0 arm64 arm

What steps can reproduce the bug?

In a Next.js project add a file that uses Typebox from Elysia:

import { t } from 'elysia';

export const sampleSchema = t.Object({})

Then build the project with next build.

What is the expected behavior?

The build should succeed.

What do you see instead?

The build fails with:

Failed to compile.

../../node_modules/.pnpm/elysia@1.2.2_@sinclair+typebox@0.34.13_typescript@5.7.2/node_modules/elysia/dist/universal/fs.mjs:2:1
Module not found: Can't resolve 'fs'
Did you mean './fs'?
Requests that should resolve in the current directory need to start with './'.
Requests that start with a name are treated as module requests and resolve within module directories (node_modules, /myproject/node_modules/.pnpm/next@15.1.2_@babel+core@7.26.0_@opentelemetry+api@1.9.0_react-dom@18.3.1_react@18.3.1__react@18.3.1/node_modules/next/dist/bin/node_modules, /myproject/node_modules/.pnpm/next@15.1.2_@babel+core@7.26.0_@opentelemetry+api@1.9.0_react-dom@18.3.1_react@18.3.1__react@18.3.1/node_modules/next/dist/node_modules, /myproject/node_modules/.pnpm/next@15.1.2_@babel+core@7.26.0_@opentelemetry+api@1.9.0_react-dom@18.3.1_react@18.3.1__react@18.3.1/node_modules/next/node_modules, /myproject/node_modules/.pnpm/next@15.1.2_@babel+core@7.26.0_@opentelemetry+api@1.9.0_react-dom@18.3.1_react@18.3.1__react@18.3.1/node_modules, /myproject/node_modules/.pnpm/node_modules, /myproject/packages/forge-web).
If changing the source code is not an option there is also a resolve options called 'preferRelative' which tries to resolve these kind of requests in the current directory too.
  1 | import{ env }from"./env.mjs";
> 2 | const fs = typeof process !== "undefined" && env.NODE_ENV ? await import("fs") : {};
    | ^
  3 | const noop = () => {
  4 | };
  5 | const createReadStream = fs.createReadStream ?? noop;

https://nextjs.org/docs/messages/module-not-found

Import trace for requested module:
../../node_modules/.pnpm/elysia@1.2.2_@sinclair+typebox@0.34.13_typescript@5.7.2/node_modules/elysia/dist/universal/file.mjs
../../node_modules/.pnpm/elysia@1.2.2_@sinclair+typebox@0.34.13_typescript@5.7.2/node_modules/elysia/dist/index.mjs
../rs-api/models/organization.ts
./pages/onboarding/index.tsx


> Build failed because of webpack errors
 ELIFECYCLE  Command failed with exit code 1.


Screenshot 2024-12-23 at 10 21 50 PM

Additional information

Rolling back to 1.1.27 fixes the problem.

Have you try removing the node_modules and bun.lockb and try again yet?

Yes

@EvHaus EvHaus added the bug Something isn't working label Dec 24, 2024
@0-don
Copy link

0-don commented Dec 24, 2024

having the same issue using as nextjs adapter only with nodejs

@SaltyAom
Copy link
Member

SaltyAom commented Dec 24, 2024

Should have been fixed in e83daaf, published under 1.2.3

Let me know if the issue still persists after updating to 1.2.3.

If so, please provide a simple reproducible repo, thanks.

@0-don
Copy link

0-don commented Dec 24, 2024

@SaltyAom
image

its still happening

[zero@arch next-elysia-prisma]$ bun run build
$ next build
   ▲ Next.js 15.1.2
   - Environments: .env

   Creating an optimized production build ...
Failed to compile.

./node_modules/elysia/dist/universal/file.mjs
Module not found: Can't resolve 'fs'

https://nextjs.org/docs/messages/module-not-found

Import trace for requested module:
./node_modules/elysia/dist/index.mjs
./src/lib/typebox/auth.ts
./src/app/(auth)/login/page.tsx

./node_modules/elysia/dist/universal/file.mjs
Module not found: Can't resolve 'fs/promises'

https://nextjs.org/docs/messages/module-not-found

Import trace for requested module:
./node_modules/elysia/dist/index.mjs
./src/lib/typebox/auth.ts
./src/app/(auth)/login/page.tsx


> Build failed because of webpack errors
error: script "build" exited with code 1
[zero@arch next-elysia-prisma]$ 

image

@0-don
Copy link

0-don commented Dec 24, 2024

here is an example project https://github.com/0-don/next-elysia-prisma

@EvHaus
Copy link
Author

EvHaus commented Dec 30, 2024

Still an issue in 1.2.10. Seeing this:

  ⨯ ./node_modules/.pnpm/elysia@1.2.10_@sinclair+typebox@0.34.13_typescript@5.7.2/node_modules/elysia/dist/universal/file.mjs:96:24
 Module not found: Can't resolve 'fs'
   94 |       } else {
   95 |         if (!createReadStream || !stat) {
 > 96 |           this.value = import("fs").then((fs) => {
      |                        ^^^^^^^^^^^^
   97 |             createReadStream = fs.createReadStream;
   98 |             return fs.createReadStream(path);
   99 |           });
 https://nextjs.org/docs/messages/module-not-found
  ⨯ ./node_modules/.pnpm/elysia@1.2.10_@sinclair+typebox@0.34.13_typescript@5.7.2/node_modules/elysia/dist/universal/file.mjs:100:24
 Module not found: Can't resolve 'fs/promises'
    98 |             return fs.createReadStream(path);
    99 |           });
 > 100 |           this.stats = import("fs/promises").then((fs) => {
       |                        ^^^^^^^^^^^^^^^^^^^^^
   101 |             stat = fs.stat;
   102 |             return fs.stat(path);
   103 |           });

I had to switch from using import {t} from 'elysia' to import {Type} from '@sinclair/typebox' instead.

@luishdz1010
Copy link

This also happens while running a simple project with wrangler dev,

  ✘ [ERROR] Could not resolve "fs/promises"
  
  
  ../../node_modules/.pnpm/elysia@1.2.10_@sinclair+typebox@0.34.14_openapi-types@12.1.3_typescript@5.7.3/node_modules/elysia/dist/universal/file.mjs:100:30:
        100 │           this.stats = import("fs/promises").then((fs) => {
            ╵                               ~~~~~~~~~~~~~

@SaltyAom SaltyAom reopened this Feb 16, 2025
@SaltyAom
Copy link
Member

Not sure if this would fix the problem since I couldn't replicate it on my end, but with b1bf2f2 which should be published under 1.2.13 might fixed the problem

@EvHaus EvHaus closed this as completed Feb 16, 2025
@EvHaus
Copy link
Author

EvHaus commented Feb 16, 2025

Unfortunately not fixed in 1.2.13:

../../node_modules/.pnpm/elysia@1.2.13_@sinclair+typebox@0.34.25_typescript@5.7.3/node_modules/elysia/dist/universal/file.mjs:96:1
Module not found: Can't resolve 'fs'
  94 |       } else {
  95 |         if (!createReadStream || !stat) {
> 96 |           this.value = import("fs").then((fs) => {
     | ^
  97 |             createReadStream = fs.createReadStream;
  98 |             return fs.createReadStream(path);
  99 |           });

However, I'm going to mark this as resolved. If you need Typebox in a Next.js build, you can switch to direct typebox imports instead:

// Change this
import {t} from 'elysia';

// to
import {Type} from '@sinclair/typebox'

@kravetsone
Copy link
Contributor

Unfortunately not fixed in 1.2.13:

../../node_modules/.pnpm/elysia@1.2.13_@sinclair+typebox@0.34.25_typescript@5.7.3/node_modules/elysia/dist/universal/file.mjs:96:1
Module not found: Can't resolve 'fs'
  94 |       } else {
  95 |         if (!createReadStream || !stat) {
> 96 |           this.value = import("fs").then((fs) => {
     | ^
  97 |             createReadStream = fs.createReadStream;
  98 |             return fs.createReadStream(path);
  99 |           });

However, I'm going to mark this as resolved. If you need Typebox in a Next.js build, you can switch to direct typebox imports instead:

// Change this
import {t} from 'elysia';

// to
import {Type} from '@sinclair/typebox'

or import { t } from "elysia/type-system";

@SaltyAom
Copy link
Member

I forgot to warp another import('fs') in 1.2.13, can you check if it works on 1.2.14? thanks

@kravetsone
Copy link
Contributor

I forgot to warp another import('fs') in 1.2.13, can you check if it works on 1.2.14? thanks

Maybe we can add test with Bun.build target web?

@EvHaus
Copy link
Author

EvHaus commented Feb 17, 2025

I forgot to warp another import('fs') in 1.2.13, can you check if it works on 1.2.14? thanks

The build script completed this time! But with warnings:

⚠ Compiled with warnings

../../node_modules/.pnpm/elysia@1.2.14_@sinclair+typebox@0.34.25_typescript@5.7.3/node_modules/elysia/dist/universal/file.mjs
Module not found: Can't resolve 'fs' in '/Users/me/proj/node_modules/.pnpm/elysia@1.2.14_@sinclair+typebox@0.34.25_typescript@5.7.3/node_modules/elysia/dist/universal'

Import trace for requested module:
../../node_modules/.pnpm/elysia@1.2.14_@sinclair+typebox@0.34.25_typescript@5.7.3/node_modules/elysia/dist/universal/file.mjs
../../node_modules/.pnpm/elysia@1.2.14_@sinclair+typebox@0.34.25_typescript@5.7.3/node_modules/elysia/dist/index.mjs
./components/AppLayout/AppLayout.tsx

../../node_modules/.pnpm/elysia@1.2.14_@sinclair+typebox@0.34.25_typescript@5.7.3/node_modules/elysia/dist/universal/file.mjs
Module not found: Can't resolve 'fs/promises' in '/Users/me/proj/node_modules/.pnpm/elysia@1.2.14_@sinclair+typebox@0.34.25_typescript@5.7.3/node_modules/elysia/dist/universal'

Import trace for requested module:
../../node_modules/.pnpm/elysia@1.2.14_@sinclair+typebox@0.34.25_typescript@5.7.3/node_modules/elysia/dist/universal/file.mjs
../../node_modules/.pnpm/elysia@1.2.14_@sinclair+typebox@0.34.25_typescript@5.7.3/node_modules/elysia/dist/index.mjs
./components/AppLayout/AppLayout.tsx

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

5 participants