1

I feel like asking a silly question but I have googled it for a while and can't find a satisfying answer. SO doesn't such discussion either, only How to set NODE_ENV to production/development in OS X and How can I set NODE_ENV=production on Windows?

To run a command after another in one line we normally join them by ; ( or &&)

So I had assumed I should run command like this PORT=3000 ; node server.js or export PORT=3000; node server.js , just like PORT=3000 ; echo $PORT

But we just put a space between PORT=3000 node server.js (without ; or &&) and PORT is read into process.env.PORT. How does shell make nodejs get environment variables? It will be better if someone can show nodejs codes.

----- update ------

The reason I was puzzled with this shell syntax (according to my limited knowledge) is that I think the general format for a Unix command line is

command options(s) filename(s)

The space in between is used to separate command from option and filename. So how can it be used to separate 2 commands as well?

Qiulang
  • 10,295
  • 11
  • 80
  • 129
  • 1
    Why do you think that it is only available in node?? Do `PORT=3000 echo Qiulang` – bugwheels94 Feb 12 '18 at 03:16
  • I assume you were trying to help but my understanding is that putting a space between two commands just does not work. And that is my question. – Qiulang Feb 12 '18 at 04:37
  • I am saying it is not Nodejs specific so u won't get any Nodejs code. Not sure, but it is part of shell syntax – bugwheels94 Feb 12 '18 at 04:44
  • @Qiulang dude, i think it *is* valid syntax, you can try with `PORT=1000 echo 'hello' && echo 'world'`, if first half failed everything after && won't get executed. – Allen Feb 12 '18 at 04:46
  • @Xlee I will suggest to you try PORT=1000 echo $PORT and you can see the result is empty. i.e. PORT is not set – Qiulang Feb 12 '18 at 04:48
  • @Qiulang wants to leave as comment but not easy to format, see below – Allen Feb 12 '18 at 04:59
  • @bugwheels94 see my updated question. – Qiulang Feb 12 '18 at 05:03
  • can you drop a reference for your so said "general format for a unix command", i am not sure if `pwd echo type` etc fits this format. – Allen Feb 12 '18 at 05:06
  • @Xlee oh sorry that is just my understanding, no reference. – Qiulang Feb 12 '18 at 05:08
  • i guess you might be interested in this: http://tldp.org/LDP/abs/html/subshells.html – Allen Feb 12 '18 at 05:08
  • Thanks for the link but did you see the words "A command list embedded between parentheses runs as a subshell." That is exactly what I was trying to say. You got to have some way to separate 2 commands – Qiulang Feb 12 '18 at 05:10
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/164946/discussion-between-xlee-and-qiulang). – Allen Feb 12 '18 at 05:11

1 Answers1

1

I checked nodejs document and found the answer https://nodejs.org/api/process.html#process_process_env

So it is environ, not 2 separated commands,

Bourne-style shells support the syntax

       NAME=value command

to create an environment variable definition only in the scope of the process that executes command.

Or as bash manual 3.7.4 explained.

For python, it is os.environ['NAME']

This is also the reason why it won't work on windows: How can I set NODE_ENV=production on Windows?

Notes for Windows users, as a comment there by daw "set NODE_ENV=production && " adds a trailing space to the variable and don't use single/double quote.

I feel quite embarrassed that I almost wanted to delete my question. I keep it here in case someone else may also have this doubt. Or someone else can further explain environ or how node read it into process.env object.

Qiulang
  • 10,295
  • 11
  • 80
  • 129