admin 管理员组

文章数量: 1086019

I am writing ES6 in NodeJS env via Babel. So here is my .babelrc file :

{
  "presets": ["es2015", "stage-2"],
  "plugins": []
}

Yep, pretty simple. I'm using npm scripts to launch mands :

"build-server": "babel server/lib -d server/dist",
    "build-server:w": "babel server/lib -w -d server/dist",

And it works great. Under server/, I have a lib folder which contains my source code and a dist folder with 'babel-code'.

So typically, I can write this :

import { ModuleAPI } from './api/moduleAPI';
import { path } from 'path';
import { fs } from 'fs';
import { express } from 'express';
let app = express();

which is successfully transpiled to :

'use strict';

var _moduleAPI = require('./api/moduleAPI');

var _path = require('path');

var _fs = require('fs');

var _express = require('express');

var app = (0, _express.express)();

The issue is, when I execut node server/dist/server.js, an error is throwed :

var app = (0, _express.express)();
                               ^
TypeError: (0 , _express.express) is not a function

I've seen some 'similar' issues : webpack babel es7 async function error "TypeError: (0 , _typeof3.default) is not a function"

But I can't get my code works with Babel.

Any idea ?

I am writing ES6 in NodeJS env via Babel. So here is my .babelrc file :

{
  "presets": ["es2015", "stage-2"],
  "plugins": []
}

Yep, pretty simple. I'm using npm scripts to launch mands :

"build-server": "babel server/lib -d server/dist",
    "build-server:w": "babel server/lib -w -d server/dist",

And it works great. Under server/, I have a lib folder which contains my source code and a dist folder with 'babel-code'.

So typically, I can write this :

import { ModuleAPI } from './api/moduleAPI';
import { path } from 'path';
import { fs } from 'fs';
import { express } from 'express';
let app = express();

which is successfully transpiled to :

'use strict';

var _moduleAPI = require('./api/moduleAPI');

var _path = require('path');

var _fs = require('fs');

var _express = require('express');

var app = (0, _express.express)();

The issue is, when I execut node server/dist/server.js, an error is throwed :

var app = (0, _express.express)();
                               ^
TypeError: (0 , _express.express) is not a function

I've seen some 'similar' issues : https://stackoverflow./questions/35187535/using-babel-jest-and-get-typeerror-0-createclass3-default-is-not-a-functio webpack babel es7 async function error "TypeError: (0 , _typeof3.default) is not a function"

But I can't get my code works with Babel.

Any idea ?

Share Improve this question edited May 23, 2017 at 12:23 CommunityBot 11 silver badge asked Mar 23, 2016 at 9:50 Clément FlodropsClément Flodrops 1,1141 gold badge14 silver badges29 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 10

You need to remove the braces around express.

import express from 'express';

express doesn't export an express property.

本文标签:

Error[2]: Invalid argument supplied for foreach(), File: /www/wwwroot/roclinux.cn/tmp/view_template_quzhiwa_htm_read.htm, Line: 58
File: /www/wwwroot/roclinux.cn/tmp/route_read.php, Line: 205, include(/www/wwwroot/roclinux.cn/tmp/view_template_quzhiwa_htm_read.htm)
File: /www/wwwroot/roclinux.cn/tmp/index.inc.php, Line: 129, include(/www/wwwroot/roclinux.cn/tmp/route_read.php)
File: /www/wwwroot/roclinux.cn/index.php, Line: 29, include(/www/wwwroot/roclinux.cn/tmp/index.inc.php)