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

[Bug]: Winston failed to execute caused by TypeError #2327

Closed
adampweb opened this issue Jul 16, 2023 · 13 comments · Fixed by #2353
Closed

[Bug]: Winston failed to execute caused by TypeError #2327

adampweb opened this issue Jul 16, 2023 · 13 comments · Fixed by #2353

Comments

@adampweb
Copy link

adampweb commented Jul 16, 2023

🔎 Search Terms

winston colors, winston docker container fails,

The problem

Winston version: ^3.10.0
NodeJS version: v18.16.1
NPM version: 9.5.1
NextJS version: 13.4.10

Winston brakes the app (NextJS 13) because of an unhandled Exception in peer dependency (@colors/colors):
The error message reflects a TypeError in https://github.com/DABH/colors.js/blob/master/lib/system/has-flag.js file. Not checks the argv variable is not undefined.

- error Error [TypeError]: Cannot read properties of undefined (reading 'indexOf')
2023-07-16T11:08:15.194016114Z     at <unknown> (webpack-internal:///(middleware)/./node_modules/@colors/colors/lib/system/has-flag.js:26)
2023-07-16T11:08:15.194019984Z     at module.exports (webpack-internal:///(middleware)/./node_modules/@colors/colors/lib/system/has-flag.js:26:30)
2023-07-16T11:08:15.194023256Z     at eval (webpack-internal:///(middleware)/./node_modules/@colors/colors/lib/system/supports-colors.js:29:5)
2023-07-16T11:08:15.194026264Z     at (middleware)/./node_modules/@colors/colors/lib/system/supports-colors.js (file:///app/_next/server/src/middleware.js:219:1)
2023-07-16T11:08:15.194029419Z     at __webpack_require__ (file:///app/_next/server/edge-runtime-webpack.js:37:33)
2023-07-16T11:08:15.194032354Z     at fn (file:///app/_next/server/edge-runtime-webpack.js:268:21)
2023-07-16T11:08:15.194035257Z     at eval (webpack-internal:///(middleware)/./node_modules/@colors/colors/lib/colors.js:36:25)
2023-07-16T11:08:15.194038400Z     at (middleware)/./node_modules/@colors/colors/lib/colors.js (file:///app/_next/server/src/middleware.js:127:1)
2023-07-16T11:08:15.194041466Z     at __webpack_require__ (file:///app/_next/server/edge-runtime-webpack.js:37:33)
2023-07-16T11:08:15.194044377Z     at fn (file:///app/_next/server/edge-runtime-webpack.js:268:21)
2023-07-16T11:08:15.194047214Z     at eval (webpack-internal:///(middleware)/./node_modules/@colors/colors/safe.js:9:14) {
2023-07-16T11:08:15.194050136Z   digest: undefined
2023-07-16T11:08:15.194052928Z }

What version of Winston presents the issue?

3.10.0

What version of Node are you using?

v18.16.11

If this worked in a previous version of Winston, which was it?

No response

Minimum Working Example

1. Fresh install NextJS with Tailwind CSS

npx create-next-app winston
Need to install the following packages:
  create-next-app@13.4.10
Ok to proceed? (y) y
✔ Would you like to use TypeScript? …  Yes
✔ Would you like to use ESLint? … No
✔ Would you like to use Tailwind CSS? … Yes
✔ Would you like to use `src/` directory? … Yes
✔ Would you like to use App Router? (recommended) … Yes
✔ Would you like to customize the default import alias? … No

2. Install the latest version of Winston

npm install winston

3. Make a logger instance in src/services/logger.ts file

import winston from 'winston';

const logger = winston.createLogger({
  level: 'debug',
  transports: [new winston.transports.Console()],
  format: winston.format.json(),
});
export default logger;

4. Create NextJS middleware in src/middleware.ts file

import { NextResponse } from 'next/server';
import type { NextRequest } from 'next/server';

import logger from '@/services/logger';

export const config = {
  matcher: '/',
}

export function middleware(request: NextRequest) {

  if(request === undefined){
    return NextResponse.json({
      message: "Bad request"
    }, {
      status: 400,
    })
  }

  const testHeader = 'script-src \'self\';';

  if(testHeader.includes('default-src') === false){
    logger.error("Header values mismatch!")
  }

  const response = NextResponse.next();
  response.headers.set('Content-Security-Policy', testHeader);
  return response;
}

5. Start the app

npm run dev

After that the error (which is detailed above) has appeared in the console and the browser.

Additional configs

package.json

{
  "name": "winston",
  "version": "0.1.0",
  "private": true,
  "scripts": {
    "dev": "next dev",
    "build": "next build",
    "start": "next start",
    "lint": "next lint"
  },
  "dependencies": {
    "@types/node": "20.4.2",
    "@types/react": "18.2.15",
    "@types/react-dom": "18.2.7",
    "autoprefixer": "10.4.14",
    "next": "13.4.10",
    "postcss": "8.4.26",
    "react": "18.2.0",
    "react-dom": "18.2.0",
    "tailwindcss": "3.3.3",
    "typescript": "5.1.6",
    "winston": "^3.10.0"
  }
}

tsconfig.json

Not modified manually, generated by npx create-next-app

{
  "compilerOptions": {
    "target": "es5",
    "lib": ["dom", "dom.iterable", "esnext"],
    "allowJs": true,
    "skipLibCheck": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "noEmit": true,
    "esModuleInterop": true,
    "module": "esnext",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "jsx": "preserve",
    "incremental": true,
    "plugins": [
      {
        "name": "next"
      }
    ],
    "paths": {
      "@/*": ["./src/*"]
    }
  },
  "include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"],
  "exclude": ["node_modules"]
}

next.config.js

Not modified manually, generated by npx create-next-app

/** @type {import('next').NextConfig} */
const nextConfig = {}

module.exports = nextConfig

Directory structure and permissions

drwxrwxr-x   7 adam adam 4,0K júl   16 14:16 .
drwxr-xr-x   8 adam adam 4,0K júl   16 14:12 ..
drwxrwxr-x   8 adam adam 4,0K júl   16 14:13 .git
-rw-rw-r--   1 adam adam  368 júl   16 14:11 .gitignore
drwxrwxr-x   6 adam adam 4,0K júl   16 14:47 .next
-rw-rw-r--   1 adam adam   92 júl   16 14:11 next.config.js
-rw-rw-r--   1 adam adam  201 júl   16 14:11 next-env.d.ts
drwxrwxr-x 133 adam adam 4,0K júl   16 14:17 node_modules
-rw-rw-r--   1 adam adam  514 júl   16 14:17 package.json
-rw-rw-r--   1 adam adam  63K júl   16 14:17 package-lock.json
-rw-rw-r--   1 adam adam   82 júl   16 14:11 postcss.config.js
drwxrwxr-x   2 adam adam 4,0K júl   16 14:12 public
-rw-rw-r--   1 adam adam 1,4K júl   16 14:11 README.md
drwxrwxr-x   4 adam adam 4,0K júl   16 14:24 src
-rw-rw-r--   1 adam adam  480 júl   16 14:12 tailwind.config.js
-rw-rw-r--   1 adam adam  642 júl   16 14:12 tsconfig.json

Additional information

No response

@adampweb
Copy link
Author

Important notice:
I tried all versions from the latest down to 3.8.2. But all present the same error. And I reproduced this outside of Docker environment too

@DABH
Copy link
Contributor

DABH commented Jul 16, 2023

@adampweb what version of colors are you picking up? I thought I fixed that colors issue in colors 1.6.0; maybe Winston needs to bump its dependency version? If so, please open a PR; if not, please consider opening a PR on colors? Thanks!

@adampweb
Copy link
Author

The latest version of winston still uses v1.5.0 of @colors/colors. But another dependency (logform) of winston also uses this version:
Output of npm ls @colors/colors:

client@0.1.0 /path/of/my/project
└─┬ winston@3.10.0
  ├── @colors/colors@1.5.0
  └─┬ logform@2.4.0
    └── @colors/colors@1.5.0 deduped

@DABH
Copy link
Contributor

DABH commented Jul 16, 2023

Ah, ok. If you open some PRs to bump the dependencies, I can merge and publish new versions, so then you should be good to go. Thanks

adampweb added a commit to adampweb/logform that referenced this issue Jul 16, 2023
adampweb added a commit to adampweb/winston that referenced this issue Jul 16, 2023
Issue represented here: winstonjs#2327
This was referenced Jul 16, 2023
@jeremybagwell
Copy link

Any idea when this will be released? I cannot figure how to work around 1.5.0 and continue to see the error.

@joekur
Copy link

joekur commented Sep 1, 2023

Please consider re-opening #2328. At least we will know if there are still issues on 1.6.0, and can then narrow down to some other potential issue.

@wu-s-john
Copy link

Aggre

Please consider re-opening #2328. At least we will know if there are still issues on 1.6.0, and can then narrow down to some other potential issue.

Just started using Winston and I am seeing issues with @colors/colors for version 1.6.0

@jameshalsall
Copy link
Contributor

"@colors/colors": "1.5.0",

is still in package.json for winston.

@TheCoreMan
Copy link

This is happenning to me as well

@iulianalbu
Copy link

Yeah, I have the same problem...

@DABH DABH closed this as completed in #2353 Oct 7, 2023
@juanmagalhaes
Copy link

I'm still having this issue after upgrading to Winston 3.11

@perseverance50k
Copy link

The same problem with winston@3.11.0

@christianvuerings
Copy link

Until winstonjs/logform#272 is merged & released you can work around the issue by using overrides in your package.json:

"overrides": {
  "@colors/colors": "1.6.0"
},

Note though that this doesn't fix the Winston + Next.js 13 app router issue since Winston uses process.nextTick:

return process.nextTick(gracefulExit);

which is not allowed in Next.js middleware: vercel/next.js#46722 (comment)

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

Successfully merging a pull request may close this issue.