-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
Comments
You need to require the file for babel hook to work. |
How to |
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. |
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. |
@vaxilicaihouxian It does, under "Installation" > "Async Functions with Babel". |
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 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. |
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"
}
} |
The problem is that you have to include babel in a different file before stating using ES2015+ code. // 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 |
OK.I will try it( don't know how the official setting work,but whatever). |
I really liked to use async-to-gen and skip babel entirely (there's an example in a comment in another issue) |
Or use node 8 and skip babel entirely :) |
You can compile project with webpack to 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. |
Nodejs version 6.11.2 is LTS.So I need to use this nodejs version to run koa2.I followed tutorial and code is:
Package.json:
But it didn't say how to run app.js?
I try
node app.js
and it did not work.Any helps?
The text was updated successfully, but these errors were encountered: