Reputation: 4156
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
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
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:
.mjs
filenode.exe
fileNow you can just enter .\script.mjs
in CMD or PowerShell to automatically open the file with node.
Upvotes: 4
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