Next.js: Question: How write ES6 code in server.js?

Created on 21 Mar 2017  路  4Comments  路  Source: vercel/next.js

I know of a few methods for this (babel-register, babel-cli), but what's your recommended solution that's most compatible with Next in both dev and prod?

Most helpful comment

Node v6 and v7 let you write ES2015, except for import and export. I think the recommendation is to use require and module.exports for that and use ES2015 without Babel.

All 4 comments

Node v6 and v7 let you write ES2015, except for import and export. I think the recommendation is to use require and module.exports for that and use ES2015 without Babel.

K thank you.

I made it work as follow:

instead of using server.js directly you could require it in index.js file. Then update script at package.json

index.js

require("babel-register")();
require("./server.js");

package.json

"start": "npm run sass && nodemon -w index.js index.js",

I am getting this error when I try that.

/Users/Vaughan/dev-live/vwp/index.js:1
(function (exports, require, module, __filename, __dirname) { import _regeneratorRuntime from 'next/node_modules/babel-runtime/regenerator';
                                                              ^^^^^^

SyntaxError: Unexpected token import
    at createScript (vm.js:74:10)
    at Object.runInThisContext (vm.js:116:10)
    at Module._compile (module.js:533:28)
    at loader (/Users/Vaughan/dev-live/vwp/node_modules/babel-register/lib/node.js:144:5)
    at Object.require.extensions.(anonymous function) [as .js] (/Users/Vaughan/dev-live/vwp/node_modules/babel-register/lib/node.js:154:7)
    at Module.load (module.js:503:32)
    at tryModuleLoad (module.js:466:12)
    at Function.Module._load (module.js:458:3)
    at Module.require (module.js:513:17)
    at require (internal/module.js:11:18)

FIX: I needed to add the es2015 preset because I am using imports.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

sospedra picture sospedra  路  3Comments

pie6k picture pie6k  路  3Comments

irrigator picture irrigator  路  3Comments

swrdfish picture swrdfish  路  3Comments

wagerfield picture wagerfield  路  3Comments