Node.js Express Response.send()抛出类型错误

Node.js Express Response.send()抛出类型错误,node.js,typescript,express,npm,Node.js,Typescript,Express,Npm,我有一个简单的快速代码: const api = Router() api.post('/some-point', async (req, res, next) => { const someStuffToSend = await Promise.resolve("hello"); res.json({ someStuffToSend }); }) 它在我的开发环境中运行良好,但在prod上,我得到以下错误: TypeError: argument entity must be

我有一个简单的快速代码:

const api = Router()

api.post('/some-point', async (req, res, next) => {
  const someStuffToSend = await Promise.resolve("hello");
  res.json({ someStuffToSend });
})
它在我的开发环境中运行良好,但在prod上,我得到以下错误:

TypeError: argument entity must be string, Buffer, or fs.Stats
    at etag (/[...]/node_modules/etag/index.js:83:11)
    at generateETag ([...]/node_modules/express/lib/utils.js:280:12)
    at ServerResponse.send ([...]/node_modules/express/lib/response.js:200:17)
    at ServerResponse.json ([...]/node_modules/express/lib/response.js:267:15)
    at api.post (/the/code/above)
我在
node\u modules/etag/index.js:83:11检查并看到

if (!isStats && typeof entity !== 'string' && !Buffer.isBuffer(entity)) {
  throw new TypeError('argument entity must be string, Buffer, or fs.Stats')
}
在此代码之前,我添加了一个printf来检查实体的类型:

console.log("Entity contains", entity, "is of type", typeof entity, "with constructor", entity.constructor, "and is it a buffer?", Buffer.isBuffer(entity))
这让我得到了下面的输出:

Entity contains <Buffer 7b 22 70 72 65 64 69 63 74 69 6f 6e 5f 69 64 22 3a 22 63 4b 57 41 64 41 46 43 77 6e 55 43 22 2c 22 69 6e 69 74 69 61 6c 5f 70 72 65 64 69 63 74 69 6f ... > is of type object with constructor function Buffer(arg, encodingOrOffset, length) {
  if (!Buffer.TYPED_ARRAY_SUPPORT && !(this instanceof Buffer)) {
    return new Buffer(arg, encodingOrOffset, length)
  }

  // Common case.
  if (typeof arg === 'number') {
    if (typeof encodingOrOffset === 'string') {
      throw new Error(
        'If encoding is specified then the first argument must be a string'
      )
    }
    return allocUnsafe(this, arg)
  }
  return from(this, arg, encodingOrOffset, length)
} and is it a buffer? false
如果您查看
/node\u modules/express/lib/response.js:221:10
,您会看到

this.end(chunk, encoding);
其中编码来自上面几行(l.189,我用printf检查)

我可以通过破解lib来实现这一点,但我怀疑一些损坏的
node\u模块
文件夹。但是,即使在
rm package-lock.json;rm-射频节点单元模块;npm i

任何关于如何解决这个问题的线索都将不胜感激

以下是我的版本号:

  • 节点9.8.0(也尝试使用8.4.0),使用nvm本地安装
  • npm 5.6.0
  • 快报4.16.3
  • ts节点5.0.1
  • 打字稿2.7.2
编辑1

  • 用更简单的方法替换异步调用
  • 指定快速版本
编辑2

我移除了
node\u modules
文件夹,然后
npm I
一个接一个地移除了包:

npm i aws-sdk body-parser bunyan check-types deepcopy duck-type express fast-csv glob handlebars http-auth md5 moment moment-timezone multer node-stream object-path randomstring
仍然会得到相同的错误

编辑3

添加有关环境的更多信息

编辑4

好吧,算了。这是一个与ts节点配置相关的问题。 我正在启动我的服务器

ts-node --harmony_async_iteration -r tsconfig-paths/register ./src/index.ts
在my
tsconfig.json
中使用以下行:

{
  "compilerOptions": {
    "module": "commonjs",
    "target": "es2017",
    "lib": [ "es2017", "esnext.asynciterable" ],
    "noImplicitAny": true,
    "moduleResolution": "node",
    "sourceMap": true,
    "outDir": "dist",
    "baseUrl": ".",
    "pretty": true,
    "paths": {
      "*": [
        "*", "src/*", "src/types/*",
        "node_modules/*"
      ]
    }
  },
  "include": [ "src/**/*" ]
}
由于命令行中的
-r tsconfig path/register
,因此加载了
tsconfig.json
中指定的路径,包括
路径['*']
中的
“node\u modules/*”

我不知道为什么,但这似乎导致
node_modules
中的lib加载了两次,打破了基于构造函数的类型检查(例如
instanceof

问题


我不确定是否完全理解其原因。有光吗?

我在
typeorm
和后来的
express
上也有类似的问题。这暗示很明显。我的解决方案是将
*
路径中删除

someAsyncFunc()
到底解决了什么问题?你能给我们看一下代码吗?还有,你在生产中运行的是什么版本的Express?谢谢你的关注。我用一个
Promise.resolve()
替换了异步调用,但它仍然因相同的错误而崩溃。我还建议使用Express版本(prod和dev上都是4.16.3),我会清楚地建议使用所有依赖模块,并从NPM干净地重新加载它们。。。
ts-node --harmony_async_iteration -r tsconfig-paths/register ./src/index.ts
{
  "compilerOptions": {
    "module": "commonjs",
    "target": "es2017",
    "lib": [ "es2017", "esnext.asynciterable" ],
    "noImplicitAny": true,
    "moduleResolution": "node",
    "sourceMap": true,
    "outDir": "dist",
    "baseUrl": ".",
    "pretty": true,
    "paths": {
      "*": [
        "*", "src/*", "src/types/*",
        "node_modules/*"
      ]
    }
  },
  "include": [ "src/**/*" ]
}