3

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?

4
  • What would be the expected output? Is script.mjs executable? Commented Oct 4, 2021 at 9:59
  • Now I'm executing it with node word before. I would like to call it directly in PowerShell like in bash. Commented Oct 4, 2021 at 12:42
  • I don't know if I can help you since I'm not familiar with bash. I assume that under the hood, you still call it node .\script.mjs because 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 *.mjs file, execute it with node". I still don't see the issue with node .\script.mjs. Commented Oct 5, 2021 at 13:44
  • @Vivere, too long to write day-by-day. Commented Oct 5, 2021 at 15:40

3 Answers 3

4
+25

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.

Sign up to request clarification or add additional context in comments.

Comments

3

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.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.