Home > Back-end >  How to execute node.js file with Powershell without `node` before the name?
How to execute node.js file with Powershell without `node` before the name?

Time:10-06

Is there a way to execute node.js files from Powershell without calling the file with node before the file name?

Example:

Now I'm executing it like this: node .\script.mjs.

I would like to execute it with the name only: .\script.mjs.

How to?

CodePudding user response:

You can register the .mjs file extension to be opened with node.exe by default. Then you can call node by just providing a path to a .mjs file. Steps:

  1. Right click any .mjs file
  2. Click on Open with / Open with...
  3. Click on Choose another app / More apps ↓ > Look for another app on this PC
  4. Navigate to your node.exe file
  5. Tick the Always use this app to open .mjs files checkbox
  6. Confirm with OK

Now you can just enter .\script.mjs in CMD or PowerShell to automatically open the file with node.

CodePudding user response:

Maybe you can create an executable from that file using vercel/pkg, then can run pkg .\script.mjs -t win to create an exe file using pkg

  • Related