如何在OS X中将NODE_ENV设置为生产/开发

JavaScript Node.js

古一蓝染大人别太浪

2020-03-18

用于express.js环境。有什么建议么?

第2139篇《如何在OS X中将NODE_ENV设置为生产/开发》来自Winter(https://github.com/aiyld/aiyld.github.io)的站点

9个回答
宝儿村村 2020.03.18

In order to have multiple environments you need all of the answers before (NODE_ENV parameter and export it), but I use a very simple approach without the need of installing anything. In your package.json just put a script for each env you need, like this:

...
"scripts": {
    "start-dev": "export NODE_ENV=dev && ts-node-dev --respawn --transpileOnly ./src/app.ts",
    "start-prod": "export NODE_ENV=prod && ts-node-dev --respawn --transpileOnly ./src/app.ts"
  }
 ...

Then, to start the app instead of using npm start use npm run script-prod.

In the code you can access the current environment with process.env.NODE_ENV.

Voila.

神奇前端 2020.03.18

Windows CMD -> set NODE_ENV=production

Windows Powershell -> $env:NODE_ENV="production"

MAC -> export NODE_ENV=production

A西里 2020.03.18

If you are on windows. Open your cmd at right folder then first

set node_env={your env name here}

hit enter then you can start your node with

node app.js

it will start with your env setting

Stafan村村达蒙 2020.03.18

Daniel has a fantastic answer which is the better approach for the correct deployment (set and forget) process.

For those using express. You can use grunt-express-server which is fantastic as well. https://www.npmjs.org/package/grunt-express-server

蛋蛋L卡卡西 2020.03.18

对于Windows Powershell,请使用此命令

$env:NODE_ENV="production" ; node app.js
达蒙A宝儿 2020.03.18

不必担心您是在Windows,Mac还是Linux上运行脚本,都可以安装cross-env软件包。然后,您可以轻松使用脚本,如下所示:

"scripts": {
    "start-dev": "cross-env NODE_ENV=development nodemon --exec babel-node -- src/index.js",
    "start-prod": "cross-env NODE_ENV=production nodemon --exec babel-node -- src/index.js"
}

给该软件包开发人员的大量道具。

卡卡西卡卡西 2020.03.18

export NODE_ENV=production 是错误的解决方案,重启后消失。

如果您不想再担心该变量,请将其添加到此文件中:

/etc/environment

不要使用导出语法,只需写(如果已有一些内容,则换行):

NODE_ENV=production

重新启动后可以工作。您将不再需要在任何地方重新输入export NODE_ENV = production命令,而只需将node与任何您想使用的东西一起使用-永远,pm2 ...

对于heroku:

heroku config:set NODE_ENV="production"

这实际上是默认设置。

前端神无 2020.03.18
heroku config:set NODE_ENV="production"
米亚村村 2020.03.18

在package.json中:

{
  ...
  "scripts": {
    "start": "NODE_ENV=production node ./app"
  }
  ...
}

然后在终端中运行:

npm start

问题类别

JavaScript Ckeditor Python Webpack TypeScript Vue.js React.js ExpressJS KoaJS CSS Node.js HTML Django 单元测试 PHP Asp.net jQuery Bootstrap IOS Android