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

Cannot find module log4js with TypeScript #1009

Closed
bbohec opened this issue May 13, 2020 · 4 comments · Fixed by #1229 or #1361
Closed

Cannot find module log4js with TypeScript #1009

bbohec opened this issue May 13, 2020 · 4 comments · Fixed by #1229 or #1361
Labels
documentation Improvements or additions to documentation
Milestone

Comments

@bbohec
Copy link

bbohec commented May 13, 2020

log4js installed as dependency through npm.

tsconfig.json using module:esnext
package.json using type:module

Not working :

import log4js = require("log4js");
//Import assignment cannot be used when targeting ECMAScript modules. Consider using 'import * as ns from "mod"', 'import {a} from "mod"', 'import d from "mod"', or another module format instead.
import * as log4js from "log4js";
//Cannot find module 'log4js'

Current workaround:

import * as log4js from "../../../node_modules/log4js/types/log4js";
@artoriasless
Copy link

image

refer

@kricha
Copy link

kricha commented Dec 21, 2021

image

refer

this gives

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: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)

@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

See #1360 (comment)

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
Projects
None yet
4 participants