Home  >  Article  >  Web Front-end  >  How to solve the error when setting NODE_ENV in nodejs?

How to solve the error when setting NODE_ENV in nodejs?

青灯夜游
青灯夜游forward
2021-05-17 10:47:571865browse

This article will introduce to you the solution to the nodejs setting NODE_ENV error in the windows environment. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.

How to solve the error when setting NODE_ENV in nodejs?

[Recommended study: "nodejs Tutorial"]

When I was looking at the node document today, I saw "Node.js Chapter "The Difference between Development Environment and Production Environment", I encountered some pitfalls when setting up NODE_ENV. I searched the Internet but couldn't find the answer I wanted, so I posted this post to help everyone avoid the pitfalls. The code shown in the document is as follows:

// app.js 文件中的代码
console.log('NODE_ENV的值为:'+process.env.NODE_ENV)
// 终端命令行
NODE_ENV=production node app.js

Run it...

How to solve the error when setting NODE_ENV in nodejs?

##emm, what's going on? Is the official document written wrong? I quickly went online to check what was going on. After some inquiries, I found that the command lines for setting running variables are different for different operating systems. It should be like this under Windows:

set NODE_ENV=production &&  node app.js
Okay, change it. Let’s do it again... At this point, some people may have successfully run it, while others may encounter the following error.

How to solve the error when setting NODE_ENV in nodejs?

What is going on? The reason is that the terminal used is powershell, which does not support the "&&" symbol. The solution is also very simple. Change powershell to Just turn it into cmd. The switching method is as shown in the figure below:

How to solve the error when setting NODE_ENV in nodejs?

How to solve the error when setting NODE_ENV in nodejs?

The final successful operation is like this:

How to solve the error when setting NODE_ENV in nodejs?

Thanks for browsing!

For more programming related knowledge, please visit:

Programming Video! !

The above is the detailed content of How to solve the error when setting NODE_ENV in nodejs?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:juejin.cn. If there is any infringement, please contact admin@php.cn delete