WP 5.3 apiFetch problems
Describe the bug
After updating WordPress to version 5.3 i can’t use
import apiFetch from '@wordpress/api-fetch';
i’m getting following error:
TypeError: "_wordpress_api_fetch__WEBPACK_IMPORTED_MODULE_12___default(...) is not a function"
To reproduce
import apiFetch from '@wordpress/api-fetch';
apiFetch is undefined
Expected behavior it should be a function
Screenshots

Desktop (please complete the following information):
- OS: Windows 10
- Browser [Firefox]
- Version 70
Additional context latest WordPress 5.3 and default TwentyTwenty theme
Hi There!
Are you using the Gutenberg plugin on your website? Which version are you using? Can you try updating?
Thanks.
Hi, @youknowriad! I'm using just Wordpress 5.3 with a Gutenberg in core, not as a separate plugin
Where is this code "import apiFetch from '@wordpress/api-fetch';" coming from? Is this one of your plugins? Based on your error, it seems like you're trying to "bundle" the api-fetch module in your own code. Did you try using wp.apiFetch and adding wp-api-fetch to your WordPress script dependencies?
i'm trying to create a block using Gutenberg documentation, and my code worked in WP 5.2, but stopped in WP 5.3
i'm using following package.json:
{
"name": "test-block",
"version": "1.0.0",
"license": "GPL-2.0-or-later",
"main": "build/index.js",
"devDependencies": {
"@wordpress/scripts": "^5.0.0"
},
"scripts": {
"start": "wp-scripts start",
"build": "wp-scripts build",
"lint-js": "wp-scripts lint-js"
}
}
and here is the code that worked before:
import { __ } from '@wordpress/i18n';
import { registerBlockType } from '@wordpress/blocks';
import { InspectorControls } from '@wordpress/block-editor';
import { PanelBody, RangeControl, SelectControl, TextControl, ToggleControl, QueryControls, Placeholder, Spinner } from '@wordpress/components';
import { Component, Fragment } from '@wordpress/element';
import { withSelect } from '@wordpress/data';
import { addQueryArgs } from '@wordpress/url';
import apiFetch from '@wordpress/api-fetch';
import { isUndefined, pickBy } from 'lodash';
class TestBlock extends Component {
constructor() {
super( ...arguments );
this.state = {
categoriesList: [],
}
}
componentWillMount() {
this.isStillMounted = true;
this.fetchRequest = apiFetch( {
path: addQueryArgs( `/wp/v2/categories`, {
tax: 'category'
}),
} ).then(
categoriesList => {
if ( this.isStillMounted ) {
this.setState( { categoriesList } );
}
}
).catch(
() => {
if ( this.isStillMounted ) {
this.setState( { categoriesList: [] } );
}
}
)
}
componentWillUnmount() {
this.isStillMounted = false;
}
render() {
const {
attributes: {
categories,
order,
order_by,
posts_to_show
},
latestPosts,
setAttributes,
clientId
} = this.props;
const { categoriesList } = this.state;
const hasPosts = Array.isArray( latestPosts ) && latestPosts.length;
const blockSettings = (
<InspectorControls>
<PanelBody title={ __( 'Query', 'test-plugin' ) } initialOpen={ true }>
<QueryControls
{ ...{ order, order_by } }
numberOfItems={ posts_to_show }
categoriesList={ categoriesList }
selectedCategoryId={ categories }
onOrderChange={ order => setAttributes( { order } ) }
onOrderByChange={ order_by => setAttributes( { order_by } ) }
onCategoryChange={ value => setAttributes( { categories: '' !== value ? value : undefined } ) }
onNumberOfItemsChange={ posts_to_show => setAttributes( { posts_to_show } ) }
/>
</PanelBody>
</InspectorControls>
);
if ( ! hasPosts ) {
return (
<Fragment>
{ blockSettings }
<Placeholder
icon="admin-post"
label={ __( 'Posts Grid', 'test-plugin' ) }
>
{ ! Array.isArray( latestPosts ) ?
<Spinner /> :
__( 'No posts found.', 'test-plugin' )
}
</Placeholder>
</Fragment>
)
}
return(
<Fragment>
{ blockSettings }
<div>
Output will be here
</div>
</Fragment>
);
}
}
registerBlockType( 'testblock/testblock-v1', {
title: __( 'Test', 'test-plugin' ),
category: 'common',
attributes: {
categories: {
type: 'string',
default: ''
},
order: {
type: 'string',
default: 'desc'
},
order_by: {
type: 'string',
default: 'date'
},
posts_to_show: {
type: 'number',
default: 10
},
},
edit: withSelect( ( select, props ) => {
const {
posts_to_show = 10,
order = 'desc',
order_by = 'date',
categories = ''
} = props.attributes;
const { getEntityRecords } = select( 'core' );
const latestPostsQuery = pickBy({
categories,
order,
orderby: order_by,
per_page: posts_to_show,
status: 'publish',
_embed: true,
}, value => ! isUndefined( value ) );
return {
latestPosts: getEntityRecords( 'postType', 'post', latestPostsQuery ),
}
} )( TestBlock ),
save() {
return null
}
} );
now when i run in my console "npm start", it compiles without issues, but when i try to add my block in Page Editor, i'm gettings following error:
TypeError: _wordpress_api_fetch__WEBPACK_IMPORTED_MODULE_13___default(...) is not a function
really i do not understand what's going wrong, and why this happens in WP 5.3.
i tried to install dev deps manually using
npm install @wordpress/api-fetch --save from this doc https://developer.wordpress.org/block-editor/packages/packages-api-fetch/
could you at lest point me please? :)
Sure, happy to help if I can.
How are you registering your script and block on the php side of things? Can you share the code as well?
i've figured it out!
when i register gutenberg block, in WP 5.3 now i need to add 'wp-api-fetch' script as dependency:
wp_register_script(
'test-block',
plugins_url( '/build/index.js', __FILE__ ),
[ 'wp-block-editor', 'wp-blocks', 'wp-element', 'wp-i18n', 'wp-polyfill', 'wp-api-fetch' ],
'210920191331'
);
hope this answer will be helpful for others!
@youknowriad thank you so much for blazing reaction and sorry for disturb!
One thing that helps in this situation is using wp-scripts for your build process, which automatically generates a build/index.deps.json file. That file contains all of the dependent scripts you're using.
So instead of this:
wp_register_script(
'test-block',
plugins_url( '/build/index.js', __FILE__ ),
[ 'wp-block-editor', 'wp-blocks', 'wp-element', 'wp-i18n', 'wp-polyfill', 'wp-api-fetch' ],
'210920191331'
);
...you'd do this:
wp_register_script(
'test-block',
plugins_url( '/build/index.js', __FILE__ ),
json_decode( file_get_contents( __DIR__ . '/build/index.deps.json' ) ),
'210920191331'
);