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?

Fred Hors
  • 3,258
  • 3
  • 25
  • 71
  • What would be the expected output? Is `script.mjs` executable? – Vivere Oct 04 '21 at 09:59
  • Now I'm executing it with `node` word before. I would like to call it directly in PowerShell like in bash. – Fred Hors Oct 04 '21 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](https://stackoverflow.com/questions/48179714/how-can-an-es6-module-be-run-as-a-script-in-node)) 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`. – Vivere Oct 05 '21 at 13:44
  • @Vivere, too long to write day-by-day. – Fred Hors Oct 05 '21 at 15:40

3 Answers3

4

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.

stackprotector
  • 10,498
  • 4
  • 35
  • 64
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.

JPBlanc
  • 70,406
  • 17
  • 130
  • 175
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

Fazle
  • 11
  • 3