Why i get the error "Call to undefined function node_object_prepare()" while i try to add a node programmatically in Drupal 7? i'm trying to add a node content programmatically from a custom module file. I have created the .module file and used this code as suggested:

$node = new stdClass(); // We create a new node object
$node-&gt;type = "page"; // Or any other content type you want
$node-&gt;title = "Your title goes jere";
$node-&gt;language = LANGUAGE_NONE; // Or any language code if Locale module is enabled.
$node-&gt;path = array('alias' =&gt; 'your node path'); // Setting a node path
node_object_prepare($node); // Set some default values.
$node-&gt;uid = 1; // Or any id you wish

// Let's add standard body field
$node-&gt;body[$node-&gt;language][0]['value'] = 'This is a body text';
$node-&gt;body[$node-&gt;language][0]['summary'] = 'Here goes a summary';
$node-&gt;body[$node-&gt;language][0]['format'] = 'filtered_html'; // If field has a format,  you need to define it. Here we define a default filtered_html format for a body field

$node = node_submit($node); // Prepare node for a submit
node_save($node); // After this call we'll get a nid


but when i enable the module what i get is an error saying "Call to undefined function node_object_prepare()"

Thanks.
I think you will have to include node.pages.inc to be able to use this function from within your module:

module_load_include('inc', 'node', 'node.pages');


I just tried the using node_object_prepare() from a sample module without using the above code and it seems to work just fine. As @Clive mentioned is his comment you should add your code for creating a node programatically inside a module hook and not directly in your module file.

// create a node using 'hook_menu' in your module, this might not be the
// best hook for doing this but it should work
function &lt;MODULE&gt;_menu() {
  $node = new stdClass();
  $node-&gt;type = "page";
  $node-&gt;title = "Your title goes jere";
  $node-&gt;language = LANGUAGE_NONE;
  $node-&gt;path = array('alias' =&gt; 'your node path');
  node_object_prepare($node);
  // ...
}  


Reference: Module hooks