Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to run koa with nodejs v6.11.2 #1037

Closed
vaxilicaihouxian opened this issue Aug 7, 2017 · 12 comments
Closed

How to run koa with nodejs v6.11.2 #1037

vaxilicaihouxian opened this issue Aug 7, 2017 · 12 comments

Comments

@vaxilicaihouxian
Copy link

Nodejs version 6.11.2 is LTS.So I need to use this nodejs version to run koa2.I followed tutorial and code is:

require('babel-register');
const Koa = require('koa');
const app = new Koa();

app.use(async ctx => {
    ctx.body = 'Hello World';
});

app.listen(3000);

Package.json:

{
  "name": "koa",
  "version": "1.0.0",
  "description": "",
  "main": "app.js",
  "dependencies": {
    "babel-cli": "^6.24.1",
    "babel-plugin-transform-async-to-module-method": "6.24.1",
    "babel-register": "6.24.1",
    "koa": "2.3.0"
  },
  "devDependencies": {},
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC"
}

But it didn't say how to run app.js?

I try node app.js and it did not work.
Any helps?

@fl0w
Copy link
Contributor

fl0w commented Aug 7, 2017

You need to require the file for babel hook to work.

@vaxilicaihouxian
Copy link
Author

How to require the file for babel hook to work ?
I have already require the file babel-register.
I'm newer,sorry about that.

@fl0w
Copy link
Contributor

fl0w commented Aug 7, 2017

Take a look at this example. I haven't used babel in a while myself.

Any specific reason to why you're not using node 8? It is recommended.

@vaxilicaihouxian
Copy link
Author

I think that LTS version may be a good choice for me because I'm new in nodejs.I wonder why the official tutorial didn't show how to work with node 6.

@fl0w
Copy link
Contributor

fl0w commented Aug 7, 2017

@vaxilicaihouxian It does, under "Installation" > "Async Functions with Babel".
node 8 will be under LTS in October, I would personally recommend you to start using node 8 instead. This way you can omit babel (which can be a bit cumbersome for beginners) and focus on creating awesome stuff instead!

@vaxilicaihouxian
Copy link
Author

This is my code (follow the "Async Functions with Babel").

//index.js
require("babel-core/register");
const app = require('./app.js');

//app.js
const Koa = require('koa');
const app = new Koa();
app.use(async ctx => {
  ctx.body = 'Hello World';
});

app.listen(3000);

I run node index.js and it shows:

ubuntu@ubuntu-xenial:/codes/koa$ node index.js
/codes/koa/node_modules/babel-core/lib/transformation/file/index.js:590
      throw err;
      ^

TypeError: /codes/koa/app.js: Property value expected type of string but got null
    at Object.validate (/codes/koa/node_modules/babel-types/lib/definitions/index.js:161:13)
    at validate (/codes/koa/node_modules/babel-types/lib/index.js:505:9)
    at Object.builder (/codes/koa/node_modules/babel-types/lib/index.js:466:7)
    at File.addImport (/codes/koa/node_modules/babel-core/lib/transformation/file/index.js:353:54)
    at PluginPass.addImport (/codes/koa/node_modules/babel-core/lib/transformation/plugin-pass.js:52:43)
    at PluginPass.Function (/codes/koa/node_modules/babel-plugin-transform-async-to-module-method/lib/index.js:14:28)
    at newFn (/codes/koa/node_modules/babel-traverse/lib/visitors.js:276:21)
    at NodePath._call (/codes/koa/node_modules/babel-traverse/lib/path/context.js:76:18)
    at NodePath.call (/codes/koa/node_modules/babel-traverse/lib/path/context.js:48:17)
    at NodePath.visit (/codes/koa/node_modules/babel-traverse/lib/path/context.js:105:12)

If I can not fix that I will change to use node version 8.

@fl0w
Copy link
Contributor

fl0w commented Aug 7, 2017

This works for me.

index.js

require('babel-core/register')
require('babel-polyfill')
require('./app.js')

app.js

'use strict'

const Koa = require('koa')
const app = new Koa()

app.use(async ctx => {
  ctx.body = 'Hello World'
})

app.listen(3000)

.babelrc

{
  "presets": [
    "node6"
  ],
  "plugins": [
    ["transform-async-to-module-method", {
        "module": "bluebird",
        "method": "coroutine"
    }]
  ]
}

package.json

{
  ...
  "dependencies": {
    "babel-plugin-transform-async-to-module-method": "^6.24.1",
    "babel-polyfill": "^6.23.0",
    "babel-preset-node6": "^11.0.0",
    "babel-register": "^6.24.1",
    "bluebird": "^3.5.0"
  }
}

@albertogasparin
Copy link

The problem is that you have to include babel in a different file before stating using ES2015+ code.
So you have to create a "loader" file and require your code from it:

// loader.js
require("babel-core/register");
// No ES2015+ code here!
require("./index.js");

However, that might not be enough as babel does not parse node_modules by default. So you might need to add the ignore option in order to successfully load Koa (and that will make babel compilation much slower). So, I second what @fl0w already said: Node 8 is stable and will be LTS in a couple of months. By using it right now you can avoid over-complicating your project and moving to a different node version half-way 😉.

@vaxilicaihouxian
Copy link
Author

OK.I will try it( don't know how the official setting work,but whatever).
And I think I should use node 8 right now.
Thanks!!
Maybe the we don't need babel in the future.

@slaskis
Copy link
Contributor

slaskis commented Aug 7, 2017

I really liked to use async-to-gen and skip babel entirely (there's an example in a comment in another issue)

@fl0w
Copy link
Contributor

fl0w commented Aug 7, 2017

Or use node 8 and skip babel entirely :)

@jonyrock
Copy link

You can compile project with webpack to server.js and then compile it again with preset with target to node 6.11.2

Sorry, I don't have time to make a simple clear solution, but you can try do digg code: https://github.com/hastic/hastic-server/blob/9000adefcc79e48e45089f9d25960f7805a37e48/server/package.json

Also I can't say that it is the BEST solution, but it works.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants