Keystone + React:' import'和'出口'可能只显示' sourceType:module'

时间:2017-01-03 14:21:54

标签: node.js ecmascript-6 browserify ecmascript-5 keystonejs

更新项目依赖项后,我开始面临错误。 Browserify在解析我的JS代码时遇到问题,这是用ES6编写的。 Browserify似乎期待ES5。

我不明白为什么sourceType: module似乎是这里的问题。

来源:

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']
        })]
    }));

};

当从上面的代码中省略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'));

};

服务器控制台:

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)

1 个答案:

答案 0 :(得分:0)

问题只是extensions: ['es6']中的值不正确,所需要的是放置JS文件extensions: ['.js']的实际扩展名。

工作代码:

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']
        })]
    }));

};