How can I add a Twig template to a programatically created custom content type? I created a module implementing a content type. How can I set a custom template file for that content type and for the teaser and full view modes? I need to use the template files in the folder of my module, not the theme the site is currently using.

These are some details about my module.


Module name: My Module
Content type name: My Content
Teaser template name: node--my-content--teaser.html.twig
Full template name: node--my-content--full.html.twig
Field template name: field--node--field-my-field--my-content.html
Nodes
  
  Pattern: node--[type|nodeid]--[viewmode].html.twig
  
  Base template: node.html.twig (base location:
  core/modules/node/templates/node.html.twig)
  
  Theme hook suggestions are made based on these factors, listed from
  the most specific template to the least. Drupal will use the most
  specific template it finds:
  
  
  node--nodeid--viewmode.html.twig
  node--nodeid.html.twig
  node--type--viewmode.html.twig
  node--type.html.twig
  node--viewmode.html.twig
  node.html.twig
  
  
  Note that underscores in a content type's machine name are replaced by
  hyphens.


Source: Twig Template naming conventions

In your case this is number 3.

Example:

node--article--teaser.html.twig

If you want to place the template inside the module see: Template for custom content type

In your case this would look like this:

/**
 * Implements hook_theme().
 */
function my_module_theme() {
  return [
    'node__my_content__teaser' =&gt; [
      'base hook' =&gt; 'node',
    ],
    'node__my_content__full' =&gt; [
      'base hook' =&gt; 'node',
    ],
  ];


In my experience you only need base hook. Drupal will discover the correct template name and path on its own. You have to be careful with the underscores (_) in the theme hook, they should match exactly the hyphens (-) of the template name.