Fred Hors
Fred Hors

Reputation: 4156

How to execute node.js file with Powershell without `node` before the name?

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?

Upvotes: 3

Views: 1322

Answers (3)

JPBlanc
JPBlanc

Reputation: 72680

Agree with @tackprotector you can do the same thing using the cmd.exe command line with the two following tools :

Assoc displays or modifies file name extension associations. If used without parameters, assoc displays a list of all the current file name extension associations. If you type assoc .txt it returns .txt=txtfile.

Ftype displays or modifies file types that are used in file name extension associations. If used without an assignment operator (=), ftype displays the current open command string for the specified file type. If used without parameters, ftype displays the file types that have open command strings defined. If you type ftype textfile it returns textfile="%ProgramFiles%\Windows NT\Accessories\WORDPAD.EXE" "%1"

So if I type assoc .mjs it returns that nothing is associeted with .mjs

So I type (in a cmd. as admin) :

assoc .mjs=nodejsfile
ftype nodejsfile=%ProgramFiles(x86)%\Notepad++\notepad++.exe %1

The first time I invoke a .mjs file it will ask me if I want to use notepad++ to open it, after that the association is done. You can do the same with node.exe.

Upvotes: 3

stackprotector
stackprotector

Reputation: 13588

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.

Upvotes: 4

Fazle
Fazle

Reputation: 11

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

Upvotes: 0

Related Questions