We have use cases for both modules / node.js usages as well as standalone/vanilla javascript. The export
statement can not be used in some cases in the latter.
export class MyClass
Is there any syntax to be able to achieve the export
needed for modules
yet still be compatible with running the javascript file ad-hoc - eg from an IDE (in my case PyCharm
? If I try to run the javascript file directly we see:
SyntaxError: Unexpected token 'export'
Not in JavaScript itself, no. A compilation unit either is or isn't a module, it can't be context-sensitive.¹
Bundlers like Rollup.js and Webpack have ways of doing it (by not using JavaScript's built-in module system), but there's no native way.
¹ Except that if it doesn't use import
or export
, it can be either a script or a module that doesn't export anything and doesn't statically import anything. (Scripts can use dynamic import.)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.