我可以在 browser/node.js 上使用不同的库,而不必编辑 package.json 吗?

Can I use different libs on the browser/node.js, without having to edit package.json?

xhr library provides a way to do requests on both browser and node.js under the same API. For that, you must require another library, request,并编辑你的 package.json 以告诉 browserify 将 request 替换为 xhr,这比缩小整个 xhr 更轻,因此更合适=14=] 库。不知道有没有不编辑也能达到同样效果的方法package.json。类似于:

#if browserify
    const req = require("xhr")
#else
    const req = require("request")

你可以这样做:

let req;
if (process.browser) {
    req = require("xhr");
} else {
    const nodeRequire = require;
    req = nodeRequire("request");
}

请注意 nodeRequire 是防止 Browserify 捆绑 request 所必需的。