Skip to content

Instantly share code, notes, and snippets.

@muhammadghazali
Created September 13, 2017 16:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save muhammadghazali/d055e1b7b7b6cb3bab7f977cf17e163f to your computer and use it in GitHub Desktop.
Save muhammadghazali/d055e1b7b7b6cb3bab7f977cf17e163f to your computer and use it in GitHub Desktop.
This is a silly world hello module that can be used on the Node.js and browser environment. Credits to this blog post: https://caolan.org/posts/writing_for_node_and_the_browser.html
(exports => (exports.greet = () => "Hello world"))(
typeof exports === "undefined" ? (this["hello"] = {}) : exports
);
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<script src="hello.js"></script>
<script>
console.log(hello.greet());
</script>
</body>
</html>
// try to run this in your console
// node index.js
const hello = require("./hello.js");
console.log(hello.greet());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment