Skip to content

8.1.0 breaks named imports when using ESM #8320

@BenSjoberg

Description

@BenSjoberg

Is there an existing issue for this?

  • I have searched the existing issues

Current behavior

8.1.0 must seems to have changed something with exports, which affects projects using pure ESM. After updating, my application fails to launch with the following message:

import { Controller, Get, Header } from '@nestjs/common';
         ^^^^^^^^^^
SyntaxError: Named export 'Controller' not found. The requested module '@nestjs/common' is a CommonJS module, which may not support all module.exports as named exports.
CommonJS modules can always be imported via the default export, for example using:

Downgrading to 8.0.11 resolves the issue. A workaround without downgrading is to do something like:

import nestcommon from '@nestjs/common';
const import { Controller, Get, Header } = nestcommon;

But this is pretty clumsy.

Our project transpiles TypeScript to ESM, instead of to CommonJS, since some of our dependencies have moved to pure ESM without a CommonJS fallback.

Minimum reproduction code

https://github.com/BenSjoberg/nest-esm-import-issue-example

Steps to reproduce

  • Install @nestjs/common 8.0.11
  • Create a file called test.mjs with the content import { Controller } from '@nestjs/common';
    • Alternatively, named the file test.js and set "type": "module" in package.json
  • Run node test.mjs - it should work.
  • Upgrade @nestjs/common to 8.1.0, it will fail with the error I described.

The sample repo I linked shows the issue in a working nest.js project. The initial commit (bfcd0c99) works because it's pinned to Nest 8.0.x, the latest commit doesn't work because I updated to 8.1.0.

Expected behavior

Named imports should work from pure ESM as they did in 8.0.x.

Package

  • I don't know. Or some 3rd-party package
  • @nestjs/common
  • @nestjs/core
  • @nestjs/microservices
  • @nestjs/platform-express
  • @nestjs/platform-fastify
  • @nestjs/platform-socket.io
  • @nestjs/platform-ws
  • @nestjs/testing
  • @nestjs/websockets
  • Other (see below)

Other package

No response

NestJS version

No response

Packages versions

  • @nestjs/cli: 8.1.2
  • @nestjs/common: 8.1.0
  • @nestjs/core: 8.1.0
  • @nestjs/platform-express: 8.1.0
  • @nestjs/schematics: 8.0.3
  • @nestjs/testing: 8.1.0

Node.js version

16.11.1

In which operating systems have you tested?

  • macOS
  • Windows
  • Linux

Other

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    needs triageThis issue has not been looked into

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions