设置环境变量。json在Windows 10

时间:2021-05-19 23:13:53

UPDATE: As it is explained in the question, this is not a duplicate because I have already tried adding the set keyword before the environment variable and that did not solve the problem.

更新:正如问题中解释的,这不是重复的,因为我已经尝试在环境变量之前添加set关键字,这并没有解决问题。


I am in the process of learning node and typing examples from a book. The first examples deal with showing how the "http" module works and how to create a server to listen to requests. At some point the book asks to add the following line to the scripts section of the package.json file:

我正在学习node,并从一本书中输入示例。第一个示例展示了“http”模块是如何工作的,以及如何创建一个服务器来侦听请求。在某些情况下,该书要求将以下行添加到包的脚本部分。json文件:

"server": "SERVERPORT=3002 node ./fiboserver"

“服务器”:“SERVERPORT = 3002节点/ fiboserver”。

When I try to run the example with npm run server I get the following error message:

当我尝试使用npm run server运行示例时,我得到以下错误消息:

'SERVERPORT' is not recognized as an internal or external command

“SERVERPORT”不被识别为内部或外部命令

I haven't been able to find any answer on the internet, at most I found that I could try:

我在网上找不到任何答案,最多也只能尝试:

"server": "set SERVERPORT=3002 node ./fiboserver"

"server": "set SERVERPORT=3002 node ./fiboserver"

But that doesn't help either, the only difference is that instead of the error message I get the command prompt again so apparently the server is never run.

但这也没有帮助,唯一的区别是,我没有得到错误消息,而是再次得到命令提示符,因此显然服务器永远不会运行。

I believe the author used a Linux machine, I am using a Windows 10 laptop.

我相信作者使用的是一台Linux机器,我使用的是Windows 10笔记本电脑。

I am really committed to learn Node and my line of work is on Windows environments. I believe that setting environment variables on package.json is important so I could really use some help in figuring this out.

我真的致力于学习Node,我的工作是在Windows环境中。我认为在包上设置环境变量。json很重要,所以我需要一些帮助。

Thank you.

谢谢你!

2 个解决方案

#1


2  

On Windows you have to separate the command of setting a variable from the one which runs the server with the && operator. That being said, you have to do something like this:

在Windows上,您必须将设置变量的命令与使用&&操作符运行服务器的命令分开。也就是说,你必须这样做:

"server": "set SERVERPORT=3002 && node ./fiboserver"

"server": "set SERVERPORT=3002 && node ./fiboserver"

#2


2  

Make it cross-platform by using cross-env:

通过使用cross-env实现跨平台:

"server": "cross-env SERVERPORT=3002 node ./fiboserver"

"server": "cross-env SERVERPORT=3002节点。/fiboserver"

#1


2  

On Windows you have to separate the command of setting a variable from the one which runs the server with the && operator. That being said, you have to do something like this:

在Windows上,您必须将设置变量的命令与使用&&操作符运行服务器的命令分开。也就是说,你必须这样做:

"server": "set SERVERPORT=3002 && node ./fiboserver"

"server": "set SERVERPORT=3002 && node ./fiboserver"

#2


2  

Make it cross-platform by using cross-env:

通过使用cross-env实现跨平台:

"server": "cross-env SERVERPORT=3002 node ./fiboserver"

"server": "cross-env SERVERPORT=3002节点。/fiboserver"