'Gutenberg cannot seem to find TextControl

I have include @wordpress/components in my build. (I ran npm install wordpress/components and see it in package.json)

I import the TextControl thus: import {TextControl} from "@wordpress/components";

But at this line I see in the console: Uncaught TypeError: Cannot read properties of undefined (reading 'TextControl')

I am using wordpress-scripts package. Obviously I am missing a step.

What might it be?!



Solution 1:[1]

There were various problems but the main one I was not loading the dependencies that the wp-scripts build puts into a file with the name pluginname.asset.php. For example;

<?php return array('dependencies' => array('wp-components', 'wp-element'), 'version' => '123');

and in your wp_enqueue_script script call you have to send these dependencies as a parameter.

wp_enqueue_script('name', 'path.js', $script_asset['dependencies'], $script_asset['version']);

It looks like Gutenberg uses this to decide which bits of itself to load. They are not included the built js file.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Kropotkin