cross-fetch vs isomorphic-fetch vs node-fetch
HTTP 请求库
cross-fetchisomorphic-fetchnode-fetch类似的npm包:

HTTP 请求库

这些库用于在不同的 JavaScript 环境中执行 HTTP 请求,提供了一种统一的方式来处理网络请求。它们的设计目标是简化在浏览器和 Node.js 中的 API 使用,确保开发者能够在不同环境中以一致的方式进行数据获取。选择合适的库可以提高代码的可维护性和可移植性,尤其是在需要同时支持客户端和服务器端的应用程序中。

npm下载趋势

3 年

GitHub Stars 排名

统计详情

npm包名称
下载量
Stars
大小
Issues
发布时间
License
cross-fetch01,69893.3 kB271 年前MIT
isomorphic-fetch06,924-576 年前MIT
node-fetch08,862107 kB2413 年前MIT

功能对比: cross-fetch vs isomorphic-fetch vs node-fetch

环境兼容性

  • cross-fetch:

    cross-fetch 是一个轻量级的库,旨在提供一个一致的 Fetch API 接口,支持在浏览器和 Node.js 中使用。它会自动检测环境并选择合适的实现,确保在不同环境中都能正常工作。

  • isomorphic-fetch:

    isomorphic-fetch 旨在提供一个统一的 API,能够在客户端和服务器端都使用。它会自动处理请求的配置,确保在不同环境中都能保持一致性,是构建同构应用的理想选择。

  • node-fetch:

    node-fetch 专为 Node.js 设计,提供了一个符合 Fetch API 规范的实现。它不支持浏览器环境,因此在需要兼容性时可能不适合,但在 Node.js 中性能优秀。

API 兼容性

  • cross-fetch:

    cross-fetch 完全实现了 Fetch API 的规范,支持 Promise 和 async/await 语法,简化了异步请求的处理。它的 API 设计与浏览器原生 Fetch API 一致,易于上手。

  • isomorphic-fetch:

    isomorphic-fetch 也实现了 Fetch API,但它在某些情况下会自动处理请求头和 cookie,使得在同构应用中使用时更为方便。它的 API 设计与原生 Fetch API 类似,易于理解和使用。

  • node-fetch:

    node-fetch 提供了与 Fetch API 完全一致的接口,支持 Promise 和 async/await,适合在 Node.js 中进行 HTTP 请求。它的设计使得开发者可以轻松地在 Node.js 中使用 Fetch API 的特性。

功能丰富性

  • cross-fetch:

    cross-fetch 提供了基本的 Fetch 功能,支持 GET、POST 等常见请求类型,但不包含额外的功能,如请求重试或拦截器。它的目标是保持简单和轻量。

  • isomorphic-fetch:

    isomorphic-fetch 除了提供基本的 Fetch 功能外,还支持一些额外的功能,如自动处理 cookie 和请求头,适合需要更复杂请求配置的应用。

  • node-fetch:

    node-fetch 主要专注于提供 Fetch API 的核心功能,支持流式请求和响应,适合需要处理大数据量的后端应用。

学习曲线

  • cross-fetch:

    cross-fetch 的学习曲线相对平缓,开发者只需了解 Fetch API 的基本用法即可上手,适合初学者和需要快速实现 HTTP 请求的项目。

  • isomorphic-fetch:

    isomorphic-fetch 的学习曲线稍微陡峭一些,因为它涉及到同构应用的概念,但对于熟悉 Fetch API 的开发者来说,理解起来并不困难。

  • node-fetch:

    node-fetch 的学习曲线也很平坦,特别是对于已经熟悉 Fetch API 的开发者来说,使用起来非常直观。

如何选择: cross-fetch vs isomorphic-fetch vs node-fetch

  • cross-fetch:

    选择 cross-fetch 如果你需要一个轻量级的库,能够在浏览器和 Node.js 中无缝工作,且支持 Promise API。它是一个简单的 polyfill,适合需要兼容性和一致性的项目。

  • isomorphic-fetch:

    选择 isomorphic-fetch 如果你需要一个能够在客户端和服务器端都使用的库,并且希望它能够自动处理一些常见的配置(如请求头和 cookie)。它是一个更全面的解决方案,适合需要在同一代码库中处理多种环境的应用。

  • node-fetch:

    选择 node-fetch 如果你只在 Node.js 环境中工作,并且需要一个轻量级的、符合 Fetch API 规范的实现。它专注于 Node.js 的性能和特性,适合后端开发。

cross-fetch的README

cross-fetch
NPM Version Downloads Per Week License: MIT CI codecov

Universal WHATWG Fetch API for Node, Browsers, Workers and React Native. The scenario that cross-fetch really shines is when the same JavaScript codebase needs to run on different platforms.

  • Platform agnostic: browsers, Node or React Native
  • Optional polyfill: it's up to you if something is going to be added to the global object or not
  • Simple interface: no instantiation, no configuration and no extra dependency
  • WHATWG compliant: it works the same way wherever your code runs
  • TypeScript support: better development experience with types.
  • Worker support: works on different types of workers such as Service Workers and CloudFlare Workers

Table of Contents


Install

npm install --save cross-fetch

As a ponyfill (imports locally):

// Using ES6 modules with Babel or TypeScript
import fetch from 'cross-fetch';

// Using CommonJS modules
const fetch = require('cross-fetch');

As a polyfill (installs globally):

// Using ES6 modules
import 'cross-fetch/polyfill';

// Using CommonJS modules
require('cross-fetch/polyfill');

The CDN build is also available on unpkg:

<script src="//unpkg.com/cross-fetch/dist/cross-fetch.js"></script>

This adds the fetch function to the window object. Note that this is not UMD compatible.


Usage

With promises:

import fetch from 'cross-fetch';
// Or just: import 'cross-fetch/polyfill';

fetch('//api.github.com/users/lquixada')
  .then(res => {
    if (res.status >= 400) {
      throw new Error("Bad response from server");
    }
    return res.json();
  })
  .then(user => {
    console.log(user);
  })
  .catch(err => {
    console.error(err);
  });

With async/await:

import fetch from 'cross-fetch';
// Or just: import 'cross-fetch/polyfill';

(async () => {
  try {
    const res = await fetch('//api.github.com/users/lquixada');
    
    if (res.status >= 400) {
      throw new Error("Bad response from server");
    }
    
    const user = await res.json();
  
    console.log(user);
  } catch (err) {
    console.error(err);
  }
})();

Demo & API

You can find a comprehensive doc at Github's fetch page. If you want to play with cross-fetch, check our JSFiddle playground.

Tip: Run the fiddle on various browsers and with different settings (for instance: cross-domain requests, wrong urls or text requests). Don't forget to open the console in the test suite page and play around.

FAQ

Yet another fetch library?

I did a lot of research in order to find a fetch library that could be simple, cross-platform and provide polyfill as an option. There's a plethora of libs out there but none could match those requirements.

Why polyfill might not be a good idea?

In a word? Risk. If the spec changes in the future, it might be problematic to debug. Read more about it on sindresorhus's ponyfill page. It's up to you if you're fine with it or not.

How does cross-fetch work?

Just like isomorphic-fetch, it is just a proxy. If you're in node, it delivers you the node-fetch library, if you're in a browser or React Native, it delivers you the github's whatwg-fetch. The same strategy applies whether you're using polyfill or ponyfill.

Who's Using It?

The New York TimesApollo GraphQLFacebookSwaggerVulcanJSgraphql-request
The New York TimesApollo GraphQLFacebookSwaggerVulcanJSgraphql-request

Thanks

Heavily inspired by the works of matthew-andrews. Kudos to him!

License

cross-fetch is licensed under the MIT license © Leonardo Quixadá

Author

@lquixada
@lquixada