#How do you require a module in NodeJS without running it?

1 messages · Page 1 of 1 (latest)

shy oyster
#

If I have these 2 files(just an example) in my project, how do I require hello.js without having it run in bye.js at the require line, but also be able to run hello.js from the command line? If I remove run(); from hello.js I won't be able to run it from the command line anymore.

hello.js

function run() {
  console.log("hello");
}
run();
module.exports = run;

bye.js

const hello = require('./hello.js');

(async () =>  {
  await hello.run();
})
shy oyster
#

I think I figured it out

#

Had to remove the async function from bye.js, and then added this code to hello.js

  run();
}```