0

i try to use miIO library from https://github.com/aholstenson/miio but when i try use it i got error ReferenceError: require is not defined

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
<script type="text/javascript">

    const mio = require('./lib');

  mio.device({ address: '192.168.31.148' })
  .then(device => console.log('Connected to', device))
  .catch(err => handleErrorHere);

</script>
</body>
</html>

can anyone help me to explain why this code got an error ?

2
  • Yes it's a NodeJS module, meaning it runs in Node (on a server), not in a browser. miio is MIT-licensed and requires at least Node 6.6.0. Commented Jan 3, 2019 at 15:48
  • @Deiv — Browserify won't turn most dependancies on Node APIs into things that work in a browser. Commented Jan 3, 2019 at 15:49

2 Answers 2

1

The documentation you link to says:

miio is MIT-licensed and requires at least Node 6.6.0.

You are trying to run it in a web browser instead of in Node.

You need to run it in Node.

Sign up to request clarification or add additional context in comments.

Comments

-1

If you want to use require without nodejs then you'll need to use Browserify/RequireJS, as outlined in this question: How to use JS require() without Node.js

5 Comments

The OP wants to use the library they have found. Support for require is not sufficient. It also needs the ability to handle network packets. That's impossible in a web browser which can only do HTTP and web sockets.
The answers there mention you can use these to loading modules for use in the browser. See this specific answer: stackoverflow.com/a/21818631/7207514
That question specifically addresses the use of require and lets you use require in a browser. As mentioned in my previous comment, supporting require is not sufficient to solve the problem posed by the question. If you require something in a browser that won't run in a browser, then it still won't run.
How is it not sufficient to solve the problem? OP is having issues with require, to fix that and load the module they must use RequireJS or other similar tools. You are simply saying what the issue is, but I am linking a solution that can help fix the issue. Obviously it won't run with using require, but this will allow OP to load the module as they clearly wish to achieve.
And after they fix that and load the module … it still won't work because the module needs other things that the browser does not support. You aren't linking to a solution, you are linking to a red herring that will just uncover a different error (but another symptom of the same problem) that can't be fixed in the browser.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.