Skip to content
This repository was archived by the owner on Oct 8, 2025. It is now read-only.
This repository was archived by the owner on Oct 8, 2025. It is now read-only.

nodejs unit-http misses options in createServer function #1043

Closed

Description

@stefanhuber

I tried to run strapi inside nginx unit. The startup of strapi doesn't work because of the following error:

TypeError [ERR_INVALID_ARG_TYPE]: The "listener" argument must be of type function. Received an instance of Object
    at checkListener (node:events:270:3)
    at _addListener (node:events:550:3)
    at Server.addListener (node:events:609:10)
    at new Server (/usr/local/lib/node_modules/unit-http/http_server.js:426:14)
    at Object.createServer (/usr/local/lib/node_modules/unit-http/http.js:15:12)
    at Module.createHTTPServer (/var/www/node_modules/@strapi/strapi/dist/services/server/http-server.js:16:40)
    at Module.createServer (/var/www/node_modules/@strapi/strapi/dist/services/server/index.js:26:35)
    at new Strapi (/var/www/node_modules/@strapi/strapi/dist/Strapi.js:130:27)
    at initFn (/var/www/node_modules/@strapi/strapi/dist/Strapi.js:466:18)
    at Object.<anonymous> (/var/www/index.js:2:1) {
  code: 'ERR_INVALID_ARG_TYPE'
}

Strapi uses options with the http.createServer function as seen here in the source code. The node signature of the createServer function looks like this: http.createServer([options][, requestListener]). Looking at the http module inside the unit-http package the createServer function is missing the options argument.

Can this be solved somehow? How could strapi be started otherwise within nginx unit?

Nginx Unit has the following config.json:

{
    "listeners": {
        "*:1337": {
            "pass": "routes/main"
        }
    },
    "routes": {
        "main": [
            {
                "action": {
                    "pass": "applications/app"
                }
            }
        ]
    },
    "applications": {
        "app": {
            "type": "external",
            "working_directory": "/var/www/",
            "executable": "/usr/bin/env",
            "arguments": [
                "node",
                "--loader",
                "unit-http/loader.mjs",
                "--require",
                "unit-http/loader",
                "index.js"
            ]
        }
    }
}

Activity

  1. stefanhuber commented on Jan 4, 2024

    @stefanhuber
    Author

    I could create a fix by using patch-package with the following steps:

    • Install unit-http and patch-package as npm dependencies: npm i unit-http patch-package --save
    • Add "postinstall": "patch-package" under "scripts" inside package.json
    • Edit the http.js file inside node_modules/unit-http by adding options as the first argument to the createServer function:
      function createServer (options, requestHandler) {
       return new Server(requestHandler);
      }
      
    • Apply the patch: npx patch-package unit-http

    As a current workaround this is ok. Although I think this might not be a sustainable approach. The options could be ignored in the case of our strapi deployment.

  2. callahad commented on Jan 5, 2024

    @callahad
    Collaborator

    Looks like the options argument was added in Node.js v9.6.0 (2018-02-22) / v8.12.0 (2018-09-11) in nodejs/node#15752 and we missed it when implementing our shim for http.createServer.

    We should fix this in Unit, but the patch-package workaround should be fine for your use case until we can get a release out with a proper fix.

  3. self-assigned this
    on Jan 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

z-bug 🐞z-node-jsLanguage-Module for JavaScript (Node.js)

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions