The export default {...} construction is just a shortcut for something like this:
const funcs = {
foo() { console.log('foo') },
bar() { console.log('bar') },
baz() { foo(); bar() }
}
export default funcs
It must become obvious now that there are no foo, bar or baz functions in the module's scope. But there is an object named funcs (though in reality it has no name) that contains these functions as its properties and which will become the module's default export.
So, to fix your code, re-write it without using the shortcut and refer to foo and bar as properties of funcs:
const funcs = {
foo() { console.log('foo') },
bar() { console.log('bar') },
baz() { funcs.foo(); funcs.bar() } // here is the fix
}
export default funcs
Another option is to use this keyword to refer to funcs object without having to declare it explicitly, as @pawel has pointed out.
Yet another option (and the one which I generally prefer) is to declare these functions in the module scope. This allows to refer to them directly:
function foo() { console.log('foo') }
function bar() { console.log('bar') }
function baz() { foo(); bar() }
export default {foo, bar, baz}
And if you want the convenience of default export and ability to import items individually, you can also export all functions individually:
// util.js
export function foo() { console.log('foo') }
export function bar() { console.log('bar') }
export function baz() { foo(); bar() }
export default {foo, bar, baz}
// a.js, using default export
import util from './util'
util.foo()
// b.js, using named exports
import {bar} from './util'
bar()
Or, as @loganfsmyth suggested, you can do without default export and just use import * as util from './util' to get all named exports in one object.
The export default {...} construction is just a shortcut for something like this:
const funcs = {
foo() { console.log('foo') },
bar() { console.log('bar') },
baz() { foo(); bar() }
}
export default funcs
It must become obvious now that there are no foo, bar or baz functions in the module's scope. But there is an object named funcs (though in reality it has no name) that contains these functions as its properties and which will become the module's default export.
So, to fix your code, re-write it without using the shortcut and refer to foo and bar as properties of funcs:
const funcs = {
foo() { console.log('foo') },
bar() { console.log('bar') },
baz() { funcs.foo(); funcs.bar() } // here is the fix
}
export default funcs
Another option is to use this keyword to refer to funcs object without having to declare it explicitly, as @pawel has pointed out.
Yet another option (and the one which I generally prefer) is to declare these functions in the module scope. This allows to refer to them directly:
function foo() { console.log('foo') }
function bar() { console.log('bar') }
function baz() { foo(); bar() }
export default {foo, bar, baz}
And if you want the convenience of default export and ability to import items individually, you can also export all functions individually:
// util.js
export function foo() { console.log('foo') }
export function bar() { console.log('bar') }
export function baz() { foo(); bar() }
export default {foo, bar, baz}
// a.js, using default export
import util from './util'
util.foo()
// b.js, using named exports
import {bar} from './util'
bar()
Or, as @loganfsmyth suggested, you can do without default export and just use import * as util from './util' to get all named exports in one object.
One alternative is to change up your module. Generally if you are exporting an object with a bunch of functions on it, it's easier to export a bunch of named functions, e.g.
export function foo() { console.log('foo') },
export function bar() { console.log('bar') },
export function baz() { foo(); bar() }
In this case you are export all of the functions with names, so you could do
import * as fns from './foo';
to get an object with properties for each function instead of the import you'd use for your first example:
import fns from './foo';
Exporting multiple functions in a module
Export Multiple Functions of a Pytorch Module
Jest: How to export multiple functions into .spec.js test file?
Named exports of functions vs default export object with functions inside?
What are the different ways to export multiple functions in React?
What are some best practices for exporting multiple functions in React?
Why is it beneficial to export multiple functions from a single file in React?
Videos
Hello I was just wondering if any of you guys could help me with this problem Im having. I have a module.js which has 2 functions MyModule and MyModules. I would like to output both of these functions on a console but if keeps giving me an error:
let foo = moduleTest.testModule(); ^
TypeError: moduleTest.testModule is not a function
I am able to get one function to output to the console but not both.
Module.js:
function myModule(){ let foo = []; foo.testModule = function(){ foo.push('one'); foo.push('two'); } return foo; } module.exports = myModule;
function myModules(){
let foos = [];
foos.testModules = function(){
foos.push('one');
foos.push('two');
}
return foos;
}module.exports = myModules;
module-import.js:( here is where I try and output both functions)
let MyModule = require('./module');
let moduleTest = new MyModule();
let foo = moduleTest.testModule();
console.log('foo is ' + moduleTest);
//importing the second method
var MyModules = MyModuleos.MyModules;
let moduleTests = new MyModules();
let foos = moduleTests.testModules();
console.log('foo is ' + moduleTests);
For some reason it is not working for me. Any help that you guys could offer is greatly appreciated.
The production code is in a file called functionOne.js. I named the first function in it functionOne as well:
function functionOne(){
// Stuff }
function functionTwo(){
// Stuff
}
module.exports = functionOne, functionTwo; And the first line of the test file is
const functionOne = require('filepath'); In the test file I use both functions and it says functionTwo is undefined. What is the correct syntax to get both functions into the test file?