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?
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.
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.
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
script.mjsexecutable?nodeword before. I would like to call it directly in PowerShell like in bash.node .\script.mjsbecause if the file is not executable, what should happen? Probably on your linux environment you have something configured (such as this) that allows you to do that. I don't know if you can make PS do something like "this is a*.mjsfile, execute it withnode". I still don't see the issue withnode .\script.mjs.