简体   繁体   English

从任何地方在Windows命令中执行单个Node.js文件

[英]Execute a single Node.js file in Windows command from anywhere

I have a Node.js file in c:\\scripts and want to execute them in Windows command from anywhere by node my-file.js . 我在c:\\scripts有一个Node.js文件,并希望通过node my-file.js在任何位置的Windows命令中执行它们。 It's kind of requirement for execute a bat file, which I can put the folder in %PATH% . 这是执行bat文件的一种要求,我可以将文件夹放在%PATH% I've tried to include c:\\scripts in %NODE_PATH% but it doesn't work. 我试图在%NODE_PATH%包含c:\\scripts但它不起作用。

nodejs has the notion of executables ... for example package nodejs具有可执行文件的概念......例如package

jslint

once installed can be directly executed from anywhere ... its installed using the -g flag as in : 一旦安装,就可以从任何地方直接执行...使用-g标志安装,如下所示:

npm install -g jslint

just like normal packages its code is installed in dir defined by env var 就像普通软件包一样,其代码安装在env var定义的目录中

NODE_PATH

additionally its executable lives in the bin dir which is by default added to PATH during initial nodejs install 此外,它的可执行文件位于bin目录中,默认情况下,它在初始nodejs安装期间添加到PATH

on linux 在Linux上

type jshint
jshint is /home/stens/node-v5.8.0/bin/jshint


echo $NODE_PATH
/home/stens/node-v5.8.0/lib/node_modules

on windows box similar happens just examine your PATH to see the nodejs bin dir ... on linux its a location relative to $NODE_PATH 在Windows框上类似的事情只是检查你的PATH看到nodejs bin目录...在linux上它相对于$ NODE_PATH的位置

UPDATE : 更新:

Alternative to above you can put a some-file (do Windows equivalent to chmod +x some-file to make it an executable) into a dir listed in your $PATH ... for example on linux if I issue 除了上面的替代,你可以将一个文件(将Windows等效于chmod + x some-file以使其成为可执行文件)放入$ PATH中列出的目录中...例如,如果我发布的话,在Linux上

type jslint

which is found because PATH contains dir /home/stens/node-v5.8.0/bin ... above command outputs : 找到因为PATH包含dir /home/stens/node-v5.8.0/bin ...以上命令输出:

jslint is hashed (/home/stens/node-v5.8.0/bin/jslint)

then show contents 然后显示内容

cat /home/stens/node-v5.8.0/bin/jslint cat /home/stens/node-v5.8.0/bin/jslint

#!/usr/bin/env node

var main = require("../lib/main.js");

main.runMain(main.parseArgs());

you can see the executable file in just nodejs code with the 1st line 您可以在第一行的nodejs代码中看到可执行文件

#!/usr/bin/env node

which tell the system to execute it using node (dunno the Windows way but similar must be available) 告诉系统使用节点执行它(dunno是Windows方式但类似必须可用)

so its executed using 所以它执行使用

jslint blah input-blah parm-blah goes-blah here-blah

Hopefully this tips you in the right direction 希望这会向正确的方向提示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM