title: How to use global URLSearchParams in node
body: <p>I am writing a (client-side) JavaScript library (a node/angular module).
  In this library, I make use of the URLSearchParams class.</p>
  
  <pre><code>const form = new URLSearchParams();
  form.set('username', data.username);
  form.set('password', data.pass);
  </code></pre>
  
  <p>As this is a shared library, it is packed as an npm module.
  However, when running a mocha unit test, I get the error that URLSearchParams is not defined. The reason seems to be that node does not have URLSearchParams at the global scope, but has to be imported using <code>require('url')</code>:</p>
  
  <pre><code>$ node
  &gt; new URLSearchParams()
  ReferenceError: URLSearchParams is not defined
      at repl:1:5
      at sigintHandlersWrap (vm.js:22:35)
      at sigintHandlersWrap (vm.js:73:12)
      at ContextifyScript.Script.runInThisContext (vm.js:21:12)
      at REPLServer.defaultEval (repl.js:340:29)
      at bound (domain.js:280:14)
      at REPLServer.runBound [as eval] (domain.js:293:12)
      at REPLServer.&lt;anonymous&gt; (repl.js:538:10)
      at emitOne (events.js:101:20)
      at REPLServer.emit (events.js:188:7)
  </code></pre>
  
  <p>How can I make URLSearchParams available to the client-side code within node, so that I can test the library using mocha?</p>
  
  <p>This is not working:</p>
  
  <pre><code>&gt; global.URLSearchParams = require('url').URLSearchParams
  undefined
  &gt; new URLSearchParams()
  TypeError: URLSearchParams is not a constructor
      at repl:1:1
      at sigintHandlersWrap (vm.js:22:35)
      at sigintHandlersWrap (vm.js:73:12)
      at ContextifyScript.Script.runInThisContext (vm.js:21:12)
      at REPLServer.defaultEval (repl.js:340:29)
      at bound (domain.js:280:14)
      at REPLServer.runBound [as eval] (domain.js:293:12)
      at REPLServer.&lt;anonymous&gt; (repl.js:538:10)
      at emitOne (events.js:101:20)
      at REPLServer.emit (events.js:188:7)
  </code></pre>