Error running with express and nextjs - TypeError [ERR_INVALID_ARG_TYPE]: The "chunk" argument must be of type string or an instance of Buffer. Received an instance of Promise

See original GitHub issue

I’m using express with nextjs, but pulled the express app out. Code look llike so

// server.js 
const next = require('next');
const { initializeExpressWithNext } = require('./src/server/app');
const dev = process.env.NODE_ENV !== 'production';
const nextApplication = next({ dev });
const handle = nextApplication.getRequestHandler();

nextApplication.prepare().then(() => {
  const app = initializeExpressWithNext(handle);

  app.listen(3000, (err) => {
    if (err) throw err;
    console.log('Ready on http://localhost:3000');
  });
});


//app.js
const express = require('express');
const cors = require('cors');
const apiMetrics = require('prometheus-api-metrics');

const questionnaireRoutes = require('./routes/questionnaire');
const recaptchaRoutes = require('./routes/recaptcha');

/**
 * @function initializeExpressWithNext
 * @description to run this with nextjs, we need to pass in a callbackhandler
 * for all routes that are not defined by express, we do this by wrapping express
 * in a function to initialize the callback and runtime
 * @param {function} callbackHandler - nextjs server callbackhandler
 * @return {*} express application
 */
function initializeExpressWithNext(callbackHandler) {
  const app = express();
  app.use(apiMetrics());

  app.get('/healthcheck', cors(), (_, res) => {
    res.status(200).send();
  });

  app.use('/', questionnaireRoutes);
  app.use('/', recaptchaRoutes);

  app.get('*', (req, res) => {
    return callbackHandler(req, res);
  });

  return app;
}

module.exports = { initializeExpressWithNext };

The app starts fine, but when i navigate to /metrics or metrics.json, I get the following error

TypeError [ERR_INVALID_ARG_TYPE]: The "chunk" argument must be of type string or an instance of Buffer. Received an instance of Promise
    at ServerResponse.end (_http_outgoing.js:784:13)
    at ExpressMiddleware.middleware (/Users/nathanhall/bwell/medstar-squeeze-page/node_modules/prometheus-api-metrics/src/express-middleware.js:99:24)
    at Layer.handle [as handle_request] (/Users/nathanhall/bwell/medstar-squeeze-page/node_modules/express/lib/router/layer.js:95:5)
    at trim_prefix (/Users/nathanhall/bwell/medstar-squeeze-page/node_modules/express/lib/router/index.js:317:13)
    at /Users/nathanhall/bwell/medstar-squeeze-page/node_modules/express/lib/router/index.js:284:7
    at Function.process_params (/Users/nathanhall/bwell/medstar-squeeze-page/node_modules/express/lib/router/index.js:335:12)
    at next (/Users/nathanhall/bwell/medstar-squeeze-page/node_modules/express/lib/router/index.js:275:10)
    at expressInit (/Users/nathanhall/bwell/medstar-squeeze-page/node_modules/express/lib/middleware/init.js:40:5)
    at Layer.handle [as handle_request] (/Users/nathanhall/bwell/medstar-squeeze-page/node_modules/express/lib/router/layer.js:95:5)
    at trim_prefix (/Users/nathanhall/bwell/medstar-squeeze-page/node_modules/express/lib/router/index.js:317:13)
    at /Users/nathanhall/bwell/medstar-squeeze-page/node_modules/express/lib/router/index.js:284:7
    at Function.process_params (/Users/nathanhall/bwell/medstar-squeeze-page/node_modules/express/lib/router/index.js:335:12)
    at next (/Users/nathanhall/bwell/medstar-squeeze-page/node_modules/express/lib/router/index.js:275:10)
    at query (/Users/nathanhall/bwell/medstar-squeeze-page/node_modules/express/lib/middleware/query.js:45:5)
    at Layer.handle [as handle_request] (/Users/nathanhall/bwell/medstar-squeeze-page/node_modules/express/lib/router/layer.js:95:5)
    at trim_prefix (/Users/nathanhall/bwell/medstar-squeeze-page/node_modules/express/lib/router/index.js:317:13)
    ```
    
    I'll try to see if I can figure it out and make. PR but I'm on a tight timeline

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
idantocommented, Jan 31, 2021

This is related to a change in prom-client 13.x

@dominathan please fix the failed tests so we can approve the MR and release a major version

0reactions
dominathancommented, Feb 9, 2021

I would please, I don’t have any quick solution to fix the tests for different node versions. Currently just using prom-client directly.

Read more comments on GitHub >

github_iconTop Results From Across the Web

The "chunk" argument must be of type string or an instance of ...
It's because data 's data type is an object/json , and res.write() accepts data type string or buffer . JSON.stringify converts the object...
Read more >
typeerror [err_invalid_arg_type]: the "path" argument must be ...
TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string or an instance of Buffer or URL. Received type number ... StackOverflow.
Read more >
The "ikm" argument must be of type string or an instance of ...
node js TypeError [ERR_INVALID_ARG_TYPE]: The argument must be of type string. Received undefined. shell by Armandres · Armandres on Oct 05 2020 Donate...
Read more >
[Solved]NodeJs: The chunk argument must be of type string or ...
Node js: TypeError [ERR_INVALID_ARG_TYPE]: The chunk argument must be of type string or an instance of Buffer. Received an instance of ...
Read more >
module-not-found - Next.js
The module you're trying to import is not installed in your dependencies. When importing a module from npm this module has to be...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found