1

I've been trying to make ewelink-api to work on a web browser directl and not as "node script" I've followed the directions stated here: https://ewelink-api.vercel.app/docs/demos/serverless So i made the following html file:

<!DOCTYPE html>

<html>
  <head>
    <title>
        Test
    </title>
  </head>

  <body>
    <div id="demo"><div>
    
    <script>
      document.getElementById("demo").innerHTML = "Hello JavaScript!";
      //const ewelink = require('ewelink-api');
      
      /* first request: get access token and api key */
      (async () => {
        const connection = new ewelink({
          email: 'myemail@gmail.com',
          password: 'mypassword',
          region: 'eu',
        });
        const login = await connection.getCredentials();
        const accessToken = login.at;
        const apiKey = login.user.apikey
        const region = login.region;
      })();
      
      /* second request: use access token to request devices */
      (async () => {
        const newConnection = new ewelink({
          at: accessToken,
          region: region
        });
        const devices = await newConnection.getDevices();
        console.log(devices);
      })();
    </script>
  </body>
</html>

And i was expecting to see some devices logged to console, but instead i get this error:

Uncaught (in promise) ReferenceError: ewelink is not defined

I've tried to add this line of code at the beggining:

const ewelink = require('ewelink-api'); but then i get this error:

Uncaught ReferenceError: require is not defined

Can anyone help me? Thanks.

  • `require` is a Node thing - https://nodejs.org/en/knowledge/getting-started/what-is-require/. You may have trouble using the ewelink api directly from the browser. – phuzi May 24 '22 at 12:09
  • Does this answer your question? [Node-style require for in-browser javascript?](https://stackoverflow.com/questions/6971583/node-style-require-for-in-browser-javascript) – phuzi May 24 '22 at 12:13
  • I'm afraid What I'm trying to do is to run ewelink api from the browser and not to run reguire() from the browser. phuzi's answer is very important but i'am too much of a newbie to follow along. Since the ewelink team states clearly on the documentation that can run on serverless enviroment, i thought that it was something i was missing and was not working. – user2358363 May 25 '22 at 04:40
  • 1
    I think you may be misunderstanding what "serverless" means! From [What is servless](https://www.redhat.com/en/topics/cloud-native-apps/what-is-serverless) _"Serverless is a cloud-native development model that allows developers to build and run applications without having to manage servers."_ It doesn't mean without a server. A server is required but your app/script is started for every request e.g. [AWS Lambda](https://aws.amazon.com/lambda/) – phuzi May 25 '22 at 07:21
  • Although it does say that it can run in browsers!... – phuzi May 25 '22 at 07:23
  • Did you try just including the ewelink-api JavaScript using a ` – phuzi May 25 '22 at 07:45
  • phuzi : Yes this is what i did. – user2358363 May 26 '22 at 05:15

1 Answers1

0

require is for Node.JS (as phuzi states) , import is a Javascript/ECMAScript (for browsers) standard.

see https://fjolt.com/article/javascript-export-import-node-js

but 'ewelink-api' and ev. dependents may also uses require,
so you had to replace all, maybe RequireJS helps

see also Client on Node.js: Uncaught ReferenceError: require is not defined

btw: the two async-calls should (must) be nested to be executed in sequence, here they are executed concurrently, so accessToken ... will be undefined in newConnection