2016-11-14 114 views
7

我正在寫一個使用ES6的流星應用程序,我有一些子組件,我想保留爲獨立的npm軟件包。我有一個名爲frog-utils的庫,它在所有包中共享,並且包含通用的幫助函數。流星找不到再出口模塊

當我嘗試重新導出模塊青蛙-utils的,它正常工作與普通節點,但流星抱怨說:

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,用巴貝爾創造其由模塊暴露出的輸出文件,和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' 

回答

4

哪個節點的版本是你的測試?我敢打賭,如果你做了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" 
    ... 
+0

謝謝,但我確實指定我使用Babel編譯到ES5,所以這不是問題。 –

1

這似乎已經在最新版本流星(1.4.2.1)已經解決了,突然下起「公正的工作」。

+0

你應該把獎金獎勵給千年發展目標或你自己(你已經付錢了,所以你應該給它) – Mikkel