从node_modules导入一个模块时,Babelify抛出ParseError [英] Babelify throws ParseError on import a module from node_modules

查看:128
本文介绍了从node_modules导入一个模块时,Babelify抛出ParseError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 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] 
` - libs
│` - test.js
` - main.js
/ pre>

然后,它工作正常。如何在 node_modules 中使用 babelify 的ES6样式模块?

解决方案

这就是Browserify转换的工作原理,转换只能在被引用的模块中直接有效。



如果您想要在node_modules中的模块进行转换,则需要向该模块添加 package.json 并添加 babelify 作为该模块的转换。例如

 browserify:{
transform:[
babelify
]
},

在你的 package.json plus babelify 作为依赖将告诉 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 in node_modules/libs

export default () => {
  console.log('Hello');
};

main.js (will be compiled to bundle.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 in node_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 add babelify as a transform for that module too. e.g.

"browserify": {
  "transform": [
    "babelify"
  ]
},

inside your package.json plus babelify as a dependency will tell browserify to run the babelify 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆