In Drupal 8 to create a node programmatically?

In General, make a small script to add the record content of a particular type. It is certainly possible to write bare sql, but it is very gemorno. How to add records using the tools Drupal?
Found these examples on request "drupal 8 create node programmatically"

use Drupal\node\Entity\Node;
$node = Node::create([
// The node entity bundle.
]);
$node->save();

But just writing it in a separate php script that lies in the root directory get the error "Class 'Drupal\node\Entity\Node' not found". How to use it?
June 10th 19 at 14:30
2 answers
June 10th 19 at 14:32
To use this:
<?php
use Drupal\Core\DrupalKernel;
use Symfony\Component\HttpFoundation\Request;
use Drupal\node\Entity\Node;

define('DRUPAL_DIR', '/path/to/root');
$autoloader = require_once DRUPAL_DIR . '/autoload.php';
$kernel = new DrupalKernel('prod', $autoloader);
$request = Request::createFromGlobals();
chdir(DRUPAL_DIR);
$response = $kernel--->handle($request);
$kernel->terminate($request, $response);

$nid = 1488;
$node = Node::load($nid);
echo $node->body->value;


This script is run from console:

$php show_node_content.php
June 10th 19 at 14:34
Use hook_update() or hook_install() in your module instead of obscure file in the root directory - this is the use of tools the Drupal. And the file doesn't work, because it required classes are not connected and the core of Drupal has not been initialized, see how it works index.php for example.

Find more questions by tags Drupal