Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Import error when using ES6 module syntax #1063

Closed
Sdator opened this issue Feb 24, 2021 · 5 comments · Fixed by #1229 or #1361
Closed

Import error when using ES6 module syntax #1063

Sdator opened this issue Feb 24, 2021 · 5 comments · Fixed by #1229 or #1361
Labels
documentation Improvements or additions to documentation duplicate This issue or pull request already exists
Milestone

Comments

@Sdator
Copy link

Sdator commented Feb 24, 2021

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)
@kevintechie
Copy link

Also missing from log4js-api.

@kricha
Copy link

kricha commented Dec 11, 2021

any news here?

@lamweili
Copy link
Contributor

lamweili commented Feb 1, 2022

Duplicate of #1009.

@lamweili lamweili added the duplicate This issue or pull request already exists label Feb 1, 2022
@lamweili lamweili added the documentation Improvements or additions to documentation label Apr 11, 2022
@lamweili lamweili added this to the 6.4.5 milestone Apr 11, 2022
@lamweili
Copy link
Contributor

lamweili commented Apr 11, 2022

I think this is a documentation issue.
The documentation below was dated 5 years ago (02c6ebb) and might have not been updated.

import { configure, getLogger } from "log4js";
configure("./filename");
const logger = getLogger();
logger.level = "debug";
logger.debug("Some debug messages");

configure({
  appenders: { cheese: { type: "file", filename: "cheese.log" } },
  categories: { default: { appenders: ["cheese"], level: "error" } }
});

I have the updated documentation below. Let me know if it works.

import * as log4js from "log4js";
log4js.configure({
  appenders: { cheese: { type: "file", filename: "cheese.log" } },
  categories: { default: { appenders: ["cheese"], level: "error" } }
});

const logger = log4js.getLogger();
logger.level = "debug";
logger.debug("Some debug messages");

@lamweili
Copy link
Contributor

lamweili commented Apr 12, 2022

@kevintechie

Also missing from log4js-api.


CJS

const log4js = require('@log4js-node/log4js-api');
const logger = log4js.getLogger('my-library');
logger.info("Library starting up");

ESM

import * as log4js from '@log4js-node/log4js-api';
const logger = log4js.getLogger('my-library');
logger.info("Library starting up");

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation duplicate This issue or pull request already exists
Projects
None yet
4 participants