2017-10-14 11 views
1

を使用して、エラーmoment.momentを得ることが...私はmoment.jsと接続詞にジャスミンをしようとしていますが、私はこのエラーを取得していますジャスミンとMoment.js

debug.js:21 Uncaught TypeError: (0 , _moment.moment) is not a function 

ないように注意してください関数ではありませんそれがmoment.jsに関連している場合、または私がこれを間違って設定している場合。助けをお待ちしています。

//script.js 
 

 
import { moment } from 'moment'; 
 

 
export class Age { 
 
    
 
    constructor(age, secondDate) { 
 
    this.age = age; 
 
    this.secondDate = secondDate; 
 
    } 
 

 
    getSecondsBetweenTwoDates(age, secondDate){ 
 
    age = moment(this.age).format('l'); 
 
    secondDate = moment(this.secondDate).format('l'); 
 
    //differenceInSeconds = moment((this.secondDate).diff(this.age, 'days')); 
 
    differenceInDays = age.diff(secondDate, 'days'); 
 
    //let differenceInDays = this.age - this.secondDate 
 
    return differenceInDays; 
 

 
    } 
 
} 
 

 

 
//age-spec.js 
 

 
import { Age } from './../js/age.js'; 
 

 
describe('Age', function() { 
 
    
 
    let reusableDate, 
 
     today, 
 
     testDate = '2016-10-05', 
 
     date = '2016-10-10'; 
 

 
    beforeEach(() => { 
 
    reusableDate = new Age(date, testDate); 
 
    console.log(reusableDate); 
 
    const mockedDateAndTime = '2017-03-02 00:00:00'; 
 
    today = moment(mockedDateAndTime).toDate(); 
 
    console.log('this is today', today); 
 
    jasmine.clock().mockDate(today); 
 
    }); 
 

 
    it('should return the difference between today',() => { 
 
    console.log(date); 
 
    console.log(testDate); 
 
    console.log(reusableDate.getSecondsBetweenTwoDates(date, testDate)); 
 
    console.log(typeof(reusableDate.getSecondsBetweenTwoDates)); 
 
    //expect(5).toEqual(reusableDate.getSecondsBetweenTwoDates()); 
 
    }); 
 

 

 
});

私は全くbeforeEachブロックを使用していない、それは私がGoogleで見つかった、としようとしていただけのものだった...私もそうのようなカルマモーメントプラグインをインストール:

フレームワーク:[ 'jqueryの-3.2.1'、 'ジャスミン'、 'browserify'、 'モーメント2.9.0']、

プラグイン:[ 'カルマ-jqueryの' 「karma- browserify '、 'カルマ-瞬間'、 'カルマ・ジャスミン'、 'カルマ - クロム - ランチャー'、 「カルマ・ジャスミン-HTML-記者 ]

答えて

1

momentパッケージには、momentという名前のエクスポートはありません。ほとんどのNPMパッケージはCommonJS/UMDで、主なエクスポートはmodule.exportsです。たとえmomentas ES module with default exportと書かれていても、different entry points for different environmentsを持っているので、UMDモジュールとしてインポートすることもできます。

それは選択肢がプロジェクト構成に依存してもよい

import * as moment from 'moment'; 

または

import moment from 'moment'; 

でなければなりません。一般的に* asは、実際のCommonJSエクスポートをよりよく反映しており、バンドル時にプロジェクトがCJSモジュールにフォールバックするように設定されていれば、問題を起こしにくいです。

+0

答えをありがとう、私はそれを認識していませんでした。 – Lucky500

0

私はいくつかの理由で私のES6のために、ダムミスを犯しましたファイルの先頭にあるインポート構文が機能しませんでした。

script.jsファイルでこれを変更しました。 'moment'からimport {moment};

これに: var moment = require( 'moment');

+0

非常に特殊な理由で動作しません。輸出という瞬間はないからです。 – estus

関連する問題