ansis: Doesn't work in Next.js middleware (Edge functions)

Current behaviour

When using Ansis inside Next.js’s middleware.ts file, colours aren’t enabled.

Expected behaviour

Colours shown in terminal when using Ansis in a Next.js edge function.

Reproduction Example

Codesandbox

Environment

  • OS: macOS
  • version of Node.js: 18.16.0
  • version of Webpack: Next.js uses Turbopack now.
  • version of the Plugin: 1.5.6

Additional context

I’ve narrowed this down to this line: https://github.com/webdiscus/ansis/blob/7fca74ca3c6289883710acc578943b7b0cb7075c/src/ansi-codes.js#L29 and process.stdout not being available in Next.js’s middleware. Is the stdout && 'isTTY' in stdout necessary if you also require the /^screen|^xterm|^vt100|^vt220|^rxvt|color|ansi|cygwin|linux/i.test(env.TERM) to pass?

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 19 (19 by maintainers)

Most upvoted comments

Awesome! Thanks, yeah let’s close and I’ll open new issues if I find any 🙂

Yeah sorry, more of a throw away comment really. I’ll put a thread in their GitHub discussions to direct people this way when looking for a color logger that supports all of Next.js 👍😊

It works!

CleanShot 2023-11-02 at 10 56 26@2x

https://github.com/will-stone/nextjs-ansis/commit/85e413c35bf2c46b70a496c8dd47d30dff38d859

Thank you for this amazingly quick turnaround 🤯 This needs to be marketed to the Next.js peeps at Vercel as this lib could be their default for all logging now.

I know, the node:tty not works in edge mode, therefore I don’t use any node lib for it. Locally by me, colors already works in edge mode. I must write many tests yet.

Oh sure, happy to do that instead too. The codesandbox can be a bit flaky. I’ll let you know when I have a repo for you to spin up 👍

I’m happy to PR it, for sure. However, I’m a little worried I could break other situations. Would you be happy for me to add a failing test PR instead? I can try to set up a test that mimics Next.js middleware environment.