Keystone+React: 'import' and 'export' may appear only with 'sourceType: module'
Asked Answered
S

1

0

After updating the project dependencies I started facing the error. Browserify has problem parsing my JS code, which is written in ES6. Browserify seems to expect ES5.

I don't understand, why the sourceType: module seems to be the issue here.

Source:

var babelify = require('babelify');
var browserify = require('browserify-middleware');
var keystone = require('keystone');   
var importRoutes = keystone.importer(__dirname);

// Setup Route Bindings
exports = module.exports = function(app) {

    app.use('/js', browserify('./client/scripts', {
        transform: [babelify.configure({
            extensions: ['es6'],
            plugins: ['object-assign']
        })]
    }));

};

The same happens, when omitting babelify from the code above:

var browserify = require('browserify-middleware');
var keystone = require('keystone');   
var importRoutes = keystone.importer(__dirname);

// Setup Route Bindings
exports = module.exports = function(app) {

    app.use('/js', browserify('./client/scripts'));

};

Server console:

GET /js/Application.js 500 61.176 ms
Error thrown for request: /js/Application.js
Error: Parsing file /home/user/project/client/scripts/Application.js: 'import' and 'export' may appear only with 'sourceType: module' (1:0)
    at Deps.parseDeps (/home/user/project/node_modules/module-deps/index.js:454:28)
    at fromSource (/home/user/project/node_modules/module-deps/index.js:391:44)
    at /home/user/project/node_modules/module-deps/index.js:385:17
    at ConcatStream.<anonymous> (/home/user/project/node_modules/concat-stream/index.js:36:43)
    at emitNone (events.js:91:20)
    at ConcatStream.emit (events.js:185:7)
    at finishMaybe (/home/user/project/node_modules/concat-stream/node_modules/readable-stream/lib/_stream_writable.js:475:14)
    at endWritable (/home/user/project/node_modules/concat-stream/node_modules/readable-stream/lib/_stream_writable.js:485:3)
    at ConcatStream.Writable.end (/home/user/project/node_modules/concat-stream/node_modules/readable-stream/lib/_stream_writable.js:455:41)
    at DuplexWrapper.onend (/home/user/project/node_modules/readable-stream/lib/_stream_readable.js:504:10)
    at DuplexWrapper.g (events.js:291:16)
    at emitNone (events.js:91:20)
    at DuplexWrapper.emit (events.js:185:7)
    at endReadableNT (/home/user/project/node_modules/readable-stream/lib/_stream_readable.js:926:12)
    at _combinedTickCallback (internal/process/next_tick.js:74:11)
    at process._tickCallback (internal/process/next_tick.js:98:9)
Scrabble answered 3/1, 2017 at 14:21 Comment(0)
S
0

The problem was just with incorrect value in extensions: ['es6'], what was needed was to put the actual extension of the JS files extensions: ['.js'].

The working code:

var babelify = require('babelify');
var browserify = require('browserify-middleware');
var keystone = require('keystone');   
var importRoutes = keystone.importer(__dirname);

// Setup Route Bindings
exports = module.exports = function(app) {

    app.use('/js', browserify('./client/scripts', {
        transform: [babelify.configure({
            extensions: ['.js'],
            plugins: ['object-assign']
        })]
    }));

};
Scrabble answered 3/1, 2017 at 14:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.