首页 > 解决方案 > 运行 { PORT=4001 npm run test} 此命令时出现节点 JS 错误

问题描述

var app= express();
const port=process.env.PORT || 4000;

app.listen(port,function(){
    console.log('Listen on port '+port);
});

当我运行这个命令 $ PORT=4001 npm run test

我看到这个错误

PORT=4001 : The term 'PORT=4001' is not recognized as the name of a cmdlet, function, script file, or operable
program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ PORT=4001 npm run test
+ ~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (PORT=4001:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

标签: javascriptnode.jsport

解决方案


您似乎在 PowerShell 下运行它,但使用的是使用 Unix/Linux shell 的某人的示例。使其成为两个命令

$Env:PORT = "4001"
npm run test

推荐阅读