【问题标题】:import a module from node_modules with babel but failed使用 babel 从 node_modules 导入模块但失败
【发布时间】:2015-10-27 15:27:12
【问题描述】:

我用es6写了一个模块并发布到npm,我想在另一个项目中使用它,所以我这样输入:

import {ActionButton} from 'rcomponents'

但它不起作用:

D:\github\blog\node_modules\rcomponents\src\actionButton.jsx:1
(function (exports, require, module, __filename, __dirname) { import React fro
                                                              ^^^^^^
SyntaxError: Unexpected reserved word
    at exports.runInThisContext (vm.js:73:16)
    at Module._compile (module.js:443:25)
    at Module._extensions..js (module.js:478:10)
    at Object.require.extensions.(anonymous function) [as .jsx] (D:\github\blog\
node_modules\babel\node_modules\babel-core\lib\api\register\node.js:214:7)
    at Module.load (module.js:355:32)
    at Function.Module._load (module.js:310:12)
    at Module.require (module.js:365:17)
    at require (module.js:384:17)
    at D:\github\blog\node_modules\rcomponents\src\index.js:3:19
    at Object.<anonymous> (D:\github\blog\node_modules\rcomponents\src\index.js:
7:3)

这是我在 webpack 中的 js 加载器配置:

{ test: /\.jsx?$/, loader: `babel?cacheDirectory=${babelCache}` }

当我尝试导入不是来自node_modules 的模块时,babel 工作得很好。但是从node_modules导入一个模块,babel好像不行?

【问题讨论】:

    标签: reactjs babeljs


    【解决方案1】:

    您可以使用https://www.npmjs.com/package/babel-node-modules 对于这种情况

    npm install --save-dev babel-node-modules
    require('babel-node-modules')([
      'helloworld' // add an array of module names here 
    ]);
    

    然后它将列出的模块编译为其他文件

    【讨论】:

      【解决方案2】:

      一般来说,上传到 npm 的包应该是预编译的,所以用户收到的是普通的 JS,不需要构建步骤。为此使用npm prepublish

      但是,如果您使用的是 webpack,则可以在 webpack 配置中指定 exclude 函数(参见 webpack docs):

      module: {
        loaders: [{
          test: /.jsx?$/,
          loader: 'babel-loader',
          exclude(file) {
            if (file.startsWith(__dirname + '/node_modules/this-package-is-es6')) {
              return false;
            }
            return file.startsWith(__dirname + '/node_modules');
          },
      

      如果你直接使用 babel,you can write a similar ignore function in the require hook

      【讨论】:

      • > 如果你直接使用 babel,你可以在 require 钩子中写一个类似的忽略函数。但这会去哪里?你在哪里添加require('@babel/register') 语句?在每个使用 ES6 模块的文件中?
      【解决方案3】:

      the babel docs:

      注意:默认情况下,所有对 node_modules 的要求都将被忽略。您可以通过传递忽略正则表达式来覆盖它。

      通常期望node_modules 中的模块已经提前被转译,因此它们不会被 Babel 处理。如果您不这样做,那么您需要告诉它它可以处理哪些文件。 ignore 允许这样做。

      require("babel/register")({
          // Ignore everything in node_modules except node_modules/rcomponents.
          ignore: /node_modules\/(?!rcomponents)/
      });
      

      【讨论】:

      • 如果您想对 babel-node 使用相同的方法,您可以使用 babel-node --ignore '/node_modules/(?!my_module1|my_module2)' script.js
      • 这不是 Babel 6 的合适答案,我遇到了类似的情况,似乎找不到从节点模块转换模块的任何方法。就像那些仍在 Babel 6 中寻找解决方案的人的附录一样。
      • 我同意,我也无法使用 babel 6 编译 node_modules 模块
      • 如果您有特定的可重现示例,请随时访问 Babel 在 Slack 上的支持频道。不过,这应该在 Babel 6 中有效。
      • @remino 它需要作为应用程序代码的一部分运行,并且它需要在文件中加载之前任何需要编译的文件容器代码,所以你可以有一个加载 babel 的根文件,然后在 Babel 之后使用require("./app.js"); 来运行你的应用程序代码。
      猜你喜欢
      • 1970-01-01
      • 2021-11-26
      • 2021-04-17
      • 1970-01-01
      • 2017-09-22
      • 2023-03-28
      • 2019-03-03
      • 1970-01-01
      • 2019-04-28
      相关资源
      最近更新 更多