Skip to content

SyntaxError: Named export 'OnModuleDestroy' not found. The requested module '@nestjs/common' is a CommonJS module, which may not support all module.exports as named exports. #114

@huan

Description

@huan

We found that the Nestjs with the swc transpiler have some compatible problems.

The Problem

When we are using the swc as the transpiler settings in the tsconfig.json

{
  "ts-node": {
    "transpileOnly": true,
    "transpiler": "ts-node/transpilers/swc-experimental",
  },
}

We will get the below error:

$ ./statuspage.saga.spec.ts 
file:///home/huan/git/wechaty/friday/src/wechaty-repository/wechaty.repository.ts:2
import { Injectable, OnModuleDestroy, OnModuleInit } from '@nestjs/common';
                     ^^^^^^^^^^^^^^^
SyntaxError: Named export 'OnModuleDestroy' 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:

import pkg from '@nestjs/common';
const { Injectable, OnModuleDestroy, OnModuleInit } = pkg;

    at ModuleJob._instantiate (node:internal/modules/esm/module_job:124:21)
    at async ModuleJob.run (node:internal/modules/esm/module_job:181:5)
    at async Promise.all (index 0)
    at async ESMLoader.import (node:internal/modules/esm/loader:281:24)
    at async loadESM (node:internal/process/esm_loader:88:5)
    at async handleMainPromise (node:internal/modules/run_main:65:12)

The Workaround

Because we are extending the tsconfig.json from @chatie/tsconfig, we add a ts-node.transpiler = '' to remove the swc transpiler, and then the system work as expected:

{
  "ts-node": {
    // It is faster to skip typechecking.
    // Remove if you want ts-node to do typechecking.
    "transpileOnly": true,
-     "transpiler": "ts-node/transpilers/swc-experimental",
+     "transpiler": "",  // <- set it to empty, or make sure it does not set in the tsconfig.json
  },
}

Reproduceable Repo

Related Issues

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions