I'm playing around with some ES6
features using Chrome v65
I have the following files:
base.js :
export function consoleLogger(message) {
console.log(message);
}
main.js
import { consoleLogger } from './base.js';
consoleLogger('Some message');
utils.js
function sayMyName(name) {
console.log(name);
}
imports.html
<!DOCTYPE html>
<html>
<head>
<script src='./main.js' type='module'></script>
<script src='./utils.js'></script>
</head>
<body>
<script>
sayMyName('Cool name');
</script>
</body>
</html>
Used like this everything seems fine, and in the console I get
Cool name utils.js:2
Some message base.js:2
However lets imagine scenario where I need some additional data in order to compose the message for the consoleLogger
method. Then I would like to have something like this in main.js
function logToConsole(msg) {
consoleLogger(msg);
}
and in imports.html
<script>
sayMyName('Cool name');
logToConsole('Send from log to console');
</script>
then for the logToConsole('Send from log to console');
call in my html
file in the console I get:
Uncaught ReferenceError: logToConsole is not defined at imports.html:10
So there is no problem to import the consoleLogger
from base.js
and call it directly in main.js
, there is no problem to include another .js file (utils.js
) and call methods from there, but if I try to call a method declared in main.js
which internally calls an imported method I got the error from above. As it seems it doesn't even matter if the method from main.js
is referencing an imported method or not. I just commented everything and left only one simple method
main.js
import { consoleLogger } from './base.js';
/*function logToConsole(msg) {
consoleLogger(msg);
}
consoleLogger('Some message');*/
function randomStuff() {
console.log('random stuff');
}
and in the console I got this error:
imports.html:11 Uncaught ReferenceError: randomStuff is not defined at imports.html:11
Can someone explain me the reason for this behavior?