从命令行运行 Node.js 脚本
🌐 Run Node.js scripts from the command line
运行 Node.js 程序的常用方法是运行全局可用的 node 命令(在安装 Node.js 后)并传递你想要执行的文件名。
🌐 The usual way to run a Node.js program is to run the globally available node command (once you install Node.js) and pass the name of the file you want to execute.
如果你的主要 Node.js 应用文件是 app.js,你可以通过输入以下命令来调用它:
🌐 If your main Node.js application file is app.js, you can call it by typing:
node app.js
上面,你明确告诉 shell 使用 node 来运行你的脚本。你也可以在你的 JavaScript 文件中通过一行 “shebang” 来嵌入这个信息。“shebang” 是文件的第一行,它告诉操作系统使用哪个解释器来运行脚本。下面是 JavaScript 的第一行示例:
#!/usr/bin/node
上面,我们明确地给出了解释器的绝对路径。并不是所有操作系统的 bin 文件夹中都有 node,但所有系统都应该有 env。你可以告诉操作系统使用 node 作为参数来运行 env:
🌐 Above, we are explicitly giving the absolute path of interpreter. Not all operating systems have node in the bin folder, but all should have env. You can tell the OS to run env with node as parameter:
#!/usr/bin/env node
// your javascript code
要使用 shebang,你的文件应具有可执行权限。你可以通过运行以下命令为 app.js 赋予可执行权限:
🌐 To use a shebang, your file should have executable permission. You can give app.js the executable permission by running:
chmod u+x app.js
运行该命令时,请确保你位于包含 app.js 文件的同一目录中。
🌐 While running the command, make sure you are in the same directory which contains the app.js file.
将字符串作为参数传递给 node 而不是文件路径
🌐 Pass string as argument to node instead of file path
要将字符串作为参数执行,可以使用 -e、--eval "script"。将以下参数作为 JavaScript 来执行。REPL 中预定义的模块也可以在脚本中使用。
🌐 To execute a string as argument you can use -e, --eval "script". Evaluate the following argument as JavaScript. The modules which are predefined in the REPL can also be used in script.
在 Windows 上,使用 cmd.exe 时单引号不会正常工作,因为它只识别双引号 " 用于引用。在 Powershell 或 Git bash 中,单引号 ' 和双引号 " 都可使用。
🌐 On Windows, using cmd.exe a single quote will not work correctly because it only recognizes double " for quoting. In Powershell or Git bash, both ' and " are usable.
node -e "console.log(123)"
自动重启应用
🌐 Restart the application automatically
从 Node.js V16 起,内置了一个选项,可以在文件更改时自动重启应用。这对开发过程非常有用。要使用此功能,需要向 Node.js 传递 --watch 标志。
🌐 As of Node.js V16, there is a built-in option to automatically restart the application when a file changes. This is useful for development purposes.
To use this feature, you need to pass the --watch flag to Node.js.
node --watch app.js
所以当你更改文件时,应用会自动重启。阅读 --watch 参数文档。
🌐 So when you change the file, the application will restart automatically.
Read the --watch flag documentation.
使用 Node.js 执行任务
🌐 Run a task with Node.js
Node.js 提供了一个内置的任务运行器,允许你执行在 package.json 文件中定义的特定命令。这对于自动化重复任务特别有用,例如运行测试、构建项目或检查代码规范。
🌐 Node.js provides a built-in task runner that allows you to execute specific commands defined in your package.json file. This can be particularly useful for automating repetitive tasks such as running tests, building your project, or linting your code.
使用 --run 标志
🌐 Using the --run flag
--run 标志允许你从 package.json 文件的 scripts 部分运行指定的命令。例如,如果你有以下 package.json:
🌐 The --run flag allows you to run a specified command from the scripts section of your package.json file. For example, if you have the following package.json:
{
"type": "module",
"scripts": {
"start": "node app.js",
"dev": "node --run start -- --watch",
"test": "node --test"
}
}
你可以使用 --run 标志来运行 test 脚本:
🌐 You can run the test script using the --run flag:
node --run test
向命令传递参数
🌐 Passing arguments to the command
让我们来解释一下 package.json 文件中 scripts 对象里的 dev 键。
🌐 Let's explain the dev key in the scripts object of the package.json file.
语法 -- --another-argument 用于向命令传递参数。在这种情况下,--watch 参数被传递给 dev 脚本。
🌐 The syntax -- --another-argument is used to pass arguments to the command. In this case, the --watch argument is passed to the dev script.
node --run dev
环境变量
🌐 Environment variables
--run 标志会设置一些特定的环境变量,这些变量对你的脚本可能很有用:
🌐 The --run flag sets specific environment variables that can be useful for your scripts:
NODE_RUN_SCRIPT_NAME:正在运行的脚本名称。NODE_RUN_PACKAGE_JSON_PATH:正在处理的package.json文件的路径。
有意的限制
🌐 Intentional limitations
Node.js 任务执行器相比其他任务执行器如 npm run 或 yarn run 故意设计得更为简化。它注重性能和简洁,省略了运行 pre 或 post 脚本等功能。这使得它适合处理简单任务,但可能无法涵盖所有使用场景。
🌐 The Node.js task runner is intentionally more limited compared to other task runners like npm run or yarn run. It focuses on performance and simplicity, omitting features like running pre or post scripts. This makes it suitable for straightforward tasks but may not cover all use cases.