NGMsoftware

NGMsoftware
로그인 회원가입
  • 매뉴얼
  • 학습
  • 매뉴얼

    학습


    Error Angular Typescript - 'Cannot find module moment/src/moment'. or 'node_…

    페이지 정보

    본문

    아래와 같이 코드를 작성한 경우 에러가 발생합니다.

    import moment from 'moment';

    Error Message

    'node_modules/moment/moment' has no default export.

     

    코드를 아래와 같이 변경해도 에러가 발생하죠?

    import moment from 'moment/src/moment';

    Error Message

    'Cannot find module moment/src/moment'.

     

    타입 스크립트에서 모멘트를 참조하는 정확한 규칙은 아래와 같습니다.

    import * as moment from 'moment';
    export default moment;

     

    참고로, Javascript의 Moment.js 또는 Typescript의 Moment.ts는 날짜와 시간 관련된 데이타의 파싱, 검증 및 조작을 쉽게 할 수 있도록 만들어진 라이브러리입니다. 대략 아래와 같은 코드를 가지고 있습니다. 코드를 분석해보면 어떻게 동작하는지 또는 사용해야 할지 알 수 있는데요. 대부분의 라이브러리들이 콘트롤키+클릭으로 정의로 이동이 가능합니다. 그리고, 소스를 직접 확인할 수 있어요.

    /**
     * @param strict Strict parsing disables the deprecated fallback to the native Date constructor when
     * parsing a string.
     */
    declare function moment(inp?: moment.MomentInput, strict?: boolean): moment.Moment;
    /**
     * @param strict Strict parsing requires that the format and input match exactly, including delimiters.
     * Strict parsing is frequently the best parsing option. For more information about choosing strict vs
     * forgiving parsing, see the [parsing guide](https://momentjs.com/guides/#/parsing/).
     */
    declare function moment(inp?: moment.MomentInput, format?: moment.MomentFormatSpecification, strict?: boolean): moment.Moment;
    /**
     * @param strict Strict parsing requires that the format and input match exactly, including delimiters.
     * Strict parsing is frequently the best parsing option. For more information about choosing strict vs
     * forgiving parsing, see the [parsing guide](https://momentjs.com/guides/#/parsing/).
     */
    declare function moment(inp?: moment.MomentInput, format?: moment.MomentFormatSpecification, language?: string, strict?: boolean): moment.Moment;
    
    declare namespace moment {
      type RelativeTimeKey = 's' | 'ss' | 'm' | 'mm' | 'h' | 'hh' | 'd' | 'dd' | 'w' | 'ww' | 'M' | 'MM' | 'y' | 'yy';
      type CalendarKey = 'sameDay' | 'nextDay' | 'lastDay' | 'nextWeek' | 'lastWeek' | 'sameElse' | string;
      type LongDateFormatKey = 'LTS' | 'LT' | 'L' | 'LL' | 'LLL' | 'LLLL' | 'lts' | 'lt' | 'l' | 'll' | 'lll' | 'llll';
    ...

     

    개발자에게 후원하기

    MGtdv7r.png

     

    추천, 구독, 홍보 꼭~ 부탁드립니다.

    여러분의 후원이 빠른 귀농을 가능하게 해줍니다~ 답답한 도시를 벗어나 귀농하고 싶은 개발자~

    감사합니다~

    • 네이버 공유하기
    • 페이스북 공유하기
    • 트위터 공유하기
    • 카카오스토리 공유하기
    추천0 비추천0

    댓글목록

    등록된 댓글이 없습니다.