It depends on what environment you're running in. In a web browser you simply need to make sure that file1.js is loaded before file2.js:

<script src="file1.js"></script>
<script src="file2.js"></script>

In node.js, the recommended way is to make file1 a module then you can load it with the require function:

require('path/to/file1.js');

It's also possible to use node's module style in HTML using the require.js library.

Answer from slebetman on Stack Overflow
Discussions

How do you do to work with external js classes files and put this on a main.js file with modules?
Hi, My application seems like that : ├── index.html └── main.js My main.js load module like this : import * as THREE from "https://threejs.org/build/three.module.js"; import { OrbitControls } from "https://threejs.org/examples/jsm/controls/OrbitControls.js"; import { CSM } from ... More on discourse.threejs.org
🌐 discourse.threejs.org
11
0
May 24, 2021
Including JavaScript class definition from another file in Node.js - Stack Overflow
Can I move my User class definition to another javascript file and "include" it somehow? More on stackoverflow.com
🌐 stackoverflow.com
January 1, 2017
Typescript: how to import a class from a javascript file? - Stack Overflow
There is no export default class in JavaScript. What you can do is write your JS file like this. myClass/index.js · "use strict"; class MyClass { hello(name) { console.log(`Hello ${name}`); } } exports.default = MyClass; Create a Type definitions for it. myClass/index.d.ts · export default class MyClass { hello(name: string): void; } You can then import it into your TypeScript like this. /// import MyClass from ... More on stackoverflow.com
🌐 stackoverflow.com
How to import a javascript class and extend it in another script unit
Hi I am trying to use two Javascript projects - and export a class defined in one to be extended (inherited) in another. The example... More on community.smartbear.com
🌐 community.smartbear.com
6
0
April 9, 2019
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Statements › import
import - JavaScript - MDN Web Docs - Mozilla
Given a value named myExport which has been exported from the module my-module either implicitly as export * from "another.js" or explicitly using the export statement, this inserts myExport into the current scope. ... You can import multiple names from the same module.
🌐
GitHub
github.com › bobbyhadz › javascript-import-class-from-another-file
GitHub - bobbyhadz/javascript-import-class-from-another-file: A repository for an article at https://bobbyhadz.com/blog/javascript-import-class-from-another-file
The JS code is in the index.js file. To be able to run the code, follow these instructions: Clone the GitHub repository with the git clone command. Open your terminal in the project's root directory (right next to package.json). Install the node modules. ... To run the code, issue the npm start command. ... Alternatively, you can run the project in watch mode, so every time you save, the JavaScript server is restarted.
Author   bobbyhadz
🌐
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() { ...
🌐
Three.js
discourse.threejs.org › questions
How do you do to work with external js classes files and put this on a main.js file with modules? - Questions - three.js forum
May 24, 2021 - Hi, My application seems like that : ├── index.html └── main.js My main.js load module like this : import * as THREE from "https://threejs.org/build/three.module.js"; import { OrbitControls } from "https://threejs.org/examples/jsm/controls/OrbitControls.js"; import { CSM } from "https://threejs.org/examples/jsm/csm/CSM.js"; import { TWEEN } from 'https://unpkg.com/three@0.125.2/examples//jsm/libs/tween.module.min' i have also class inside my main.js file : class Player{ constructor() ...
Find elsewhere
🌐
DEV Community
dev.to › askyt › how-to-include-a-javascript-file-in-another-javascript-file-231f
How to Include a JavaScript File in Another JavaScript File - DEV Community
December 19, 2024 - This method is not natively supported in modern browsers without bundlers like Webpack or Rollup. For browser environments, you can include multiple JavaScript files directly using <script> tags in your HTML file.
🌐
Tutorial Republic
tutorialrepublic.com › faq › how-to-include-a-javascript-file-in-another-javascript-file.php
How to Include a JavaScript File in another JavaScript File
import { msg, PI, addNumbers } from './main.js'; console.log(msg); // Prints: Hello World! console.log(PI); // Prints: 3.14 console.log(addNumbers(5, 16)); // Prints: 21 · Alternatively, you can use the jQuery getScript() method to load a JS file from the server, then execute it with a sigle line of code.
🌐
SmartBear Community
community.smartbear.com › smartbear community › testcomplete › testcomplete questions
How to import a javascript class and extend it in another script unit | SmartBear Community
April 9, 2019 - The "import" keyword usage shows syntax error "Unexpected token import". If I use require, then I cannot actually use it with extends e.g. (Note that I have shared the Parent.js into the project) //In Parent.js class ParentApp { constructor(x) { this.name = x; } } module.exports = { ParentApp: ParentApp } //In Child.js var parent = require("Parent") class Child extends parent.ParentApp { constructor(y, z) { super(y); this.age = z; } }
🌐
Stanley Ulili
stanleyulili.com › node › node-modules-import-and-use-functions-from-another-file
Node.js Modules: Import and use Functions from Another File
March 26, 2020 - Adding the function to module.exports will make it available in any file that imports thelib.js module. You are not limited to exporting functions. You can export variables, objects, and classes, etc. To include functions defined in another file in Node.js, we need to import the module.
🌐
freeCodeCamp
forum.freecodecamp.org › javascript
How to add import module into class? - JavaScript - The freeCodeCamp Forum
March 25, 2021 - I made an export as follows in a file; export const cartBtn = document.querySelector('.cart-btn') export const closeCartBtn = document.querySelector('.close-cart') export const clearCartBtn = document.querySelector('.cl…
🌐
Processing Foundation
discourse.processing.org › p5.js › coding questions
Correct way to use ES6 modules and import classes - Coding Questions - Processing Community Forum
Hi, I am making a project in p5.js where I have a simple class called Stone that I want to import into another file using ES6 modules. So I used export default : // js/src/Stone.js export default class Stone { constructor(x, y, is_white) { this.x = x; this.y = y; this.is_white = is_white; } } And I have a main file called main.js which is included in the HTML as a module like this : // index.html And I import...
Published   May 28, 2020
🌐
MicroStudio
microstudio.dev › community › questions › javascript-how-to-reference-objects-from-other-files › 280
Javascript: How to reference objects from other files?
init = function() { import_class1() ... this issue does not really exist, probably due to your transpiler. What would be helpful is to give priority to one file, like "init"....
🌐
freeCodeCamp
forum.freecodecamp.org › javascript
Inheritance from another file - JavaScript - The freeCodeCamp Forum
January 28, 2022 - I’m not 100% sure I understand what you are asking but if my intuition is correct then you want to use JS modules. You can define and export a class in one file and then import into another · Yes. If you are importing a Class then you can use it the same way you would use any other Class, ...
🌐
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…