如何将NODE_ENV设置为OS X的生产/开发?

时间:2022-11-04 23:17:20

For use in express.js environments. Any suggestions?

用于表达。js环境。有什么建议吗?

12 个解决方案

#1


442  

Before running your app, you can do this in console,

在运行你的应用程序之前,你可以在控制台,

export NODE_ENV=production

Or if you are in windows you could try this:

或者如果你在windows里你可以试试这个:

SET NODE_ENV=production

or you can run your app like this:

或者你可以这样运行你的应用:

NODE_ENV=production node app.js

You can also set it in your js file:

您还可以将其设置为js文件:

process.env.NODE_ENV = 'production';

But I don't suggest to do it in your runtime file, since it's not easy to open up VIM in your server and change it to production. You can make a config.json file in your directory and everytime your app runs, it reads from it and sets the configuration.

但我不建议在您的运行时文件中这样做,因为在您的服务器中打开VIM并将其更改为生产是不容易的。你可以做一个配置。json文件在你的目录中,每次你的应用程序运行时,它从它读取并设置配置。

#2


69  

in package.json:

在package.json:

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

then run in terminal:

然后运行在终端:

npm start

#3


37  

export NODE_ENV=production is bad solution, it disappears after restart.

输出NODE_ENV=生产是糟糕的解决方案,重启后就会消失。

if you want not to worry about that variable anymore - add it to this file:

如果你不想再担心那个变量了,把它添加到这个文件中:

/etc/environment

don't use export syntax, just write (in new line if some content is already there):

不要使用导出语法,只需写入(如果有一些内容已经在新行中):

NODE_ENV=production

it works after restart. You will not have to re-enter export NODE_ENV=production command anymore anywhere and just use node with anything you'd like - forever, pm2...

重启后它的工作原理。您将不再需要重新输入export NODE_ENV=生产命令,只要使用您想要的任何东西——永久地,pm2…

For heroku:

heroku的:

heroku config:set NODE_ENV="production"

which is actually default.

这实际上是违约。

#4


30  

No one mentioned .env in here yet? Make a .env file in your app root, then require('dotenv').config() and read the values. Easily changed, easily read, cross platform.

没人提过,env在这里吗?在app根中创建一个.env文件,然后需要('dotenv').config()并读取这些值。容易改变,容易阅读,跨平台。

https://www.npmjs.com/package/dotenv

https://www.npmjs.com/package/dotenv

#5


21  

heroku config:set NODE_ENV="production"

#6


20  

This is a good article about NODE_ENV: http://www.hacksparrow.com/running-express-js-in-production-mode.html.

这是一篇关于NODE_ENV的好文章:http://www.hacksparrow.com/runningexpress-js-productionmode.html。

For automatically setting from Grunt you can use https://npmjs.org/package/grunt-env plugin.

对于从Grunt自动设置,您可以使用https://npmjs.org/package/grunt-env插件。

#7


5  

On OSX I'd recommend adding export NODE_ENV=development to your ~/.bash_profile and/or ~/.bashrc and/or ~/.profile.

在OSX上,我建议增加出口NODE_ENV=开发到您的~/。bash_profile和/或~ /。bashrc和/或(~ / . profile。

Personally I add that entry to my ~/.bashrc and then have the ~/.bash_profile ~/.profile import the contents of that file, so it's consistent across environments.

我个人把它添加到~/。bashrc然后是~/。bash_profile ~ /。概要文件导入该文件的内容,因此它在不同的环境中是一致的。

After making these additions, be sure to restart your terminal to pick up settings.

在做了这些添加之后,一定要重新启动你的终端以获取设置。

#8


4  

To not have to worry whether you are running your scripts on Windows, Mac or Linux install the cross-env package. Then you can use your scripts easily, like so:

不必担心您是否在Windows、Mac或Linux上运行您的脚本,安装跨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"
}

Massive props to the developers of this package.

巨大的道具给开发商的这个包裹。

#9


4  

For Windows Powershell use this command

对于Windows Powershell,可以使用此命令。

$env:NODE_ENV="production" ; node app.js

#10


2  

If you using webpack in your application, you can simply set it there, using DefinePlugin...

如果你在你的应用程序中使用webpack,你可以简单地设置它,使用DefinePlugin…

So in your plugin section, set the NODE_ENV to production:

所以在你的插件部分,将NODE_ENV设置为:

plugins: [
  new webpack.DefinePlugin({
    'process.env.NODE_ENV': '"production"',
  })
]

#11


1  

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

如果你在窗户上。首先在正确的文件夹打开你的cmd。

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

它将从你的env设置开始。

#12


0  

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

Daniel有一个奇妙的答案,这是正确部署(设置和忘记)过程的更好方法。

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

对于那些使用表达。你也可以使用grunt- expressserver,这也很棒。https://www.npmjs.org/package/grunt-express-server

#1


442  

Before running your app, you can do this in console,

在运行你的应用程序之前,你可以在控制台,

export NODE_ENV=production

Or if you are in windows you could try this:

或者如果你在windows里你可以试试这个:

SET NODE_ENV=production

or you can run your app like this:

或者你可以这样运行你的应用:

NODE_ENV=production node app.js

You can also set it in your js file:

您还可以将其设置为js文件:

process.env.NODE_ENV = 'production';

But I don't suggest to do it in your runtime file, since it's not easy to open up VIM in your server and change it to production. You can make a config.json file in your directory and everytime your app runs, it reads from it and sets the configuration.

但我不建议在您的运行时文件中这样做,因为在您的服务器中打开VIM并将其更改为生产是不容易的。你可以做一个配置。json文件在你的目录中,每次你的应用程序运行时,它从它读取并设置配置。

#2


69  

in package.json:

在package.json:

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

then run in terminal:

然后运行在终端:

npm start

#3


37  

export NODE_ENV=production is bad solution, it disappears after restart.

输出NODE_ENV=生产是糟糕的解决方案,重启后就会消失。

if you want not to worry about that variable anymore - add it to this file:

如果你不想再担心那个变量了,把它添加到这个文件中:

/etc/environment

don't use export syntax, just write (in new line if some content is already there):

不要使用导出语法,只需写入(如果有一些内容已经在新行中):

NODE_ENV=production

it works after restart. You will not have to re-enter export NODE_ENV=production command anymore anywhere and just use node with anything you'd like - forever, pm2...

重启后它的工作原理。您将不再需要重新输入export NODE_ENV=生产命令,只要使用您想要的任何东西——永久地,pm2…

For heroku:

heroku的:

heroku config:set NODE_ENV="production"

which is actually default.

这实际上是违约。

#4


30  

No one mentioned .env in here yet? Make a .env file in your app root, then require('dotenv').config() and read the values. Easily changed, easily read, cross platform.

没人提过,env在这里吗?在app根中创建一个.env文件,然后需要('dotenv').config()并读取这些值。容易改变,容易阅读,跨平台。

https://www.npmjs.com/package/dotenv

https://www.npmjs.com/package/dotenv

#5


21  

heroku config:set NODE_ENV="production"

#6


20  

This is a good article about NODE_ENV: http://www.hacksparrow.com/running-express-js-in-production-mode.html.

这是一篇关于NODE_ENV的好文章:http://www.hacksparrow.com/runningexpress-js-productionmode.html。

For automatically setting from Grunt you can use https://npmjs.org/package/grunt-env plugin.

对于从Grunt自动设置,您可以使用https://npmjs.org/package/grunt-env插件。

#7


5  

On OSX I'd recommend adding export NODE_ENV=development to your ~/.bash_profile and/or ~/.bashrc and/or ~/.profile.

在OSX上,我建议增加出口NODE_ENV=开发到您的~/。bash_profile和/或~ /。bashrc和/或(~ / . profile。

Personally I add that entry to my ~/.bashrc and then have the ~/.bash_profile ~/.profile import the contents of that file, so it's consistent across environments.

我个人把它添加到~/。bashrc然后是~/。bash_profile ~ /。概要文件导入该文件的内容,因此它在不同的环境中是一致的。

After making these additions, be sure to restart your terminal to pick up settings.

在做了这些添加之后,一定要重新启动你的终端以获取设置。

#8


4  

To not have to worry whether you are running your scripts on Windows, Mac or Linux install the cross-env package. Then you can use your scripts easily, like so:

不必担心您是否在Windows、Mac或Linux上运行您的脚本,安装跨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"
}

Massive props to the developers of this package.

巨大的道具给开发商的这个包裹。

#9


4  

For Windows Powershell use this command

对于Windows Powershell,可以使用此命令。

$env:NODE_ENV="production" ; node app.js

#10


2  

If you using webpack in your application, you can simply set it there, using DefinePlugin...

如果你在你的应用程序中使用webpack,你可以简单地设置它,使用DefinePlugin…

So in your plugin section, set the NODE_ENV to production:

所以在你的插件部分,将NODE_ENV设置为:

plugins: [
  new webpack.DefinePlugin({
    'process.env.NODE_ENV': '"production"',
  })
]

#11


1  

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

如果你在窗户上。首先在正确的文件夹打开你的cmd。

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

它将从你的env设置开始。

#12


0  

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

Daniel有一个奇妙的答案,这是正确部署(设置和忘记)过程的更好方法。

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

对于那些使用表达。你也可以使用grunt- expressserver,这也很棒。https://www.npmjs.org/package/grunt-express-server