javascript - Using ES6 features in Expressjs, Node - Stack Overflow

So i've been trying to use ES6 features in Express. I read that Nodejs now has natively supports e

So i've been trying to use ES6 features in Express. I read that Nodejs now has natively supports es6 so I don't need babel to do anything for me.

Within my app.js file I have:

'use strict';

const express = require('express');
const path = require('path');
const favicon = require('serve-favicon');
const logger = require('morgan');
const cookieParser = require('cookie-parser');
const bodyParser = require('body-parser');
const mongodb = require('mongodb');
const mongoose = require('mongoose');
const shell = require('shelljs');
const fs = require('fs'), gm = require('gm');
const routes = require('./routes/index');
const users = require('./routes/users');
const app = express();
// view engine setup
// app.use(express.static(__dirname + '/bundle'));
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'hbs');
// unment after placing your favicon in /public
// app.use(favicon(__dirname + '/public/favicon.ico'));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use('/', routes);
app.use('/users', users);
// catch 404 and forward to error handler
app.use((req, res, next) => {
    const err = new Error('Not Found');
    err.status = 404;
    next(err);
});
// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
    app.use((err, req, res, next) => {
        res.status(err.status || 500);
        res.render('error', {
            message: err.message,
            error: err
        });
    });
}
// production error handler
// no stacktraces leaked to user
app.use((err, req, res, next) => {
    res.status(err.status || 500);
    res.render('error', {
        message: err.message,
        error: {}
    });
});
module.exports = app;

I then have my server.js file which just basically runs http server and points to my app.js no errors within my server.js file. However, I am using es5 in there.

Path/To/Project/app.js:3
const express = require('express');
^^^^^
SyntaxError: Use of const in strict mode.
    at exports.runInThisContext (vm.js:73:16)
    at Module._pile (module.js:443:25)
    at Object.Module._extensions..js (module.js:478:10)
    at Module.load (module.js:355:32)
    at Function.Module._load (module.js:310:12)
    at Module.require (module.js:365:17)
    at require (module.js:384:17)
    at Object.<anonymous> (Path/To/Project/server.js:7:11)
    at Module._pile (module.js:460:26)
    at Object.Module._extensions..js (module.js:478:10)
NAME-MacBook-Pro% 

I removed the 'use strict'; from the top of the file as it plained but just got another error with unexpected token.

Do I need to use babel to pile it down or something? Why's it plaining?

Thanks!

So i've been trying to use ES6 features in Express. I read that Nodejs now has natively supports es6 so I don't need babel to do anything for me.

Within my app.js file I have:

'use strict';

const express = require('express');
const path = require('path');
const favicon = require('serve-favicon');
const logger = require('morgan');
const cookieParser = require('cookie-parser');
const bodyParser = require('body-parser');
const mongodb = require('mongodb');
const mongoose = require('mongoose');
const shell = require('shelljs');
const fs = require('fs'), gm = require('gm');
const routes = require('./routes/index');
const users = require('./routes/users');
const app = express();
// view engine setup
// app.use(express.static(__dirname + '/bundle'));
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'hbs');
// unment after placing your favicon in /public
// app.use(favicon(__dirname + '/public/favicon.ico'));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use('/', routes);
app.use('/users', users);
// catch 404 and forward to error handler
app.use((req, res, next) => {
    const err = new Error('Not Found');
    err.status = 404;
    next(err);
});
// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
    app.use((err, req, res, next) => {
        res.status(err.status || 500);
        res.render('error', {
            message: err.message,
            error: err
        });
    });
}
// production error handler
// no stacktraces leaked to user
app.use((err, req, res, next) => {
    res.status(err.status || 500);
    res.render('error', {
        message: err.message,
        error: {}
    });
});
module.exports = app;

I then have my server.js file which just basically runs http server and points to my app.js no errors within my server.js file. However, I am using es5 in there.

Path/To/Project/app.js:3
const express = require('express');
^^^^^
SyntaxError: Use of const in strict mode.
    at exports.runInThisContext (vm.js:73:16)
    at Module._pile (module.js:443:25)
    at Object.Module._extensions..js (module.js:478:10)
    at Module.load (module.js:355:32)
    at Function.Module._load (module.js:310:12)
    at Module.require (module.js:365:17)
    at require (module.js:384:17)
    at Object.<anonymous> (Path/To/Project/server.js:7:11)
    at Module._pile (module.js:460:26)
    at Object.Module._extensions..js (module.js:478:10)
NAME-MacBook-Pro% 

I removed the 'use strict'; from the top of the file as it plained but just got another error with unexpected token.

Do I need to use babel to pile it down or something? Why's it plaining?

Thanks!

Share Improve this question asked Nov 29, 2015 at 12:21 pourmesomecodepourmesomecode 4,37812 gold badges53 silver badges91 bronze badges 3
  • 2 Which version of nodejs you are using? – FlorianTopf Commented Nov 29, 2015 at 12:27
  • 2 That was embarrassing. I was using V0.12.0, upgraded to V5.0.0 now. Works fine! – pourmesomecode Commented Nov 29, 2015 at 12:33
  • I was expecting that, since I walked into the same trap. :-) Happy coding. – FlorianTopf Commented Nov 29, 2015 at 12:35
Add a ment  | 

3 Answers 3

Reset to default 3

I think you are using an old version of node. You have to have version 4+ to be able to use ES6 features like const.

If you need use to all features of es6, its better to pile using babel first. As even Node v5.0.0 implements only 59% of ES6 features as of today.

Check this link for ES6 patibility table.

const will still continue plaining as in even in ES6 const has been shipped as block scope. And your are using it here outside block.

Please go through this for more details

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745566234a4633408.html

相关推荐

  • javascript - Using ES6 features in Expressjs, Node - Stack Overflow

    So i've been trying to use ES6 features in Express. I read that Nodejs now has natively supports e

    6小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信