Uncaught Error: Module name "@google-cloud/pubsub" has not been loaded yet for context: _. Use require([])

Viewed 13

I am trying to run JS file with a on-click action of HTML button and I am getting such error even after using requirejs as prompted. Attached both error and my code. I have also installed the necessary package in the project directory.

Uncaught Error: Module name "@google-cloud/pubsub" has not been loaded yet for context: _. Use require([])

index.html

> <!DOCTYPE html> <html> <body>
> 
> <h1>HTML DOM Events</h1>
> 
> 
> <button id="main">DELETE</button>
> 
> <p id="demo"></p> <script
> src="https://requirejs.org/docs/release/2.3.5/minified/require.js" >
> 
> </script> <script type="module" src="pub.js">
> 
> </script>
> 
> </body> </html>

pub.js

let a1=["test","test-2"]
let a2=["us-west4-b","us-west4-b"]
let t="compute/Instance"
let d_index = [2]


const {PubSub} = require('@google-cloud/pubsub');

  
  let main = document.getElementById('main');
  main.onclick =  function(
      topicNameOrId = 'delete_msg_compute_vm',
      data = JSON.stringify({name: a1[d_index[i]],type: t, location: a2[d_index[i]]})
    ) {
    
      for(let i=0;i<1;i++){
      // Imports the Google Cloud client library
      // = requirejs('@google-cloud/pubsub');
      
      
      // Creates a client; cache this for further use
      const pubSubClient = new PubSub({keyFilename: 'buoyant-country-360907-a85239705bfe.json'});
    
      async function publishMessage() {
        // Publishes the message as a string, e.g. "Hello, world!" or JSON.stringify(someObject)
        const dataBuffer = Buffer.from(data);
    
        try {
          const messageId = await pubSubClient
            .topic(topicNameOrId)
            .publishMessage({data: dataBuffer});
          console.log(`Message ${messageId} published.`);
        } catch (error) {
          console.error(`Received error while publishing: ${error.message}`);
          process.exitCode = 1;
        }
      }
    
      publishMessage();
     
    }
    
  
  }
0 Answers
Related