2

Problem

I am trying to run simple http server using , but have problems running it.

It uses that is expected to work in future versions and I was wondering how can I run it with node v6.1.0 ?

Code

import Koa from 'koa';    
const app = new Koa();

// Setup handler.
app.use(async ctx => {
    ctx.body = "Hello World!";
});

// Start server.
app.listen(3000);

Output

$ node --version
v6.1.0

$ node --harmony index.js
C:\Users\gevor\WebstormProjects\untitled1\index.js:1
(function (exports, require, module, __filename, __dirname) { import Koa from 'koa';
                                                              ^^^^^^
SyntaxError: Unexpected token import
    at exports.runInThisContext (vm.js:53:16)
    at Module._compile (module.js:511:25)
    at Object.Module._extensions..js (module.js:550:10)
    at Module.load (module.js:456:32)
    at tryModuleLoad (module.js:415:12)
    at Function.Module._load (module.js:407:3)
    at Function.Module.runMain (module.js:575:10)
    at startup (node.js:160:18)
    at node.js:445:3

Question

I want to know how to run my app?

Similar Issues

gevorg
  • 4,835
  • 4
  • 35
  • 52
  • What problems do you have? Is this all the code you have? – Molda May 30 '16 at 16:42
  • I want know how to set environment to run basic sample with Koa v2 and node v6.1.0. – gevorg May 30 '16 at 16:47
  • Install node.js and run it `node index.js` assuming the code is in index.js. Have you tried? – Molda May 30 '16 at 16:49
  • You should add this errors to your question. Have you tried to run node with harmony flag `node --harmony index.js`? – Molda May 30 '16 at 17:07
  • @Molda thanks for suggestion, I just added execution log to question. – gevorg May 31 '16 at 08:35
  • 4
    I'm no expert at this but i think that node doesn't support *import, async, await* yet. I quess the only way is to use transpiler such as https://babeljs.io/ – Molda May 31 '16 at 10:04
  • @Molda I found workaround that I was searching for, that includes installing [Babel](http://babeljs.io), thanks Man! – gevorg Jun 02 '16 at 20:34

2 Answers2

5

Solution

I was able to find workaround and will describe solution that includes installing Babel module

Step 1 - Install Babel and required presets

$ npm install babel-core --save
$ npm install babel-preset-es2015-node5 --save
$ npm install babel-preset-stage-3 --save

Step 2 - Create index.js file with babel-core/register requirement

// set babel in entry file
require('babel-core/register')({
    presets: ['es2015-node5', 'stage-3']
});

require('./app');

Step 3 - Put your sample code inside of app.js

import Koa from 'koa';    
const app = new Koa();

// Setup handler.
app.use(async ctx => {
    ctx.body = "Hello World!";
});

// Start server.
app.listen(3000);

After running node index.js server works like a pie and import, async, await are being processed properly.

References

gevorg
  • 4,835
  • 4
  • 35
  • 52
  • 1
    For the async/await part, you can use a native implementation now that does not require to transpile your code. You need node >= 7, and run your app with --harmony-async-await flag. There is no native solution yet for the ES6 imports. – Bertofer Feb 17 '17 at 14:44
  • 2
    Just to mention, Node 7.6 comes with async/await by default, so no more need for the --harmony-async-await flag :) – Bertofer Feb 25 '17 at 09:28
2

Upgraded your node version to at least 7.6 after that there is no need to transpile your code using babel which is highly avoidable in production.Node >7.6 version support async/await which is very powerful.

BHUVNESH KUMAR
  • 391
  • 4
  • 15