Skip to content

Commit

Permalink
Updated way to check for main in ESM
Browse files Browse the repository at this point in the history
  • Loading branch information
rodyvansambeek committed Mar 21, 2024
1 parent 3473425 commit f9111d5
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 2 deletions.
8 changes: 8 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,8 @@
"jest": "^29.7.0",
"ts-jest": "^29.1.2",
"typescript": "^5.4.2"
},
"dependencies": {
"es-main": "^1.3.0"
}
}
4 changes: 3 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#!/usr/bin/env node

import esMain from "es-main";

const getCurrentISOWeek = (date: Date) => {
const dayOfWeek = date.getDay();
const currentDay = date.getDate();
Expand All @@ -18,6 +20,6 @@ const getCurrentISOWeek = (date: Date) => {

export default getCurrentISOWeek;

if (require.main === module) {
if (esMain(import.meta)) {
console.log(getCurrentISOWeek(new Date()));
}
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
// "moduleDetection": "auto", /* Control what method is used to detect module-format JS files. */

/* Modules */
"module": "CommonJS", /* Specify what module code is generated. */
"module": "ESNext", /* Specify what module code is generated. */
"rootDir": "./src", /* Specify the root folder within your source files. */
"moduleResolution": "node10", /* Specify how TypeScript looks up a file from a given module specifier. */
// "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */
Expand Down

0 comments on commit f9111d5

Please sign in to comment.