I have a library that I want to use in both client side and server side. However, because request
is not compatible with browserify, when compiling using browserify, I need to use a different library called browser-request
我有一个我想在客户端和服务器端使用的库。但是,因为请求与browserify不兼容,所以在使用browserify进行编译时,我需要使用一个名为browser-request的不同库
if (inNodejsRuntime) {
var request = require('request');
} else if (isBrowserifyRuntime) {
var request = require('browser-request');
}
How do I go about detecting when browserifying is running vs when it is inside node
如何检测浏览器何时运行以及何时进入节点内部
3 个解决方案
#1
30
If you are just doing a simple module swap with compatible APIs you should use the browser field in package.json. So for your example, just do
如果您只是使用兼容的API进行简单的模块交换,则应使用package.json中的浏览器字段。所以对于你的例子,就这样做
var request = require('request')
like before and then in the package.json put:
喜欢之前然后在package.json中放:
{
"browser": {
"request": "browser-request"
}
}
This way in the browser you will get browser-request instead of request when you require('request')
.
这样在浏览器中,当您需要('request')时,您将获得浏览器请求而不是请求。
What you shouldn't do is require both modules with a runtime check for the presence of window
or some similar property. This is because you will get browser-request AND request bundled into your frontend code, even if you only actually use browser-request, resulting in a needlessly inflated file size.
你不应该做的是要求两个模块都有运行时检查是否存在窗口或类似的属性。这是因为即使您实际上只使用浏览器请求,您也会将浏览器请求和请求捆绑到您的前端代码中,从而导致文件大小不必要地膨胀。
#2
43
The accepted answer is correct. But if you got here by googling 'detect browserify' and want the more general answer, browserify automatically transforms the node-provided global process
. You can use:
接受的答案是正确的。但是如果你通过谷歌搜索“检测浏览器”来获得更一般的答案,那么browserify会自动转换节点提供的全局流程。您可以使用:
process.browser
which will be true
in the browser, undefined
in node.
这将在浏览器中生效,在节点中未定义。
#3
3
I found the answer:
我找到了答案:
if (typeof window === 'undefined') {
var request = require('request');
} else {
var request = require('browser-request');
}
Superagent is also looking like a very good alternative!
Superagent也看起来是一个非常好的选择!
#1
30
If you are just doing a simple module swap with compatible APIs you should use the browser field in package.json. So for your example, just do
如果您只是使用兼容的API进行简单的模块交换,则应使用package.json中的浏览器字段。所以对于你的例子,就这样做
var request = require('request')
like before and then in the package.json put:
喜欢之前然后在package.json中放:
{
"browser": {
"request": "browser-request"
}
}
This way in the browser you will get browser-request instead of request when you require('request')
.
这样在浏览器中,当您需要('request')时,您将获得浏览器请求而不是请求。
What you shouldn't do is require both modules with a runtime check for the presence of window
or some similar property. This is because you will get browser-request AND request bundled into your frontend code, even if you only actually use browser-request, resulting in a needlessly inflated file size.
你不应该做的是要求两个模块都有运行时检查是否存在窗口或类似的属性。这是因为即使您实际上只使用浏览器请求,您也会将浏览器请求和请求捆绑到您的前端代码中,从而导致文件大小不必要地膨胀。
#2
43
The accepted answer is correct. But if you got here by googling 'detect browserify' and want the more general answer, browserify automatically transforms the node-provided global process
. You can use:
接受的答案是正确的。但是如果你通过谷歌搜索“检测浏览器”来获得更一般的答案,那么browserify会自动转换节点提供的全局流程。您可以使用:
process.browser
which will be true
in the browser, undefined
in node.
这将在浏览器中生效,在节点中未定义。
#3
3
I found the answer:
我找到了答案:
if (typeof window === 'undefined') {
var request = require('request');
} else {
var request = require('browser-request');
}
Superagent is also looking like a very good alternative!
Superagent也看起来是一个非常好的选择!