standard-things / esm

Tomorrow's ECMAScript modules today!
Other
5.26k stars 146 forks source link

support class-properties? #749

Closed zzswang closed 5 years ago

zzswang commented 5 years ago

esm: 3.2.17

yarn start: nodemon -r esm src/index.js --watch src

it will run into syntax error with class properties.

the error:

╰─ yarn start
yarn run v1.9.4
$ node -r esm src/index.js --watch src | pino-pretty -c -t
file:///Users/zzs/Workspace/src/github.com/36node/sketch/packages/tpl-service/src/models/pet.js:24
  name;
      ^

SyntaxError: Unexpected token ;
    at new Script (vm.js:79:7)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
✨  Done in 0.29s.

file with calss properties

class Pet {
  /** @type {string} */
  name;
  /** @type {string} */
  tag;
  /** @type {string} */
  owner;
  /** @type {("CAT"|"DOG")} */
  category;
}
dnalborczyk commented 5 years ago

@zzswang this is not an esm issue. class properties are not yet fully supported by the current V8 version of node.js. they are very likely supported with the next major version of node.js v12.

in the meantime you have several options: you can use a transpiler, like Babel, or you can experiment starting node with harmony flags, e.g.

node --harmony-class-fields -r esm index.js

here is an overview of some flags: https://flaviocopes.com/node-runtime-v8-options/ use them at your own risk, as they are meant to be used to experiment, not recommended for production.

if you are interested, here is a list of supported language features in node.js by version: https://node.green/

zzswang commented 5 years ago

@dnalborczyk

Thanks