sylvainpolletvillard / ObjectModel

Strong Dynamically Typed Object Modeling for JavaScript
http://objectmodel.js.org
MIT License
467 stars 30 forks source link

Error: Unexpected token export #112

Closed nicolasdezena closed 5 years ago

nicolasdezena commented 5 years ago

Hi guys,

I have an error when i put const { Model } = require("objectmodel")

Error:


billing-shopback-api | export { Any, ArrayModel, BasicModel, FunctionModel, MapModel, Model, ObjectModel, SetModel };
billing-shopback-api | ^^^^^^
billing-shopback-api |
billing-shopback-api | SyntaxError: Unexpected token export
sylvainpolletvillard commented 5 years ago

Hello,

Since v4, ObjectModel is distributed as ES Module, and no longer CommonJS. See release notes here: https://github.com/sylvainpolletvillard/ObjectModel/releases/tag/v4.0.0

import { Model } from "objectmodel"
gotjoshua commented 5 years ago

I also get a similar error in the legacy build for meteor, even though i am using the correct syntax. There i think it is due to meteor's transpiling but I don't know how to get around it (so i am stuck on 3.7.7).

sylvainpolletvillard commented 5 years ago

yeah, unfortunately the transition from CommonJS to ES modules is not super smooth, mainly because of Node.js modules plan and bundler/transpiler issues.

I like the idea of distributing ESM, because it lets the final user use its own build tools, decide which environments to target with @babel/preset-env, and gets great tree-shaking and code elimination thanks to static analysis of ES modules. It's more initial work for the library consumer, but at the end he gets a custom build that is optimized for his specific needs. ESM also improves the developer experience with better autocompletion, for example, compared to a UMD bundle.

In the future, we can expect all the browsers/environments/tools to fully adopt ES modules by default and just work. Nethertheless, I hesitated when publishing v4 if this move was too early or not. Do you think I should still provide a UMD bundle for v4 ? Or let you do it by yourself ?

rpstreef commented 5 years ago

Trying to set this up with Node10 and babel proved to be an exercise in frustration.

So yes a commonjs variant would be appreciated.

sylvainpolletvillard commented 5 years ago

In the meantime, I found a way to fix this without having to provide a UMD bundle myself. The pika.dev service is providing UMD versions of my library for free. You can take it from their CDN here: https://umd.cdn.pika.dev/objectmodel/v4

Docs have been updated, and you can find a usage example here too: https://github.com/sylvainpolletvillard/ObjectModel/blob/master/test/umd.html

jnreynoso commented 4 years ago

Even with webpack, I get this error.

sylvainpolletvillard commented 4 years ago

please check my answers above