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 #1360

Closed
2277509846 opened this issue Jan 18, 2023 · 6 comments · Fixed by #1361
Closed

Cannot find module log4js with TypeScript #1360

2277509846 opened this issue Jan 18, 2023 · 6 comments · Fixed by #1361

Comments

@2277509846
Copy link

2277509846 commented Jan 18, 2023

Use the import method to import log4js, execute the tsc command, and the error "Cannot find module 'log4js'" will be reported, but the compiled js file can run normally.

error TS2792: Cannot find module 'log4js'
index.ts:
`import 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");`
image

@lamweili
Copy link
Contributor

lamweili commented Jan 18, 2023

It seems that ts-node works for runtime. But tsc fails to build.

You can download this: test.zip
Unzip and run the following commands within the folder:

npm i
npx ts-node test.ts # works
npx tsc test.ts     # fails

Switching to import * as log4js from 'log4js'; resolves the issue.

@2277509846
Copy link
Author

How to write the tsconfig.json file so that tsc can find log4js.d.ts. This is the tsconfig.json file:
{
"compilerOptions": {
/* Language and Environment /
"target": "es2020", /
Set the JavaScript language version for emitted JavaScript and include compatible library declarations. /
/
Modules /
"module": "es2020", /
Specify what module code is generated. /
"outDir": "./dist", /
Specify an output folder for all emitted files. /
"esModuleInterop": true, /
Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. /
"forceConsistentCasingInFileNames": true, /
Ensure that casing is correct in imports. /
/
Type Checking /
"strict": true, /
Enable all strict type-checking options. */
"skipLibCheck": true
}
}
image

@lamweili
Copy link
Contributor

lamweili commented Jan 18, 2023

I have included your tsconfig.json into replaced.zip.
Unzip and run the following commands within the folder:

npm i
npx tsc

Sorry, I'm not so familiar with TypeScript. But I don't have any errors.
Any where that I could be wrong?

EDITED: This is because the tsconfig.json contains comments which invalidated it and fallbacks to default configuration.

@2277509846
Copy link
Author

2277509846 commented Jan 18, 2023

It seems that these two configuration options cause the error.
{
"compilerOptions": {
"target": "es2015",// cause error
"module": "es2015",// cause error
"outDir": "./dist",
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"skipLibCheck": true
}
}

@lamweili
Copy link
Contributor

After removing the comments in tsconfig.json, both es2020 and es2015 (for target and module) have errors.

You will need to add "moduleResolution": "node".

es2015

  {
    "compilerOptions": {
      "target": "es2015",
      "module": "es2015",
      "outDir": "./dist",
      "esModuleInterop": true,
      "forceConsistentCasingInFileNames": true,
+     "moduleResolution": "node",
      "strict": true,
      "skipLibCheck": true
    }
  }

es2020

  {
    "compilerOptions": {
      "target": "es2020",
      "module": "es2020",
      "outDir": "./dist",
      "esModuleInterop": true,
      "forceConsistentCasingInFileNames": true,
+     "moduleResolution": "node",
      "strict": true,
      "skipLibCheck": true
    }
  }

@2277509846
Copy link
Author

My problem has been solved. Thank you very much

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants