1

How can I register NodeJS as a default executable program for .js files on Windows and Linux

For example

//script01.js console.log("I'm executable") 

Then in cmd bash:

$: script.js I'm executable $: | 

On Windows I'm aware to the ControlPanel/Programs/Make a file type always open in specific program it work fine until I need to pass arguments to the script.

so let say I have :

 //script2.js console.log('argument 3 is :',process.argv[2]) 

then :

$: script2.js myArg argument 3 is : undefind $: | 

instead of

$: script2.js myArg argument 3 is : myArg $: | 

1 Answer 1

3

In your script.js file, add a shebang to the top of the file to indicate the script should be run by the node interpreter:

#!/usr/bin/env node 

In your package.json, add "bin": "path/to/script.js" and when you npm install (or npm link), npm will create an executable script.js as well as script.js.cmd that will run on windows.

Alternatively, you can just use node script.js each time to run the script.

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

3 Comments

there can be a way to add more the one executable scripts to package.json ?
@perymimon Yes, and you can give them different names as well docs.npmjs.com/files/package.json#bin
cool it work fine even for .cmd files . but now i'm curious how make .js files work on windows but not in npm project

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.