Last active
July 10, 2024 10:43
-
-
Save LearningNerd/08a5039a7c5cddf7342ecd0a32da94e3 to your computer and use it in GitHub Desktop.
Using import/export for front-end modules, example 1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export function helper(x) { | |
console.log(x); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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!"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In javascript front-end, module imported must end with .js in path. Overwise it will not work