+ 3
Cannot find module.js for import //SOLVED;
I was coding an example about import export for a question here. https://gorgorgordon.github.io/MySoloLearnCodes/exportimport.html However, the import cannot find module.js The console logged error 404. Here is my repo: https://github.com/gorgorgordon/MySoloLearnCodes module.js export modules exportimport.html import from module.js
3 Antworten
+ 3
Original Code:
import {today, sum} from './module';
console.log(myModule.today);
Update1:
import {today, sum} from './module.js';
console.log(myModule.today);
//Reason: While using modules in browser, original extention of module/script is necessary, 'module' is incorrect but 'module.js' is correct path.
Update2:
import {today, sum} from './module.js';
console.log(today);
/*Reason:
As you are de-structuring the imported objects, the de-structured values should be used. If you'd do
"import modulName from './module.js';" then, you should do console.log(modulName.today);
And again, update the module export with
export default ({
sum:(x, y) => x + y,
today: () => new Date().toLocaleString("en-US")
})
*/
Update3:
import {today, sum} from './module.js';
console.log(today());
/*Reason: Maybe you want to call the today function */
+ 6
Try changing your module.js to this:
export const today = () => new Date().toLocaleString("en-US");
export const sum = (x, y) => x + y;
Notice i changed "today" to be a function. You wouldn't want the code to be run for a few days, and having the today value to be evaluated once when the code was initialized.
Also, lookie here:
https://www.sitepoint.com/using-es-modules/
+ 1
It works, so I was missing a .js, thanks Sarthak 🇳🇵 ~