Sorry for maybe a stupid question, a problem, but I do not understand)

Now I run the following script

node script.js 

I want to run just a command, enter the name of the program from anywhere, like

 script 

and all)

How to implement it?

  • And what's the problem, I just tried on Win7 + Node 4.4.3, I enter the directory where js is and run it even without the * .js extension - pnp2000
  • @ vnn198 I need to run from anywhere in the system as a regular console program - WebWorkDeveloper

3 answers 3

I added to package.json

 "preferGlobal": true, "bin": { "script": "script.js" }, 

To the top of the script.js file (entry point)

 #!/usr/bin/env node 

Next, create a link using

 npm link 

And after that we can run our script script just

 script 

    It is necessary to add the path in the PATH

    1. On the desktop, right-click on the Computer icon.
    2. In the context menu, select Properties.
    3. Click on the Advanced system settings link.
    4. Click Environment Variables. In the Environment Variables section in the System Variables subsection, select the path environment variable and at the end of the line through ; specify the path to your script.
    • If everything was so simple :) I already checked, does not work - WebWorkDeveloper
    • It should work, for example, throw your script into the folder that is written in the path. For example, I threw the script for the sample into the nodejs folder which is already registered in the path, and everything worked, by the way, after registering, you need to open a new cmd window, because the path is initialized only when running cmd. And yet, if non-standard modules are used in the script, then it will be necessary to register full paths to them in the script itself. - pnp2000

    Presumably you have Windows .
    I have so:

    1. Created .bat , which is executed with each start cmd :

       HKEY_CURRENT_USER [SOFTWARE] [Microsoft] [Command Processor] (Autorun) = "%systemdrive%\path\to\file.bat" 
    2. Creating links of the form:

       doskey sudo = powershell %Tcmd%\ImplementationSudoCommand.ps1 $* 

      Now in Windows there is a convenient way to run any program with elevated permissions: sudo cmd .

    • one
      m, sudo in Windows? you probably have a developer preview - KoVadim 4:52 pm
    • @KoVadim, I shove this script everywhere, but it’s convenient to sit without special rights, but sometimes it’s necessary to increase it. In Windows, this is hell. - user207618
    • Yes, there is no default sudo in Windows. - Kirby
    • @Kirby, you are insightful :) - user207618