javascript 如何在节点中使用全局 URLSearchParams
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/47266550/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use global URLSearchParams in node
提问by Martin Nyolt
I am writing a (client-side) JavaScript library (a node/angular module). In this library, I make use of the URLSearchParams class.
我正在编写一个(客户端)JavaScript 库(一个节点/角度模块)。在这个库中,我使用了 URLSearchParams 类。
const form = new URLSearchParams();
form.set('username', data.username);
form.set('password', data.pass);
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'):
由于这是一个共享库,它被打包为一个 npm 模块。但是,在运行 mocha 单元测试时,我收到未定义 URLSearchParams 的错误。原因似乎是该节点在全局范围内没有 URLSearchParams,但必须使用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)
How can I make URLSearchParams available to the client-side code within node, so that I can test the library using mocha?
如何使 URLSearchParams 可用于节点内的客户端代码,以便我可以使用 mocha 测试库?
This is not working:
这不起作用:
> 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)
回答by marnusw
Update:Node v10 has built-in availability of URLSearchParamson the global object so it can be used directly as anticipated in the question.
更新:Node v10URLSearchParams在全局对象上具有内置可用性,因此可以按照问题中的预期直接使用。
Older versions of Node:
旧版本的节点:
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
With Jest, for example, you would use the setupTestFrameworkScriptFileto point to the above start-up script.
例如,对于 Jest,您将使用setupTestFrameworkScriptFile指向上述启动脚本。
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:
附带说明一下,如果您想在创建服务器端 Webpack 通用代码包时获得类似的结果,您可以使用 Webpack 来实现ProvidePlugin:
{
name: 'server',
target: 'node',
// ...
plugins: [
// ...
new webpack.ProvidePlugin({
URLSearchParams: ['url', 'URLSearchParams'],
fetch: 'node-fetch',
}),
],
}
回答by Bruno Bronosky
In AWS Lambda which uses Node8.10, I had to do:
在使用 Node8.10 的 AWS Lambda 中,我必须执行以下操作:
const {URLSearchParams} = require('url')
const sp = new URLSearchParams(request.querystring)
or
或者
const url = require('url')
const sp = new url.URLSearchParams(request.querystring)
回答by SayJeyHi
If we want to support big range of nodejsversions in our application , we could use some dirty code like this :
如果我们想nodejs在我们的应用程序中支持大范围的版本,我们可以使用一些像这样的脏代码:
if(typeof URLSearchParams === 'undefined'){
URLSearchParams = require('url').URLSearchParams;
}
Note: It is not best practice to requirewith a condition.
注意:require使用条件不是最佳做法。
回答by Viacheslav Dobromyslov
You may use polifill @ungap/url-search-paramshttps://www.npmjs.com/package/@ungap/url-search-paramsand for webpack may use @ungap/url-search-params/cjs
你可以使用 polifill @ungap/url-search-params https://www.npmjs.com/package/@ungap/url-search-params并且 webpack 可以使用@ungap/url-search-params/cjs
Old NodeJS 8(which isused in AWS and GCloud) does not support URLSearchParams so this polifill helps.
旧的NodeJS 8(在 AWS 和 GCloud 中使用)不支持 URLSearchParams,所以这个 polifill 有帮助。
In Node 10when using TypeScript you may enable library domwhich includes URLSearchParams implementation. Change tsconfig.json:
在Node 10 中使用 TypeScript 时,您可以启用包含 URLSearchParams 实现的库dom。更改tsconfig.json:
{
"compilerOptions": {
"lib": [
...
"dom"
...
]
}
}

