bash 'DEBUG=myapp:* npm start' 实际在做什么的解释

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36240385/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 14:25:12  来源:igfitidea点击:

Explanation for what 'DEBUG=myapp:* npm start' is actually doing

node.jsbashexpresscommand-line

提问by Pak

The getting started page for the Express application generator (located here) says to start the server by using $ DEBUG=myapp:* npm startif using MacOS or Linux.

Express 应用程序生成器的入门页面(位于此处)说,$ DEBUG=myapp:* npm start如果使用 MacOS 或 Linux,则通过使用来启动服务器。

This works fine, but I'm having trouble understanding what this line is actually doing. I would have guessed that it would be assigning something to the variable DEBUG, but after exiting the server I ran echo $DEBUGand it printed nothing. I'm guessing the :is somehow key here, but it's not clear to me how/why.

这工作正常,但我无法理解这条线实际上在做什么。我会猜到它会为变量分配一些东西DEBUG,但是在退出服务器后我跑了echo $DEBUG它并没有打印任何东西。我猜这:在某种程度上是关键,但我不清楚如何/为什么。

Hopefully someone can break this down for me.

希望有人能帮我解决这个问题。

采纳答案by Pak

It turns out I was thinking that this was far more complicated than it actually is. Chepner's answer got some gears unstuck in my head.

事实证明,我认为这比实际情况复杂得多。Chepner 的回答让我脑子里有些混乱。

I created a python script that prints the DEBUGvariable and called it instead of npm start

我创建了一个 python 脚本来打印DEBUG变量并调用它而不是npm start

$ DEBUG=myapp:* python printvar.py

$ DEBUG=myapp:* python printvar.py

Sure enough, this printed myapp:*. It seems the big piece that I was missing was that the space after the variable assignment is used as a separator between the commands and the :*is nothing more than text that's part of the variable that gets assigned to DEBUG.

果然,这个打印出来了myapp:*。似乎我遗漏的重要部分是变量赋值后的空格用作命令之间的分隔符,而 the:*只不过是分配给DEBUG.

回答by Andrew Lam

  • DEBUG=myapp:* npm startconsist of two parts.

  • The first part is DEBUG=myapp:*and the second part is npm start

  • You may run DEBUG=myapp:*first in your command-line tool, and then followed by running npm start.

  • DEBUG=myapp:*means you are telling nodejs that you want to turn on logging for debugging purposes.

    • Remember to replace myappwith your app name. You can find your app name in package.json file under "name"property. enter image description here
    • The *in myapp:*means to see all the internal logs used in Express
    • If you only want to see the logs from the router implementation, then set the value of DEBUG to myapp:router. Likewise, to see logs only from the application implementation set the value of DEBUG to myapp:application, and so on.
  • npm startis telling npm to run your scriptsstated in the package.json file and the script name is called startenter image description here

  • Source: https://expressjs.com/en/guide/debugging.html

  • DEBUG=myapp:* npm start由两部分组成。

  • 第一部分是DEBUG=myapp:*,第二部分是npm start

  • 您可以DEBUG=myapp:*先在命令行工具中运行,然后再运行npm start.

  • DEBUG=myapp:*意味着您告诉 nodejs 您要打开日志记录以进行调试。

    • 请记住替换myapp为您的应用程序名称。您可以在"name"属性下的 package.json 文件中找到您的应用程序名称。 在此处输入图片说明
    • *myapp:*手段看到快递使用的所有内部日志
    • 如果您只想查看路由器实现的日志,请将 DEBUG 的值设置为myapp:router。同样,要仅查看来自应用程序实现的日志,请将 DEBUG 的值设置为myapp:application,依此类推。
  • npm start告诉 npm 运行您scripts在 package.json 文件中声明的内容,并调用脚本名称start在此处输入图片说明

  • 来源:https://expressjs.com/en/guide/debugging.html

回答by Eric Tamminga

DEBUG is an environment variable set to all debuggers in the myapp app via the wildcard character * for the duration of the session. The DEBUG variable is used by the "tiny node.js & browser debugging utility for your libraries and applications", for which documentation can be found here.

DEBUG 是一个环境变量,在会话期间通过通配符 * 设置为 myapp 应用程序中的所有调试器。DEBUG 变量由“用于库和应用程序的微小 node.js 和浏览器调试实用程序”使用,相关文档可在此处找到。

回答by chepner

This is a temporary assignment; npmsees the assigned value in its environment, but it does not affect the current shell. DEBUGretains whatever value it had (or remains unset) after npmexits.

这是一个临时任务;npm在其环境中看到指定的值,但不影响当前 shell。退出DEBUG后保留它所拥有的任何值(或保持未设置)npm

回答by andlrc

DEBUGis set as an environment variable for npmand sub processes but not set in your shell, consider this:

DEBUG被设置为npm和子进程的环境变量,但未在您的 shell 中设置,请考虑:

HELLO=World bash -c 'echo $HELLO' # World
HELLO=World bash -c "bash -c 'echo $HELLO'" # World
echo $HELLO # Nothing, assuming that HELLO was null before running the above snippets

In your case the value myapp:*gets assigned to the variable DEBUG. And this variable will be available inside npm, for what reason it's needed I cannot answer.

在您的情况下,值myapp:*被分配给变量DEBUG。而这个变量将在内部可用npm,出于什么原因需要它,我无法回答。

回答by DaveS_550

The : is just text that gets assigned to the env variable.

: 只是分配给 env 变量的文本。

What the line is doing and how it works is as follows:

该生产线正在做什么以及它是如何工作的如下:

First I'll discuss express internal level debug messages. The following env setting all display the same internal log level

首先,我将讨论表达内部级别的调试消息。以下环境设置都显示相同的内部日志级别

DEBUG=express:*, npm start
DEBUG=express.*, npm start
DEBUG=express*, npm start

And the following setting displays the internal log level messages for just the internal router

以下设置仅显示内部路由器的内部日志级别消息

DEBUG=express:router, npm start
DEBUG=express.router, npm start

Now in order to see "application" level DEBUG statements. Those that you've added to your code with the debug function.

现在以查看“应用程序”级别的 DEBUG 语句。您使用调试功能添加到代码中的那些。

It has to do with your module dependencies. The value(s) passed into the debug package. The values can be any arbitrary name - that is they don't need to match package.json, file, object variable name, etc. However the env variable DEBUG value needs to correspond to the value passed into the debug package in your code.

它与您的模块依赖项有关。传递到调试包的值。这些值可以是任意名称 - 即它们不需要匹配 package.json、文件、对象变量名称等。但是 env 变量 DEBUG 值需要对应于传递到代码中调试包的值。

This line of code

这行代码

var debug = require('debug') ('myapp:myapp');

Consider these two files

考虑这两个文件

www
…
var debug = require('debug')('myapp:www');

app.js
…
var debug = require('debug')('myapp:app');

Then if we run the following scenarios:

那么如果我们运行以下场景:

  1. DEBUG=myapp:* npm start
  2. DEBUG=myapp:www npm start
  3. DEBUG=myapp:app npm start
  1. DEBUG=myapp:* npm start
  2. DEBUG=myapp:www npm start
  3. DEBUG=myapp:app npm start

We get the following:

我们得到以下信息:

screen shot

截屏

vagrant@n1:~/despobaldo/server$ DEBUG=myapp:* npm start

> [email protected] start /home/vagrant/despobaldo/server
> node ./bin/www

  myapp:app here I am +0ms
  myapp:www Listening on port 3000 +8ms
^Cvagrant@n1:~/despobaldo/server$
vagrant@n1:~/despobaldo/server$
vagrant@n1:~/despobaldo/server$ DEBUG=myapp:www npm start

> [email protected] start /home/vagrant/despobaldo/server
> node ./bin/www

  myapp:www Listening on port 3000 +0ms
^Cvagrant@n1:~/despobaldo/server$
vagrant@n1:~/despobaldo/server$
vagrant@n1:~/despobaldo/server$ DEBUG=myapp:app npm start

> [email protected] start /home/vagrant/despobaldo/server
> node ./bin/www

  myapp:app here I am +0ms
^Cvagrant@n1:~/despobaldo/server$
vagrant@n1:~/despobaldo/server$
vagrant@n1:~/despobaldo/server$

In order to see both internal and "application" level logging you would doing something like this:

为了同时查看内部​​和“应用程序”级别的日志记录,您可以执行以下操作:

DEBUG=myapp:*,express:router npm start

DEBUG=myapp:*,express:router npm start

screen shot

截屏

vagrant@n1:~/despobaldo/server$
vagrant@n1:~/despobaldo/server$ DEBUG=myapp:*,express:router npm start

> [email protected] start /home/vagrant/despobaldo/server
> node ./bin/www

  express:router use '/' query +0ms
  express:router use '/' expressInit +4ms
  express:router use '/' logger +0ms
  express:router use '/' jsonParser +7ms
  express:router use '/' urlencodedParser +3ms
  express:router use '/' cookieParser +0ms
  express:router use '/' serveStatic +1ms
  myapp:app here I am +0ms
  express:router use '/' router +1ms
  express:router use '/users' router +0ms
  express:router use '/' <anonymous> +0ms
  express:router use '/' <anonymous> +0ms
  myapp:www Listening on port 3000 +7ms
^Cvagrant@n1:~/despobaldo/server$