Skip to content

Instantly share code, notes, and snippets.

@LearningNerd
Last active July 10, 2024 10:43
Show Gist options
  • Save LearningNerd/08a5039a7c5cddf7342ecd0a32da94e3 to your computer and use it in GitHub Desktop.
Save LearningNerd/08a5039a7c5cddf7342ecd0a32da94e3 to your computer and use it in GitHub Desktop.
Using import/export for front-end modules, example 1
export function helper(x) {
console.log(x);
}
<!DOCTYPE html>
<html lang="en">
<head>
<!-- ..... other <head> stuff here .... -->
<!-- *** IMPORTANT: use type="module" here! *** -->
<script type="module" src="main.js"></script>
</head>
<body>
<!-- ... <body> stuff here ... -->
</body>
</html>
// IMPORTANT:
// - Must preface relative file paths with / or ./ etc
// - Must include the .js at end of the file name
// - Must surround named imports with { }
import {helper} from "./helper.js";
helper("This works!");
@harivonyR
Copy link

In javascript front-end, module imported must end with .js in path. Overwise it will not work

@a-creamy
Copy link

Thanks for the example

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment