Creating a node programmatically

There are 3 methods of creating a node (or with some slight adjustments any content entity) in Drupal 8:

Procedural wrapper

<?php
$node = entity_create('node', [
  'title' => 'Title goes here',
  'body' => 'Body content goes here.',
  'type' => 'article', // node type (bundle) is mandatory to prevent NodeStorage from complaining 
]);

$node->save();

static create() method

<?php
use Drupal\node\Entity\Node;

$node = Node::create([
  'title' => 'Title goes here',
  'type' => 'page', // node type (bundle) is mandatory to prevent NodeStorage from complaining 
]);

$node->save();

Using the entity manager

$node = \Drupal::entityTypeManager()->getStorage('node')->create([
  'type' => 'page', 
  'title' => 'Title goes here'
]);

$node->save();

Saving a file with a node using the static create() method

<?php
use Drupal\node\Entity\Node;
use \Drupal\file\Entity\File;

$data = file_get_contents('https://pbs.twimg.com/media/CrkX7amWIAA2AIl.jpg');
$file = file_save_data($data, 'public://bordercollie.jpg', FILE_EXISTS_REPLACE);

$node = Node::create([
  'title' => 'Title goes here',
  'type' => 'page', // node type is mandatory
  'field_upload' => [
    'target_id' => $file->id(),
    'alt' => 'My uploaded file',
    'title' => 'Title attribute goes here'
  ],
]);

$node->save();