本文介绍了如何在节点中使用全局URLSearchParams的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个(客户端)JavaScript库(一个节点/角度模块).在此库中,我使用了URLSearchParams类.

I am writing a (client-side) JavaScript library (a node/angular module).In this library, I make use of the URLSearchParams class.

const form = new URLSearchParams();
form.set('username', data.username);
form.set('password', data.pass);

由于这是一个共享库,因此打包为npm模块.但是,在运行摩卡单元测试时,出现错误,即未定义URLSearchParams.原因似乎是该节点在全局范围内没有URLSearchParams,但是必须使用 require('url'):

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 require('url'):

$ node
> 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.<anonymous> (repl.js:538:10)
    at emitOne (events.js:101:20)
    at REPLServer.emit (events.js:188:7)

如何使URLSearchParams可用于节点内的客户端代码,以便可以使用mocha测试库?

How can I make URLSearchParams available to the client-side code within node, so that I can test the library using mocha?

这不起作用:

> global.URLSearchParams = require('url').URLSearchParams
undefined
> 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.<anonymous> (repl.js:538:10)
    at emitOne (events.js:101:20)
    at REPLServer.emit (events.js:188:7)

推荐答案

更新:节点v10在全局对象上具有 URLSearchParams 的内置可用性,因此可以按照问题的预期直接使用.

Update: Node v10 has built-in availability of URLSearchParams on the global object so it can be used directly as anticipated in the question.

旧版本的节点:

一种选择是在测试运行程序的启动脚本中将其设置为全局变量:

One option is to set it as a global in the start-up script of the test runner:

import { URLSearchParams } from 'url';
global.URLSearchParams = URLSearchParams

例如,使用Jest,您将使用 setupTestFrameworkScriptFile 指向上面的启动脚本.

With Jest, for example, you would use the setupTestFrameworkScriptFile to point to the above start-up script.

作为旁注,如果要在创建通用代码的服务器端Webpack捆绑包时实现类似的结果,则可以使用Webpack ProvidePlugin :

As a side note, if you wanted to achieve a similar outcome when creating a server-side Webpack bundle of universal code you can achieve this with the Webpack ProvidePlugin:

{
  name: 'server',
  target: 'node',
  // ...
  plugins: [
    // ...
    new webpack.ProvidePlugin({
      URLSearchParams: ['url', 'URLSearchParams'],
      fetch: 'node-fetch',
    }),
  ],
}

这篇关于如何在节点中使用全局URLSearchParams的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-22 06:38