ladjs / superagent

Ajax for Node.js and browsers (JS HTTP client). Maintained for @forwardemail, @ladjs, @spamscanner, @breejs, @cabinjs, and @lassjs.
https://ladjs.github.io/superagent/
MIT License
16.59k stars 1.33k forks source link

Error: Attempt to change mapping for "form" extension from "application/x-www-form-urlencoded" to "application/x-www-form-urlencoded". #1288

Closed sdesalas closed 7 years ago

sdesalas commented 7 years ago

Superagent has recently been upgraded to use version 2.0.3 of mime.

However, between version 1.4.1 and 2.0.3 of mime an earlier warning has been upgraded to an error when defining the same mime types twice .

And since many other packages and tools rely on superagent, the lack of pre-check before defining mime-types a second time is causing subsequent require("superagent") calls from erroring out as per the example below (whenever two packages feature superagent as a dependency).

Use following steps to reproduce the error.

npm install swagger-tools supertest
node -e 'require("swagger-tools"); require("supertest");'

And the output:

/home/user/projects/mytest/node_modules/mime/Mime.js:32
          throw new Error(`Attempt to change mapping for "${ext}" extension from "${this._types[ext]}" to "${type}". Pass \`force=true\` to allow this, otherwise remove "${ext}" from the list of extensions for "${type}".`);
          ^

Error: Attempt to change mapping for "form" extension from "application/x-www-form-urlencoded" to "application/x-www-form-urlencoded". Pass `force=true` to allow this, otherwise remove "form" from the list of extensions for "application/x-www-form-urlencoded".
    at Mime.define (/home/user/projects/mytest/node_modules/mime/Mime.js:32:17)
    at Object.<anonymous> (/home/user/projects/mytest/node_modules/supertest/node_modules/superagent/lib/node/index.js:71:6)
    at Module._compile (module.js:569:30)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:503:32)
    at tryModuleLoad (module.js:466:12)
    at Function.Module._load (module.js:458:3)
    at Module.require (module.js:513:17)
    at require (internal/module.js:11:18)
    at Object.<anonymous> (/home/user/projects/mytest/node_modules/supertest/lib/test.js:5:15)
sdesalas commented 7 years ago

Mime version 2.0.0 onwards provides a force=true flag to allow multiple calls to .define() for the same mime type, which in turn stops the error from being thrown.

Fix as follows:

/lib/node/index.js L71

/**
 * Define "form" mime type.
 */

mime.define({
  'application/x-www-form-urlencoded': ['form', 'urlencoded', 'form-data']
}, true); // <--- add 'force=true' here 
allanyego commented 4 years ago

I believe this is fixed for superagent but somehow I still get this error when using supertest to hit my endpoints. This was ok just yesterday, then today it's not working... Can anyone help here or is this an issue for supertest?

leemr commented 3 years ago

Based on this, https://github.com/strongloop/loopback-next/issues/6616, I updated loopback-connector-rest from ^3 to ^4.0.1. It has the fix for mime force=true.