From CommonJS to ES modules (ESM) in TypeScript

Поделиться
HTML-код
  • Опубликовано: 15 июл 2024
  • In this video, you will learn how to make the switch from CommonJS to ES Module Syntax.
    My FREE programming apps:
    - apps.apple.com/app/id659282498
    - play.google.com/store/apps/de...
    My FREE TypeScript Course:
    - typescript.tv/
    Timeline:
    00:00 History of ES modules
    02:09 TypeScript project setup with CommonJS (CJS)
    03:02 Migrating from CommonJS to ES modules (ESM)
    03:27 Unexpected token export
    03:57 require is not defined in ES module scope
    Follow TypeScript TV:
    🌐 typescript.tv
    🐦 / typescripttv
    📺 / typescripttv
    #TypeScript #JavaScript #nodejs
  • НаукаНаука

Комментарии • 8

  • @TypeScriptTV
    @TypeScriptTV  Год назад +1

    The main drawback of transitioning to ESM (ECMAScript Modules) at the moment is the inability of jest.mock to mock ES modules without Babel: github.com/jestjs/jest/issues/10025

    • @PatricSjoeoe
      @PatricSjoeoe 5 месяцев назад

      Recommend esmock and vitest instead.

  • @fluttterdev1k
    @fluttterdev1k Год назад +2

    thanks buddy that worked really well

    • @TypeScriptTV
      @TypeScriptTV  Год назад +1

      I am glad to hear that. This week I will also add another video to my TypeScript series. Best regards, Benny.

  • @haiiry
    @haiiry Год назад +1

    One thing that bothers me about modules is .js extension inside the import statement of the TS file. Another is that the pure esm library is not compatible with commonjs project, so I have to use an older version or skip that library and use a different one.

    • @TypeScriptTV
      @TypeScriptTV  Год назад +2

      Totally! There are also new file extensions (.cjs & .mjs) and differing behavior of the two module systems: nodejs.org/api/esm.html#differences-between-es-modules-and-commonjs

  • @user-cm3pm4oh9j
    @user-cm3pm4oh9j Месяц назад

    How can we migrate an existing project with large codebase as I don't want to edit each and every file

    • @TypeScriptTV
      @TypeScriptTV  29 дней назад

      Hey, the best way to do this is using ts2esm: github.com/bennycode/ts2esm