本文介绍了Keystone + React:“导入"和“导出"可能仅与"sourceType:模块"一起出现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新项目依赖项后,我开始遇到错误. Browserify在解析用ES6编写的JS代码时遇到问题. Browserify似乎期望使用ES5.

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.

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

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

来源:

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时,也会发生同样的情况:

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

};

服务器控制台:

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)

推荐答案

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

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'].

工作代码:

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

};

这篇关于Keystone + React:“导入"和“导出"可能仅与"sourceType:模块"一起出现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 13:13