1

Difference between export {sayHi} from ‘./say.js’ and export {default as sayHi} from ‘./say.js’?

Doesn't the first statement rename default to sayHi just like the second one?

Also are these statements valid? If not why?

  • export foo as default from ‘bar.js’
  • export foo as newFooName from ‘bar.js’
9
  • Do you have access to a editor and a way to run javascript? It should allow you to answer at least some of these. Commented Jul 11, 2020 at 0:29
  • @Evert I tried running both versions for the first question I posed above and I don't think theyre equivalent because my app broke, but I don't know what export {sayHi} from './say.js does. The syntax seems a little misleading to me and I have been struggling to find answers Commented Jul 11, 2020 at 0:38
  • 1
    export {sayHi} from ... is similar to import {sayHi}. The file that you're importing from must have a named export (not default) with the explicit sayHi name like export function sayHi(). The export {sayHi} from ... imports sayHi by name and re-exports it with the same name. So no, they are not equivalent. Commented Jul 11, 2020 at 0:41
  • @Evert this confuses me because I thought, export { } from ... with braces means that we are importing something that was a default export imgur.com/a/F2dRPVm Commented Jul 11, 2020 at 0:51
  • 1
    No it's explicitly not that. With braces you need the default keyword to get the default thing. Commented Jul 11, 2020 at 0:54

1 Answer 1

1

I hope this helps. There are many ways to export/import in JS, maybe this answer is too teorical but this is my reference to understand how modules works in JS.

//MODULES

module.exports  //exports the module for use in another program.
require()       //imports the module for use in the current program.


//exports / require
//exports   (in menu.js file).
let Menu = {};      //Create an object to represent the module.
Menu.specialty = "Roasted Beet Burger with Mint Sauce";     //Add properties or methods to the module object.
module.exports = Menu;  //Export the module with module.exports.
//require   (in order.js file).
const Menu = require('./menu.js');  //Import the module with require() and assign it to a local variable (the .js extension is optional).
console.log('My order is: ' + Menu.specialty);  // Use the module and its properties within a program.


//export default
let Menu = {};
export default Menu;   //Uses the JavaScript export statement to export JavaScript objects, functions, and primitive data types.
//import
import Menu from './menu';


//Named exports
let burger = 'test';
export { burger };  //Named exports allow us to export data through the use of variables.
//Named imports
import { burger } from './menu';


//Export named exports
export let fries = "fries"; //They can be exported as soon as they are declared
//Import named imports
import { fries } from 'menu';


//Export assign                 
let specialty = "specialty";
export { specialty as chefsSpecial };   // The 'as' keyword allows us to give a variable name.
//Import as
import { chefsSpecial as specialForYou } from 'Menu';


//Another way of using aliases is to import the entire module as an alias:
import * as Carte from './menu';
Carte.chefsSpecial;
Carte.isVeg();
Carte.isLowSodium(); 

Sign up to request clarification or add additional context in comments.

1 Comment

dont think it quite answers my question, but appreciate it nonetheless +1

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.