无法导入ES2015模块 [英] Cannot import ES2015 module
本文介绍了无法导入ES2015模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
import React从'反应'
从react-smallgrid导入SmallGrid;
导出默认类库扩展React.Component {
我收到以下错误:
$ grunt browserify
运行browserify:jsx(browserify)任务
>> /Users/me/code/myapp/node_modules/react-smallgrid/src/smallgrid.jsx:1
>>进口反应来自反应;
>> ^
>> ParseError:'import'和'export'只能显示'sourceType:module'
警告:运行grunt-browserify时出错。使用--force继续。
import React from 反应;
import _ from'lodash';
导出默认类SmallGrid extends React.Component {
有没有人知道这些可怕的JavaScript错误是什么意思?
解决方案
目前不可能从编写的node_modules中导入代码ES2015。您必须将库中的代码编译为ES5。例如使用babel(通过npm脚本):
脚本:{
compile:babel - -presets es2015,stage-0 -d lib / src /
}
此脚本期望将您的ES2015代码放在 src /
目录中,并将编译的代码输出到 lib /
目录
I'm trying to develop an npm library, but when I use it in another app, e.g.:
import React from 'react';
import SmallGrid from 'react-smallgrid';
export default class Library extends React.Component{
I get the following error:
$ grunt browserify
Running "browserify:jsx" (browserify) task
>> /Users/me/code/myapp/node_modules/react-smallgrid/src/smallgrid.jsx:1
>> import React from 'react';
>> ^
>> ParseError: 'import' and 'export' may appear only with 'sourceType: module'
Warning: Error running grunt-browserify. Use --force to continue.
The library starts with:
import React from 'react';
import _ from 'lodash';
export default class SmallGrid extends React.Component{
Is there anybody here that knows what these horrible javascript errors mean?
解决方案
Currently it is not possible to import code from node_modules written in ES2015. You have to compile code in your library to ES5. For example using babel (via npm script):
"scripts": {
"compile": "babel --presets es2015,stage-0 -d lib/ src/"
}
This script expects your ES2015 code to be placed in src/
directory and outputs compiled code into lib/
directory
这篇关于无法导入ES2015模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文