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
๐ŸŒ
MDN Web Docs
developer.mozilla.org โ€บ en-US โ€บ docs โ€บ Web โ€บ JavaScript โ€บ Reference โ€บ Statements โ€บ import
import - JavaScript - MDN Web Docs - Mozilla
The imported bindings are called live bindings because they are updated by the module that exported the binding, but cannot be re-assigned by the importing module. In order to use the import declaration in a source file, the file must be interpreted by the runtime as a module.
Discussions

javascript - Include es6 class from external file in Node.js - Stack Overflow
@BaldBantha this is because this is new ES6 syntax, you're going to need to use babel to transpile your ES6 to ES5 so node can run it. Node doesn't support import/export and never will (at least in the near future) 2016-08-18T01:12:34.683Z+00:00 ... export class MyClass { constructor(arg){ ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
Correct way to use ES6 modules and import classes
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 { conโ€ฆ More on discourse.processing.org
๐ŸŒ discourse.processing.org
1
1
May 28, 2020
Including JavaScript class definition from another file in Node.js - Stack Overflow
Communities for your favorite technologies. Explore all Collectives ยท Ask questions, find answers and collaborate at work with Stack Overflow for Teams More on stackoverflow.com
๐ŸŒ stackoverflow.com
January 1, 2017
javascript - How to import a JS file in another JS file in ES6? - Stack Overflow
I have got this node server and a bunch of JS classes in my js directory. I want to create a file called "exports.js" that exports all the classes required by server (using exports.Classname = class More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
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:
๐ŸŒ
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() { ...
๐ŸŒ
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
Since ECMAScript 6 (or ES6) you can use the export or import statement in a JavaScript file to export or import variables, functions, classes or any other entity to/from other JS files.
๐ŸŒ
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
Find elsewhere
๐ŸŒ
Ccoenraets
ccoenraets.github.io โ€บ es6-tutorial โ€บ classes
Using Classes - ECMAScript 6 Tutorial
Since this is an alternative implementation rather than the logical continuation of the previous implementation, make a copy of index.html and main.js in case you want to go back to that version. In main.js, remove the import statement at the top of the file...
๐ŸŒ
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 - // Dynamically import the module import('./math.mjs').then((module) => { console.log(module.add(2, 3)); // Output: 5 console.log(module.pi); // Output: 3.14159 }); ... Useful for optimizing performance in large applications by loading code only when needed. Including JavaScript files in other JavaScript files is a foundational concept in web development. The method you choose depends on the environment you're working in and the needs of your project. For modern development, ES6 modules are the go-to solution, offering powerful features like tree-shaking and dynamic imports.
Top answer
1 of 2
5

On the Salesforce platform, you can't import a specific file from another module. You can only import the entry file.

If you want module2.js to import some value exported by module1-extra.js this value has to be re-exported in the module entry point.

// module2.js
import { foo } from 'c/module1';

// module1.js
export { foo } from './module1-extra';

// module1-extra.js
export const foo = 'bar';
2 of 2
1

We came across this question as well and solved it a couple of months ago with pmdartus' answer. Meanwhile, we were stuck in debugging because our component started behaving oddly. I will not go into detail here, but the outcome was, that in the background, the export { foo } solution seems to break the export default class used by the LWC compiler.

So if you export anything inside your component except the LightningElement implementation, you will run into an error. In production, this error is hidden and only certain things go wrong, so probably you won't even notice. (You might for example experience broken "target" references inside your event handlers.)

But in a local dev environment, the component never loads and throws this exception in the console:

Uncaught (in promise) Error: Invalid LWC Constructor [object Object] for custom element .

Technically multiple exports next to the default should be absolutely valid, but somehow the LWC-Compiler cannot handle it. If anyone has a deeper understanding or is willing to dive into the lwc repos, please contribute your knowledge, I am curious.

Back to the question

The most accurate and recommended solution from salesforce is, create a UI-less LWC component for your shared file. You might argue, that your exported file belongs to this component and should be stored next to it, in this case maybe question yourself, why you would want to expose it then?

This is the official documentation to Share JavaScript Code, where et the end of the document exports also are passed through multiple files as suggested in the previous answer but be aware, that they are not passing things through a UI component, but just through another utils file.

๐ŸŒ
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() ...
๐ŸŒ
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).
Author ย  bobbyhadz
๐ŸŒ
DigitalOcean
digitalocean.com โ€บ community โ€บ tutorials โ€บ js-modules-es6
ES6 Modules and How to Use Import and Export in JavaScript | DigitalOcean
September 4, 2020 - default function myLogger() { console.log(myNumbers, pets); } export class Alligator { constructor() { // ... } } ... import keyword, members to be imported in curly brackets and then the location of the module relative to the current file:
๐ŸŒ
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 - But unable to do this using ES6 terms. 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; } }
๐ŸŒ
Packtpub
subscription.packtpub.com โ€บ book โ€บ web-development โ€บ 9781789800104 โ€บ 1 โ€บ ch01lvl1sec11 โ€บ classes-and-modules
Introducing ECMAScript 6 | Advanced JavaScript
In this section, we introduced JavaScript classes and ES6 modules. We discussed the prototype-based inheritance structure and demonstrated the basics of class creation and JavaScript class inheritance. When discussing modules, we first showed how to create a module and export the functions and variables stored within them. Then, we showed you how to load a module and import the data contained within.