1

Given a file named weekday.js in my src directory of:

#! /usr/bin/env node

export const mon = () => console.log('Monday')

export const tues = () => console.log('Tuesday')

export const wed = () => console.log('Wednesday')

export default async () => {
  await mon()
  await tues()
  await wed()
}

and in package.json scripts of:

"type": "module",
"scripts": {
  "week": "node src/weekday.js"
},

how should the file be called when the command of:

npm run week

does not log anything to the console and I've also tried:

"node -e 'import(\"./src/weekday.js\")'"

In my research all I am finding is about the addition of module:

How should an export default be called in package.json's scripts?

Edit

Per the comment of:

Try to execute the function as a normal function: "node -e 'require("./src/weekday.js")();'"

throws the terminal error:

Must use import to load ES Module

so tried:

"scripts": {
    "weekday": "node -e 'import(\"./src/weekday.js\")()'"
  },

but throws error:

[eval]:1
import("./src/weekday.js")()
                              ^
TypeError: ImportCall("./src/weekday.js") is not a function
DᴀʀᴛʜVᴀᴅᴇʀ
  • 7,681
  • 17
  • 73
  • 127
  • Try to execute the function as a normal function: "node -e 'require(\"./src/weekday.js\")();'" – hoangdv Jun 25 '22 at 12:26
  • @hoangdv that throws the error `Must use import to load ES Module` – DᴀʀᴛʜVᴀᴅᴇʀ Jun 25 '22 at 15:18
  • The async/await doesn't seem to make sense in your example – Bergi Jun 25 '22 at 17:34
  • Why did you put the functionality inside a (default-exported) function instead of just running the code, if your *weekday.js* is meant to be a cli script? Why do you `export` stuff at all, is there a module that imports these things? – Bergi Jun 25 '22 at 17:36

0 Answers0