2

I need to export and import a plain js file to work keyboard navigation correctly. I am following the example here,and using import and export pattern of ES5 but it is not linking one js file to another. https://www.w3.org/TR/wai-aria-practices-1.1/examples/menubar/menubar-1/menubar-1.html# This is my codepen.io link https://codepen.io/ankita-sharma-the-flexboxer/project/editor/DzdmBE

module.exports = PopupMenu;
var myModule = require('./popuplinks');

Ankita Sharma
  • 23
  • 1
  • 5
  • does this answer of your question [link](https://stackoverflow.com/questions/950087/how-do-i-include-a-javascript-file-in-another-javascript-file) – نور Aug 31 '20 at 16:05

1 Answers1

1

There are multiple ways of exporting and importing JavaScript modules, in the past, we were using CommonJS modules which are in a format you've presented, they can be used in following way.

// module.js
const myModule = "something"
module.exports = myModule
// And then if you want to import in another file, you're using following sentence.
const module = require("./module")

Actually we're using ES6-like imports, you can read about them at MDN, I'm attaching a small sample of ES6+ export.

// module.js
const myModule = "Something"
export { myModule }
// And then
import {myModule} from './module'

Actually you should read post that will resolve your issue

keinsell
  • 426
  • 5
  • 16
  • Do i need to use call module() after the require statement? const module = require("./module"); module(); – Ankita Sharma Aug 31 '20 at 16:15
  • Also I am calling one js file init method into another js file after creating object from the constructor of that file. which seems to be not working after using import , export also. – Ankita Sharma Aug 31 '20 at 16:16
  • https://www.w3.org/TR/wai-aria-practices-1.1/examples/menubar/menubar-1/menubar-1.html# Please see the JS files here. – Ankita Sharma Aug 31 '20 at 16:17
  • According to your code... You're importing file ("module") that don't exist in your foler, and obiviously nothing happens because doesn't exist. – keinsell Aug 31 '20 at 16:18
  • Additionally @AnkitaSharma you don't import your scripts to HTML file and that can be potential problem with your code. – keinsell Aug 31 '20 at 16:20
  • though im using correct import, export method , I think the init method means the constructor of one js file does not get called from another JS file, thats why I am not able to see the exact output as mentioned in w3 site in my codepen. How can i resolve that? – Ankita Sharma Aug 31 '20 at 17:04
  • If you can please push this code to your GitHub or something because your Codepen is read-only and I cannot change anything. – keinsell Sep 01 '20 at 18:30