sylvainpolletvillard / ObjectModel

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

SyntaxError: Unexpected token 'export' #127

Closed giangchau92 closed 4 years ago

giangchau92 commented 4 years ago

I have pulled and make a node test project but it was not worked. What do I miss? Error: export { Any, ArrayModel, BasicModel, FunctionModel, MapModel, Model, ObjectModel, SetModel }; ^^^^^^

SyntaxError: Unexpected token 'export' at Module._compile (internal/modules/cjs/loader.js:892:18) at Object.Module._extensions..js (internal/modules/cjs/loader.js:973:10) at Module.load (internal/modules/cjs/loader.js:812:32) at Function.Module._load (internal/modules/cjs/loader.js:724:14) at Module.require (internal/modules/cjs/loader.js:849:19) at require (internal/modules/cjs/helpers.js:74:18)

**index.ts**
import { ObjectModel } from "objectmodel"

const Order = new ObjectModel({
    product: { name: String, quantity: Number },
    orderDate: Date
});

const myOrder = new Order({
    product: { name: "Apple Pie", quantity: 1 },
    orderDate: new Date()
});

***package.json***
{
  "name": "objectmodeling",
  "version": "1.0.0",
  "description": "",
  "main": "build/index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "@types/node": "^14.0.13",
    "typescript": "^3.9.5"
  },
  "dependencies": {
    "objectmodel": "^4.2.2"
  }
}
sylvainpolletvillard commented 4 years ago

related: https://github.com/sylvainpolletvillard/ObjectModel/issues/126

ObjectModel v4 is using ES Module exports, so you need a Node.js version that supports ES Modules, that is >= 13.2.0

https://medium.com/@nodejs/announcing-core-node-js-support-for-ecmascript-modules-c5d6dc29b663

The other option, if you do not want to update your Node.js version, is to retrieve a CommonJS or UMD version of the lib. You can either build it yourself or take an existing one, for example Pika CDN provides one here: https://cdn.pika.dev/objectmodel/%5E4.2.2

giangchau92 commented 4 years ago

Thanks for your reply. I could run my script now.