Problem 1:

You have a circular dependency - app.js imports calc.js, but calc.js imports app.js.

Usually the module system will ensure all of a module's dependencies are executed before the module itself, but this is impossible when your dependency tree has cycles - it has to pick one of them to run first. In this case, calc.js is run first, at which point Test isn't yet defined!

There are ways to work around this if you're careful, but cyclic dependencies are pretty much always a sign that you're structuring your code wrong - the best solution is to reorganize it so you no longer have a cycle.

Problem 2:

ES2016 modules don't execute in the global scope. If you want to make something available to the window as a whole, you need to be explicit:

window.Calc = Calc;
Answer from Joe Clay on Stack Overflow
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Statements › import
import - JavaScript - MDN Web Docs - Mozilla
For example, if you have import x from "foo", then the runtime will look for the foo package within any node_modules directory in the parent directories of the current module. This behavior can be reproduced in browsers using import maps, which also enable you to customize resolution in other ways. The module resolution algorithm can also be executed programmatically using the import.meta.resolve function defined by the HTML spec.
🌐
Mozilla
developer.mozilla.org › en-US › docs › Web › JavaScript › Guide › Modules
JavaScript modules - MDN Web Docs - Mozilla
1 month ago - In the dynamic-module-imports directory we've got another example based on our classes example. This time however we are not drawing anything on the canvas when the example loads. Instead, we include three buttons — "Circle", "Square", and "Triangle" — that, when pressed, dynamically load the required module and then use it to draw the associated shape. In this example we've only made changes to our index.html ...
Discussions

How to use a external Javascript class in html? - Stack Overflow
Bring the best of human thought and AI automation together at your work. Explore Stack Internal ... I have a .js file which has a class defined. I want to call this class from More on stackoverflow.com
🌐 stackoverflow.com
How to export and import class in javascript
I have write a class in a abc.js file. I need to import that class within a html file. for that i am using export class myclass{ ---------------------- --------------------- }```. and import using `import myclass fro… More on forum.freecodecamp.org
🌐 forum.freecodecamp.org
0
0
September 24, 2019
javascript - How to import class from `js` file inside `html` file? - Stack Overflow
I have the following problem: I want to create an instance of a class inside HTML file. Here is my structure: and inside HTML body I want to do: ... More on stackoverflow.com
🌐 stackoverflow.com
how to import a javascript class
hi,I would like to import a javascript class to another file but unfortunately I couldn't successfully do that :)I tried something like this: //... More on community.smartbear.com
🌐 community.smartbear.com
2
0
September 1, 2017
🌐
freeCodeCamp
forum.freecodecamp.org › javascript
How to export and import class in javascript - JavaScript - The freeCodeCamp Forum
September 24, 2019 - I have write a class in a abc.js file. I need to import that class within a html file. for that i am using export class myclass{ ---------------------- --------------------- }```. and import using `import myclass fro…
Top answer
1 of 1
2

Developing without a bundler feels very stone aged nowadays. I strongly recommend using a bundler. I recommend Vite.

The OP's goal is to change and debug files inside node_modules/@stackoverflow/stacks-editor/.

I don't know why but it was interesting to setup Vite to handle that. The stacks-editor NPM package provides 2 ways of embedding: through a bundler (EMS or CommonJS) and <script>. The <script> links already bundled code. So we go with Vite to handle the project.

And we have an unsolved problem here: the NPM package is/has CommonJS dependencies and Vite doesn't support excluding CommonJS packages from its dependency pre-bundling! So we are stuck with the Vite's dev server...

The next try is to work with the build and ask Rollup to do some stuff. Basically we have watch option to watch a directory inside node_modules and that example was in the feature request of adding watch option! For some reason the option doesn't work and using npx vite build --watch doesn't rebuild the project...

My next step will be reporting the issue on the Rollup's github.

So... We have the last resort: we watch files and re-run build.
On linux for that I've installed entr and run the command:

find node_modules/@stacksoverflow/stacks-editor | entr npx vite build

Now we can change the code inside the stacks-editor code and the page is rebuilt automatically...

To debug comfortably with the build we need turn off minification & treeshaking.

The only problem you don't debug your original files but a compiled one.

So now when I write in the source:

export class StacksEditor {
    constructor(target, content, options = {}) {
        debugger;
        console.log('EDITOR CONSTRUCTOR')
        // do a deep merge of the passed options with our default options
        this.options = deepMerge(StacksEditor.defaultOptions, options);
        this.target = target;
        // naively generate a random internalId for this editor instance
        this.internalId = generateRandomId();
        this.innerTarget = document.createElement("div");
        this.target.appendChild(this.innerTarget);
        this.setupPluginContainer();
        this.pluginProvider = new ExternalPluginProvider(this.options.editorPlugins, this.options);
        this.setBackingView(this.options.defaultView, content);
    }

and load the page I get:

vite.config.js

import { defineConfig } from 'vite';

export default defineConfig({
    build: {
        minify: false,
        rollupOptions: {
            treeshake: false
        }
    }
});

package.json

{
  "name": "stacks-editor",
  "private": true,
  "version": "0.0.0",
  "type": "module",
  "scripts": {
    "dev": "vite",
    "build": "vite build",
    "preview": "vite preview"
  },
  "devDependencies": {
    "vite": "^4.3.9"
  },
  "dependencies": {
    "@stackoverflow/stacks-editor": "^0.8.7"
  }
}


index.html

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Vite App</title>
  </head>
  <body>
    <div id="editor-container"></div>
    <script type="module" src="/main.js"></script>
  </body>
</html>

main.js

import { StacksEditor } from "@stackoverflow/stacks-editor";
// don't forget to include the styles as well
import "@stackoverflow/stacks-editor/dist/styles.css";
// include the Stacks js and css as they're not included in the bundle
import "@stackoverflow/stacks";
import "@stackoverflow/stacks/dist/css/stacks.css";

new StacksEditor(
    document.querySelector("#editor-container"),
    "*Your* **markdown** here"
);

Install, build and run Vite production server:

npm install
find node_modules/@stacksoverflow/stacks-editor | entr npx vite build
npx vite preview
🌐
Bobby Hadz
bobbyhadz.com › blog › javascript-import-class-from-another-file
Import and export Classes and Functions in JavaScript | bobbyhadz
We used a default import to import the Employee class and a named import to import the Person class. Note that you can only have a single default export per file, but you can have as many named exports as necessary. To import a function from another file in JavaScript:
🌐
Ccoenraets
ccoenraets.github.io › es6-tutorial › classes
Using Classes - ECMAScript 6 Tutorial
In main.js, remove the Mortgage class definition. Import the mortgage module.
Find elsewhere
🌐
JavaScript.info
javascript.info › tutorial › the javascript language › modules
Modules, introduction
Async scripts run immediately when ready, independently of other scripts or the HTML document. For module scripts, it works on inline scripts as well. For example, the inline script below has async, so it doesn’t wait for anything. It performs the import (fetches ./analytics.js) and runs when ready, even if the HTML document is not finished yet, or if other scripts are still pending.
🌐
JavaScript.info
javascript.info › tutorial › the javascript language › modules
Export and Import
So, remember, import needs curly braces for named exports and doesn’t need them for the default one. Technically, we may have both default and named exports in a single module, but in practice people usually don’t mix them. A module has either named exports or the default one. As there may be at most one default export per file, the exported entity may have no name. For instance, these are all perfectly valid default exports: export default class { // no class name constructor() { ...
🌐
IQCode
iqcode.com › code › javascript › javascript-classes-and-how-to-import-them
javascript classes and how to import them Code Example
January 22, 2022 - // import the class // From (location of class) import Example from "Math.js"; // Create a linked variable / instance of the class Example MyClass = new Example(); // Check if class works MyClass.test(); ... //import it import Example from './file2'; //Create an Instance var myInstance = new ...
🌐
Tania's Website
taniarascia.com › understanding modules, import and export in javascript
Understanding Modules, Import and Export in JavaScript | Tania Rascia's Website
In this tutorial, you will learn what a JavaScript module is and how to use import and export to organize your code. Before the concept of modules appeared in JavaScript, when a developer wanted to organize their code into segments, they would create multiple files and link to them as separate scripts. To demonstrate this, create an example index.html file and two JavaScript files, functions.js and script.js.
Top answer
1 of 4
41

I went through this and I've a solution with a third js file as module. rectangle.js will be same and myHifiRectangle.js file have only one modification.

import Rectangle from './rectangle.js';

export default class MyHiFiRectangle extends Rectangle {
      constructor(height, width) {
      super(height,width);
      this.foo= "bar";  
   }
}

Now, we need a third file which will be a module file, let say, script.js

import MyHiFiRectangle from './myHifiRectangle.js'

var v = new MyHiFiRectangle(2,4);
console.log(v.foo);

Now, the third file, script.js should be made a module. More on modules here. I have all three files under modelJS folder.

<script type="module" src="/modelJS/script.js"></script>

Now, when you run, you should see 'bar' getting printed in the developer tool's console tab.

2 of 4
6

I'm also adding an answer after getting hint from curiou.netter's answer in this thread.

I will point out the errors in precise sequential manner in the original code files. By the way, I was able to fix the issue without involving additional script.js file:

  1. While referring to JS modules, the script type should be module instead. I was referring to myHiFiRectancle.js like a regular JS file using src tag as:

    src="Scripts/myHiFiRectangle.js"
    

    I also imported MyHiFiRectangle module. Here is how the head tag now looks in the test.html file after fixing this error:

    <head>
      <meta charset = "UTF-8">
      <title>Javascipt by Rasik Bihari Tiwari</title>
      <script type="module">
         import MyHiFiRectangle from './scripts/myHiFirectangle.js';
         var v = new MyHiFiRectangle(2,4);
         console.debug(v.foo);
      </script>
    </head>
    
  2. export default statement was missing in myHiFiRectangle.js file. Every class has to be exported as a module when it has to be used from a different place. The rectified myHiFiRectangle.js file looks like below:

    import Rectangle from './rectangle.js';
    export default class MyHiFiRectangle extends Rectangle {
      constructor(height, width) {
      super(height,width);
      this.foo= "bar";  
     }
    }
    
  3. My script files had another error in the way I was importing modules.

    Incorrect way:

    import Rectangle from 'rectangle.js';
    import MyHiFiRectangle from '/scripts/myHiFirectangle.js';
    

    It causes below error which is self explanatory:

    Uncaught TypeError: Failed to resolve module specifier "rectangle.js". Relative references must start with either "/", "./", or "../".

    Correct way:

    import Rectangle from './rectangle.js';
    import MyHiFiRectangle from './scripts/myHiFirectangle.js';
    
🌐
Exploring JS
exploringjs.com › es6 › ch_modules.html
16. Modules
//------ main2.js ------ import MyClass from 'MyClass'; const inst = new MyClass(); Note that there is no semicolon at the end if you default-export a function or a class (which are anonymous declarations). Even though JavaScript never had built-in modules, the community has converged on a ...
🌐
Simmons University
web.simmons.edu › ~grabiner › comm244 › weeknine › including-javascript.html
Including JavaScript In Your Page
Here's a very simple demonstration of how to include an external JavaScript file into an HTML page. ... For this class you are not expected to write any actual JavaScript code.
🌐
xjavascript
xjavascript.com › blog › how-to-import-export-a-class-in-vanilla-javascript-js
How to Import and Export Classes in Vanilla JavaScript (ES6 Modules) with HTML Integration — xjavascript.com
In this guide, we’ll focus on a common use case: importing and exporting classes (a core ES6 feature) using Vanilla JS modules, with step-by-step HTML integration. By the end, you’ll master modular class management and avoid common pitfalls. ... Exporting Classes: Named vs. Default Exports · Importing Classes: Bringing Exports Into Your Code ... An ES6 module is a JavaScript file that exports variables, functions, or classes, allowing other files (modules) to import and use them.
🌐
W3Schools
w3schools.com › js › js_modules.asp
JavaScript Modules
JS Examples JS HTML DOM JS HTML Input JS HTML Objects JS HTML Events JS Browser JS Editor JS Exercises JS Quiz JS Website JS Syllabus JS Study Plan JS Interview Prep JS Bootcamp JS Certificate JS Reference ... Modules are code blocks that can export and/or import functions and values.