No, there's no wildcard export (except when you're re-exporting everything from another module, but that's not what you're asking about).
Simply put export in front of each function declaration you want exported, e.g.
export function foo() {
// ...
}
export function bar() {
// ...
}
...or of course, if you're using function expressions:
export var foo = function() {
// ...
};
export let bar = () => {
// ...
};
export const baz = value => {
// ...
};
Answer from T.J. Crowder on Stack OverflowNo, there's no wildcard export (except when you're re-exporting everything from another module, but that's not what you're asking about).
Simply put export in front of each function declaration you want exported, e.g.
export function foo() {
// ...
}
export function bar() {
// ...
}
...or of course, if you're using function expressions:
export var foo = function() {
// ...
};
export let bar = () => {
// ...
};
export const baz = value => {
// ...
};
I think there are a lot of solutions to this. And as has been answered, there's no wildcard export. But, you can 'wildcard' the import. So, I much prefer the one putting export before each of the functions you want to expose from the file:
//myfile.js
export function fn1() {...}
export function fn2() {...}
and then import it like so:
import * as MyFn from './myfile.js'
Afterwards you could use it like so:
MyFn.fn1();
MyFn.fn2();
Import functions from another js file. Javascript - Stack Overflow
How to export both a React component and a function to another JavaScript file?
javascript - Export functions or variables to another module in node.js - Stack Overflow
javascript - Is it possible to access other module export functions within the same file? - Stack Overflow
Videos
The following works for me in Firefox and Chrome. In Firefox it even works from file:///
models/course.js
export function Course() {
this.id = '';
this.name = '';
};
models/student.js
import { Course } from './course.js';
export function Student() {
this.firstName = '';
this.lastName = '';
this.course = new Course();
};
index.html
<div id="myDiv">
</div>
<script type="module">
import { Student } from './models/student.js';
window.onload = function () {
var x = new Student();
x.course.id = 1;
document.getElementById('myDiv').innerHTML = x.course.id;
}
</script>
You can try as follows:
//------ js/functions.js ------
export function square(x) {
return x * x;
}
export function diag(x, y) {
return sqrt(square(x) + square(y));
}
//------ js/main.js ------
import { square, diag } from './functions.js';
console.log(square(11)); // 121
console.log(diag(4, 3)); // 5
You can also import completely:
//------ js/main.js ------
import * as lib from './functions.js';
console.log(lib.square(11)); // 121
console.log(lib.diag(4, 3)); // 5
Normally we use ./fileName.js for importing own js file/module and fileName.js is used for importing package/library module
When you will include the main.js file to your webpage you must set the type="module" attribute as follows:
<script type="module" src="js/main.js"></script>
For more details please check ES6 modules
Apologies if this seems like a basic question- I can't wrap my head around exports. have an App.js file that contains most of the functionalities of my project. There's also a custom function inside of App, called defineErrorMessage, that I also want to export along with App- my App.js code is here.
The reason why I want to export defineErrorMessage is that I want to import that function in another JavaScript file that has no components, blogs.js.
If I want to be able to both import and use defineErrorMessage instead of console logging the error message in the catch block, how can I export both App and defineErrorMessage together?
Just simply module.exports.functionOne().
If that's too cumbersome, just do the following:
function fnOne() {
console.log("One!");
}
module.exports.fnOne = fnOne;
var me = require(module.filename);
me.functionOne(name);
or just use exports object itself
module.exports.functionOne(name);
I have been working on a project where I have to import and export constants and function between different js files, but for some reason, I have been getting issues. Import/export has been working fine in my previous projects, so I don't know what changed
Here is my github repo: https://github.com/leey56522/battleShip
The errors seem to be saying different things, and I am not sure what exactly the problem is
-
If I Run Code on ship.js, I get "SyntaxError: Cannot use import statement outside a module"
-
When I use chrome inspect, it says "Access to script at 'file://battleShip/dist/main.js' from origin 'null' has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, chrome-untrusted, https. main.js:1 Failed to load resource: net::ERR_FAILED"
-
When another person got my file from git repo and tried it on his computer, it worked without an error
Here are some things I tried:
-
updating node.js to the most recent version
-
adding "type": "module" to package.json or script tag in index.html
-
trying out import/export function in a completely separate folder (still did not work with same issue. I also remember seeing "can't find module" error even though I am not sure what triggered it and the file address was correct)
Can someone please tell me what I am doing wrong? I have been asking for help on discord and looking up on stack overflow, but I could not figure out what is going on with my code