Skip to content

Import error when using ES6 module syntax #1063

@Sdator

Description

@Sdator

Since my project has enabled "type": "module" in package.json, an error was thrown when I imported it.

If I remove "type": "module" and use the CJS module, all modules in my project will need to be rewritten. Is there a good solution? thank

The following is the error message:

import { configure, getLogger } from "log4js";
         ^^^^^^^^^
SyntaxError: Named export 'configure' not found. The requested module 'log4js' 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 'log4js';
const { configure, getLogger } = pkg;

    at ModuleJob._instantiate (node:internal/modules/esm/module_job:105:21)
    at async ModuleJob.run (node:internal/modules/esm/module_job:151:5)
    at async Loader.import (node:internal/modules/esm/loader:166:24)
    at async Object.loadESM (node:internal/process/esm_loader:68:5)

Metadata

Metadata

Assignees

No one assigned

    Labels

    documentationImprovements or additions to documentationduplicateThis issue or pull request already exists

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions