从node_modules导入一个模块时,Babelify抛出ParseError [英] Babelify throws ParseError on import a module from node_modules
问题描述
我正在使用 Babelify
和浏览。此外,我使用节点模块系统的ES6样式模块功能。
我想将所有我自己的模块放到 node_modules / libs
。
例如:
test.js
node_modules / libs
export default )=> {
console.log('Hello');
};
main.js
(将被编译为 bundle.js
)
'libs / test'导入测试;
test();
之后,我将上述代码编译成 bundle.js
使用此命令:
browserify -t babelify main.js -o bundle.js
但不幸的是,我有一些错误:
export default()=> {
^
ParseError:'import'和'export'可能只会显示'sourceType:module'
目录结构:
[test]
` - node_modules
│` - libs
│` - test.js
` - main.js
但是,当自己的模块不在 node_modules
中时,如下所示:
[test]
/ pre>
` - libs
│` - test.js
` - main.js
然后,它工作正常。如何在
node_modules
中使用babelify
的ES6样式模块?解决方案这就是Browserify转换的工作原理,转换只能在被引用的模块中直接有效。
如果您想要在node_modules中的模块进行转换,则需要向该模块添加
package.json
并添加babelify
作为该模块的转换。例如browserify:{
transform:[
babelify
]
},
在你的
package.json
plusbabelify
作为依赖将告诉browserify
运行babelify
转换为该模块中的任何文件。
在$ _ c $ c> libs 中是node_modules中的一个文件夹可能是个坏主意通常,该文件夹将具有真正的独立模块。我通常会说,如果文件夹不能在其他地方被使用和重用,那么它不应该在node_modules中。
更新
对于最近发布的Babel v6,您还需要指定要在代码上执行哪些转换。为此,我建议在您的根目录中创建一个
.babelrc
文件来配置Babel。{
预设:[es2015]
}
和
npm install --save-dev babel-preset-es2015
I'm working with
Babelify
and Browserify. Also, I'm using ES6 style module features by node module system.I would like to put all my own modules into
node_modules/libs
.For instance:
test.js
innode_modules/libs
export default () => { console.log('Hello'); };
main.js
(will be compiled tobundle.js
)import test from 'libs/test'; test();
After that, I have compiled the above codes to
bundle.js
with this command:browserify -t babelify main.js -o bundle.js
But unfortunately, I have got some error:
export default () => { ^ ParseError: 'import' and 'export' may appear only with 'sourceType: module'
Directory structure:
[test] `-- node_modules │ `-- libs │ `-- test.js `-- main.js
But, when own modules not in
node_modules
like this:[test] `-- libs │ `-- test.js `-- main.js
Then, it works fine. How can I use the ES6 style modules with
babelify
innode_modules
?解决方案That is how Browserify transforms work, transforms only have an effect directly in the module that is being referenced.
If you want a module in node_modules to have a transform, you'd need to add a
package.json
to that module and addbabelify
as a transform for that module too. e.g."browserify": { "transform": [ "babelify" ] },
inside your
package.json
plusbabelify
as a dependency will tellbrowserify
to run thebabelify
transform on any file inside that module.Having
libs
be a folder in node_modules is however probably a bad idea. Generally that folder would have true standalone modules in it. I'd generally say that if the folder can't be taken and reused elsewhere, then it shouldn't be in node_modules.Update
For Babel v6, which was recently released, you will also need to specify which transformations you would like to perform on your code. For that, I would recommend creating a
.babelrc
file in your root directory to configure Babel.{ "presets": ["es2015"] }
and
npm install --save-dev babel-preset-es2015
这篇关于从node_modules导入一个模块时,Babelify抛出ParseError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!