Create Text Field Programmatically to Media Type I want to create a text field to videos with code. I have the following in the .install file in a custom module:

function video_update_8101() {
  $field_name = 'field_video_alternative_text';
  // Make sure the field doesn't already exist.
  if (!FieldStorageConfig::loadByName('file', $field_name)) {
    // Create the field.
    $field = FieldStorageConfig::create(array(
      'field_name' =&gt; $field_name,
      'type' =&gt; 'text',
      'settings' =&gt; array('max_length' =&gt; 64),
    ));
    $field-&gt;save();

    // Create the instance.
    $instance = FieldStorageConfig::create(array(
      'field_name' =&gt; $field_name,
      'entity_type' =&gt; 'file',
      'bundle' =&gt; 'video',
      'label' =&gt; 'Alternative tekst',
      'description' =&gt; 'En beskrivelse kommer her.',
      'required' =&gt; TRUE,
    ));
    $instance-&gt;save();
  }
}


I get the following when running update.php

Attempt to create a field storage field_video_alternative_text with no entity_type.                                                                                                                                                                                 [error]
Performing emu_video_update_8101                                                                                                                                                                                                                                    [ok]
Failed: Attempt to create a field storage field_video_alternative_text with no entity_type.


Furthermore, I want this to happen when I install the module. I suppose it's not at this point?

Edit: I got inspiration from this post: How do programmatically create fields for content types, and ADD them to the the content type form
In Drupal 7 fields were shared across all content types; in Drupal 8 they're shared across a single entity type.

As such you've just missed the entity_type when creating the storage:

$field = FieldStorageConfig::create(array(
  'entity_type' =&gt; 'file',
  'field_name' =&gt; $field_name,
  'type' =&gt; 'text',
  'settings' =&gt; array('max_length' =&gt; 64),
));


As an aside, hook_update_N is for updates only, those functions won't be executed upon install. The point of them is to allow older versions of the module to get up to date with the current version that they're upgrading to. The initial state of the latest version of the module should reflect the state the older version of the module would be in after the update. 

You can use hook_install or config files in config/install/*.yml to perform initial setup of your module upon install.