Gutenberg: Error when trying to use @wordpress/api-request package standalone

Created on 16 Sep 2018  路  7Comments  路  Source: WordPress/gutenberg

Describe the bug
When trying to use the @wordpress/api-request package (it's in my package.json file) I am receiving the following error during my JS application's build:

ERROR in ./node_modules/@wordpress/api-request/build-module/middlewares/http-v1.js
Module not found: Error: Can't resolve '@babel/runtime/core-js/json/stringify' in '/Users/...{path omitted}.../node_modules/@wordpress/api-request/build-module/middlewares'
 @ ./node_modules/@wordpress/api-request/build-module/middlewares/http-v1.js 1:0-68
 @ ./node_modules/@wordpress/api-request/build-module/index.js
 @ ./assets/scripts/components/Component.jsx
 @ ./assets/scripts/frontend.js
 @ multi ./assets/scripts/frontend.js ./assets/styles/frontend.scss

To Reproduce
Steps to reproduce the behavior:

  1. Include @wordpress/api-request in package.json like so:
"dependencies": {
    "@wordpress/api-request": "^1.0.0-alpha.3",
    ...
}
  1. Import apiRequest as mentioned in the docs like so:
import apiRequest from '@wordpress/api-request';
  1. Try to use apiRequest in your code, like so:
apiRequest( { path: '/wp/v2/posts' } ).then( posts => {
    console.log( posts );
} );
  1. See in build (I'm using Webpack)

Expected behavior
apiRequest should be use-able


Am I doing something wrong here? If there are additional dependencies, shouldn't those be included in the docs or already installed as dependencies of the apiRequest package? Sorry if I'm missing something obvious.

[Package] API fetch [Type] Help Request

Most helpful comment

Sorry about the confusion with the docs, @JulienMelissas. @chrisvanpatten is correct, @wordpress/api-request is deprecated in favour of @wordpress/api-fetch, as of #7566.

The npm install command is supposed to show a message to warn you @wordpress/api-request is deprecated, but it isn't showing when I test it. @ntwb, could you look into this, please?

@gziolo, @youknowriad: Could we publish an update to the @wordpress/api-request readme, showing that it's deprecated?

I've edited the handbook page to point to @wordpress/api-fetch.

All 7 comments

@JulienMelissas Could you try using the @wordpress/api-fetch package? I believe it has replaced the @wordpress/api-request package.

Sorry about the confusion with the docs, @JulienMelissas. @chrisvanpatten is correct, @wordpress/api-request is deprecated in favour of @wordpress/api-fetch, as of #7566.

The npm install command is supposed to show a message to warn you @wordpress/api-request is deprecated, but it isn't showing when I test it. @ntwb, could you look into this, please?

@gziolo, @youknowriad: Could we publish an update to the @wordpress/api-request readme, showing that it's deprecated?

I've edited the handbook page to point to @wordpress/api-fetch.

Thank you both for the quick reply. Smart move to using fetch. I lost internet with some down trees due to the storm (Florence) yesterday but I'm back at it today and will give it a test.

I can confirm I didn't see a deprecation warning on npm install.

Feel free to close this out if you would like, but yes, a docs update would be great. Please let me know if I can be of help with that.

Just reporting in to say apiFetch works great (nonce middleware ftw). Thanks.

npmjs.org and npm install now show a deprecated message. 馃檪

screen shot 2018-09-25 at 09 24 03

Not sure why it wasn't originally showing the deprecated message, anyways, fixed now 馃憤

Was this page helpful?
0 / 5 - 0 ratings