title: Google Cloud Pub/Sub Node.js Sample: TypeError: Cannot read property 'on' of null
body: <p>I'm using GCP and I want to use Cloud Pub/Sub. I got this error below when I tried Node.js sample. Does anyone knows how to fix it?</p>
  
  <pre><code>/private/tmp/pubsub/pubsubsample.js:26
    subscription.on('error', onError);
                ^
  
  TypeError: Cannot read property 'on' of null
      at /private/tmp/pubsub/pubsubsample.js:26:15
      at /private/tmp/pubsub/node_modules/gcloud/lib/pubsub/index.js:474:7
      at Object.handleResp (/private/tmp/pubsub/node_modules/gcloud/lib/common/util.js:113:3)
      at /private/tmp/pubsub/node_modules/gcloud/lib/common/util.js:422:12
      at Request.onResponse [as _callback] (/private/tmp/pubsub/node_modules/gcloud/node_modules/retry-request/index.js:106:7)
      at Request.self.callback (/private/tmp/pubsub/node_modules/gcloud/node_modules/request/request.js:198:22)
      at emitTwo (events.js:87:13)
      at Request.emit (events.js:172:7)
      at Request.&lt;anonymous&gt; (/private/tmp/pubsub/node_modules/gcloud/node_modules/request/request.js:1035:10)
      at emitOne (events.js:82:20)
  </code></pre>
  
  <p><a href="https://github.com/GoogleCloudPlatform/gcloud-node" rel="nofollow noreferrer">https://github.com/GoogleCloudPlatform/gcloud-node</a></p>
  
  <pre><code>var gcloud = require('gcloud');
  
  // Authenticating on a per-API-basis. You don't need to do this if you
  // auth on a global basis (see Authentication section above).
  
  var pubsub = gcloud.pubsub({
    projectId: 'xxxxx',
    keyFilename: 'xxx.json'
  });
  
  // Reference a topic that has been previously created.
  var topic = pubsub.topic('info');
  
  // Publish a message to the topic.
  topic.publish({
    data: 'New message!'
  }, function(err) {});
  
  // Subscribe to the topic.
  topic.subscribe('new-subscription', function(err, subscription) {
    // Register listeners to start pulling for messages.
    function onError(err) {}
    function onMessage(message) {}
    subscription.on('error', onError);
    subscription.on('message', onMessage);
  
    // Remove listeners to stop pulling for messages.
    subscription.removeListener('message', onMessage);
    subscription.removeListener('error', onError);
  });
  </code></pre>
  
  <p>... I'm using now PubSub but I'm thinking whether I can do same thing by using Google Cloud PubSub.</p>
  
  <p>This post may be relevant. <a href="https://stackoverflow.com/questions/30677889/node-js-on-google-cloud-platform-pub-sub-tutorial-worker-is-failing-with-typeer">Node.js on Google Cloud Platform Pub/Sub tutorial worker is failing with &quot;TypeError: Cannot call method &#39;on&#39; of null&quot;</a></p>
  
  <hr>
  
  <p>Update 1</p>
  
  <p>I changed to this code but same error was showed. </p>
  
  <p>(error)</p>
  
  <pre><code>subscription.on('error', onError);
              ^
  TypeError: Cannot read property 'on' of null
  </code></pre>
  
  <p>(code)</p>
  
  <pre><code>// Subscribe to the topic
  topic.subscribe('new-subscription', function(err, subscription) {
    if( err ) {
      // something went wrong, react!
      return;
    }
  
    // Register listeners to start pulling for messages.
    function onError(err) {}
    function onMessage(message) {}
    subscription.on('error', onError);
    subscription.on('message', onMessage);
  
    // Remove listeners to stop pulling for messages.
    subscription.removeListener('message', onMessage);
    subscription.removeListener('error', onError);
  });
  </code></pre>
  
  <hr>
  
  <p>Update 2</p>
  
  <p>My expectation is this below.</p>
  
  <ol>
  <li>execute "node pubsub.js"</li>
  <li>I can see the sample message 'New message!'</li>
  </ol>