Feathers: [Question] Export, import modules

Created on 20 Jul 2017  路  3Comments  路  Source: feathersjs/feathers

Currently, my project base on feather-cli and i need to use import, export module instead of require
But i got the error SyntaxError: Unexpected token export when i try to use import, export.
I saw some issue make feather turn back into commonjs.
I understand after https://github.com/feathersjs/feathers/issues/608 approve and merged, i can use import, export without error.
So my question is what can i do to use import & export instead of require ?

Most helpful comment

You could use babel-register to bootstrap your code as it runs.

// package.json
}
  "dependencies": {
  "babel-preset-es2015": "^6.24.1",
  "babel-register": "^6.24.1",
  ...
}
// .babelrc
{
  "presets": ["es2015"]
}



md5-d9dbd6153ad663c52db1c46098188247



```js
// server.js < your actual code here
import express from 'express';
export const run () => {
     const app = express()
     ......
}

Or you could simply transpile the code using babel in case you do not want to use babel-register at runtime.

All 3 comments

You could use babel-register to bootstrap your code as it runs.

// package.json
}
  "dependencies": {
  "babel-preset-es2015": "^6.24.1",
  "babel-register": "^6.24.1",
  ...
}
// .babelrc
{
  "presets": ["es2015"]
}



md5-d9dbd6153ad663c52db1c46098188247



```js
// server.js < your actual code here
import express from 'express';
export const run () => {
     const app = express()
     ......
}

Or you could simply transpile the code using babel in case you do not want to use babel-register at runtime.

The generated application will always only use what the latest version of Node supports (we'll probably update it to use async/await for the next version). You can find more of the reasoning behind not using Babel by default in https://github.com/feathersjs/generator-feathers/issues/31 and a discussion in https://github.com/feathersjs/feathers-cli/issues/91 (including the challenges of updating our generator codemods to the new syntax).

In summary, using ES modules with Babel will increase startup/compile time and possibly memory consumption just to use a spec whose implementation details (especially for Node) have not been fully finalized.

If you still want to use it, you can probably run the generated code through a require to ES module tranformer with the Babel setup that @zusamann pointed out.

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue with a link to this issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

intumwa picture intumwa  路  3Comments

harrytang picture harrytang  路  3Comments

corymsmith picture corymsmith  路  4Comments

rstegg picture rstegg  路  3Comments

stephane303 picture stephane303  路  3Comments