getify / You-Dont-Know-JS

A book series on JavaScript. @YDKJS on twitter.
Other
179.32k stars 33.47k forks source link

Chapter 8: Bad syntax in "export getName;" in ESM section #1646

Closed mirao closed 4 years ago

mirao commented 4 years ago

Edition: 2nd

Book Title: You Don't Know JS Yet: Scope & Closures - 2nd Edition

Chapter: Chapter 8: The Module Pattern

Section Title: Modern ES Modules (ESM)

Problem: The statement export getName; doesn't work me with NodeJS 10.x and ESM module (node -r esm client.js), I'm getting "SyntaxError: Invalid or unexpected token". image

I think the right syntax is export { getName };, see also MDN docs. Or you can use an alternative suggested by IntelliSense in VScode:

const _getName = getName;
export { _getName as getName };
mirao commented 4 years ago

This is what I'm getting when using the syntax in your docs:

$ node -r esm client.js 
/home/mirao/workspace/javascript/ydkjs/modules/students.js:3
export getName;
       ^

SyntaxError: Invalid or unexpected token
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
getify commented 4 years ago

@saebrabaebra you have to unwatch a repository to stop getting notifications on new issues.

getify commented 4 years ago

@mirao -- sorry, that is a mistake, should have been export { getName };. will fix.