Nuxt.js: process.env.NODE_ENV getting set to development no matter what.

Created on 23 May 2017  路  4Comments  路  Source: nuxt/nuxt.js

echo $NODE_ENV gives me production

Package.json on OSX:

"scripts": {
  "dev": "backpack dev",
  "build": "NODE_ENV=production nuxt build && backpack build",
  "start": "node build/main.js",
  "generate": "nuxt generate",
  "lint": "eslint --ext .js,.vue --ignore-path .gitignore .",
  "precommit": "npm run lint"
}

npm run build runs with process.env.NODE_ENV set to development.
npm run start runs with process.env.NODE_ENV set to development and rebuilds every time.

I have verified both of the above with console.log statements.

I am using Nuxt with Express.

I do not understand where my environment is getting set to development. I have nothing in my nuxt.config.js that sets this environment variable.

Any help would be appreciated.

This question is available on Nuxt.js community (#c652)
help-wanted

Most helpful comment

Okay, the reason why backpack build did not run is because of this line in my nuxt.config.js:

build: {
    analyze: true
},

All 4 comments

This is my /server/index.js file

import Nuxt from 'nuxt';
import express from 'express';
import subdomain from 'express-subdomain';
import bodyParser from 'body-parser';
import cors from 'cors';
import https from 'https';
import fs from 'fs';

import api from './api';

const app = express();
const host = process.env.HOST || '0.0.0.0';
const port = process.env.PORT || 3000;

console.log(process.env.NODE_ENV);  //ALWAYS DEVELOPMENT
if (process.env.NODE_ENV === 'development') {
  const sslOptions = {
    key: fs.readFileSync('./ssl/ssl.key'),
    cert: fs.readFileSync('./ssl/ssl.crt'),
    requestCert: false,
    rejectUnauthorized: false
  };

  console.log(process.env.NODE_ENV);  //ALWAYS DEVELOPMENT

  https.createServer(sslOptions, app).listen(8443, () => {
    console.log('Development SSL server started on port 8443!');
  });
}

app.set('port', port);

app.use((req, res, next) => {
  const str = 'www.';
  if (req.headers.host.indexOf(str) === 0) {
    res.redirect(301, 'https://' + req.host.slice(str.length) + req.originalUrl);
  } else {
    next();
  }
});

const whitelist = [
  'http://127.0.0.1:3000'
];

const corsOptions = {
  origin: (origin, callback) => {
    let originIsWhitelisted = whitelist.indexOf(origin) !== -1;
    callback(null, originIsWhitelisted);
  },
  credentials: true
};

app.use(cors(corsOptions));

// Allow a payload to be sent and parsed with a POST request
app.use(bodyParser.json());

// Import API Routes
app.use(subdomain('api', api));

// Import and Set Nuxt.js options
let config = require('../nuxt.config.js');
config.dev = !(process.env.NODE_ENV === 'production');
console.log(process.env.NODE_ENV);  //ALWAYS DEVELOPMENT

// Init Nuxt.js
const nuxt = new Nuxt(config);
app.use(nuxt.render);

// Build only in dev mode
if (config.dev) {
  nuxt.build()
  .catch((error) => {
    console.error(error); // eslint-disable-line no-console
    process.exit(1);
  });
}

// Listen the server
app.listen(port, host);
console.log('Server listening on ' + host + ':' + port); // eslint-disable-line no-console

I think I found the issue.

nuxt build && backpack build

After nuxt build runs, backpack build does not. Is this a bug? Why would backpack build not run?

Because backpack build does not run there are two old files in the build directory:

build/main.js
build/main.map

These old files were created by npm run dev, which of course set process.env.NODE_ENV to development

There are no build errors that would suggest that backpack build did not run.

Okay, the reason why backpack build did not run is because of this line in my nuxt.config.js:

build: {
    analyze: true
},

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

lazycrazy picture lazycrazy  路  3Comments

pehbehbeh picture pehbehbeh  路  3Comments

vadimsg picture vadimsg  路  3Comments

shyamchandranmec picture shyamchandranmec  路  3Comments

vadimsg picture vadimsg  路  3Comments