問題

I am writing a Meteor application using ES6, and I have a number of sub-components that I want to keep as separate npm packages. I have a library called frog-utils, which is shared across all packages, and which contain common helper functions.

When I try to re-export a module in frog-utils, it works fine with plain node, but Meteor complains that:

 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)
 

(Here's an example from plain node, in the same directory)

 ~/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] }
 

I'm writing in ES6, using Babel to create the output files which are exposed by the module, and the ES5 seems fine to me:

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

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

(Here's the ES6 line I use)

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

  ベストアンサー

Which version of node are you testing with? I bet you if you did meteor node and tried the same require('frog-utils') it wouldn't work, because meteor currently uses node 4.5 (at least in 1.4.X).

I'm afraid you won't be able to use ES6 in your npm package without compiling it (also see https://github.com/meteor/meteor/issues/4828 ). However compiling is not very hard, you can look how I just solved a very similar problem in: https://github.com/chfritz/ros_msg_utils/blob/add_babel/package.json

The trick is to define a script that compiles the code using babel on install.

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

  同じタグがついた質問を見る

javascriptmeteorbabeljsnode-modules