Search code examples
javascriptnode.jsecmascript-6babeljsnw.js

NW.js + Babel: ES6 import working but not export?


I'm building a NW.js app, currently with babel-standalone and React. I can use ES6 import, but ES6 export on the other hand does not work, console spits out unexpected token export. What's going on?

index.html:

<html>
<head>
    <meta charset="utf-8">
    <script src="assets/react.min.js" charset="utf-8"></script>
    <script src="assets/react-dom.min.js" charset="utf-8"></script>
    <script src="assets/babel.min.js" charset="utf-8"></script>
</head>
<body>
    <script type="text/babel" src="script/App.js"></script>
</body>
</html>

(yes, Babel indeed works, since React stuff inside runs OK)

In app.js:

import Lib from "./script/lib.js";

(and it's indeed exporting lib.js correctly, since that is the file responsible for the error)

In script/lib.js:

export default class {...};

I'm aware I can use Node modules instead, or even HTML script loading, but that's beside the point. I want to know why export doesn't work even if Babel doesn't seem to be broken, and even import works fine.


Solution

  • The problem is that Babel doesn't see files that were loaded via require, and they are loaded as they are, without transpilation.

    There can be several ways to work this around, but the easiest one will be using Babel at build step.

    Process your source code and then load processed code nw.js environment. The example how to do that you can find at this boilerplate project