javascript - Meteor 找不到重新导出的模块

标签 javascript meteor babeljs node-modules

我正在使用 ES6 编写一个 Meteor 应用程序,并且我有许多子组件我想保留为单独的 npm 包。我有一个名为 frog-utils 的库,它在所有包之间共享,并且包含通用的辅助函数。

当我尝试在 frog-utils 中重新导出模块时,它在普通节点上工作正常,但 Meteor 提示说:

W20161114-10:12:17.483(1)? (STDERR) Error: Cannot find module './color_range'
W20161114-10:12:17.484(1)? (STDERR)     at require (packages/modules-runtime.js:109:19)
W20161114-10:12:17.484(1)? (STDERR)     at meteorInstall.node_modules.frog-utils.dist.index.js (packages/modules.js:17407:20)

(这是来自同一目录中普通节点的示例)

~/s/F/frog (ac-collab) $ node
> frogutils = require('frog-utils')
{ color_range: [Getter],
  uuid: [Function: uuid],
  currentDate: [Function: currentDate],
  booleanize: [Function: booleanize],
  shorten: [Function: shorten],
  compose: [Function: compose],
  composeReducers: [Function: composeReducers],
  notEmpty: [Function: notEmpty],
  identity: [Function: identity],
  getKey: [Function: getKey] }

我在 ES6 中编写,使用 Babel 创建模块公开的输出文件,ES5 对我来说似乎没问题:

var _color_range = require('./color_range');

Object.defineProperty(exports, 'color_range', {
  enumerable: true,
  get: function get() {
    return _interopRequireDefault(_color_range).default;
  }
});

(这里是我用的 ES6 行)

export {default as color_range} from './color_range'

最佳答案

您正在测试哪个版本的节点?我敢打赌,如果你使用了 meteor node 并尝试了相同的 require('frog-utils') 它不会起作用,因为 meteor 目前使用节点 4.5(至少在1.4.X)。

恐怕如果不编译,您将无法在 npm 包中使用 ES6(另请参阅 https://github.com/meteor/meteor/issues/4828)。然而编译并不是很难,你可以看看我是如何解决一个非常相似的问题的: https://github.com/chfritz/ros_msg_utils/blob/add_babel/package.json

诀窍是定义一个脚本,在安装时使用 babel 编译代码。

  ...
  "main": "dist/index.js",
  "scripts": {
    "compile": "babel --presets es2015 index.js -d dist/ && babel --presets es2015 lib -d dist/lib/",
    "preinstall": "npm run compile"
  ...

关于javascript - Meteor 找不到重新导出的模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40585266/

相关文章:

javascript - 如何正确检查 Set 是否为空

javascript - 如何根据window.width()加载不同的HTML?

typescript - 带有 typescript 和 karma 的 Babelify 项目

html - meteor 错误: Couldn't parse .。嗯..一些 HTML 文件,在某行。对不起

javascript - 找不到模块 'babel-core' 中的错误。使用 react.js、webpack 和 express 服务器

javascript - index.html 不从 babel 加载转译的 javascript

javascript - 用于添加内联样式的 jQuery/JavaScript 切换按钮

javascript - 在 Azure 函数中运行 Playwright

meteor - 如何在 Meteor 中使用 Prerenderio?

javascript - Meteor 仅检索集合中所有项目的一张图像