balderdashy/sails · error
E_INVALID_NODE_ENV
E_INVALID_NODE_ENV
Error message
When the Sails environment is set to "production", NODE_ENV must also be set to "production" (but it was set to "' + process.env.NODE_ENV + '" instead).
What it means
When Sails' own environment is set to "production", the NODE_ENV process variable must match. If NODE_ENV is unset Sails fixes it automatically, but if it is set to anything other than "production" the load fails with code E_INVALID_NODE_ENV (verifyEnvironment in lib/app/load.js).
Source
Thrown at lib/app/load.js:248
function verifyEnvironment() {
// At this point, the Sails environment is set to its final value,
// whether it came from the command line or a config file. So we
// can now compare it to the NODE_ENV environment variable and
// act accordingly. This may involve changing NODE_ENV to "production",
// which we want to do as early as possible since dependencies might
// be relying on that value.
// If the Sails environment is production, but NODE_ENV is undefined,
// log a warning and change NODE_ENV to "production".
if (sails.config.environment === 'production' && process.env.NODE_ENV !== 'production' ) {
if (_.isUndefined(process.env.NODE_ENV)) {
sails.log.debug('Detected Sails environment is "production", but NODE_ENV is `undefined`.');
sails.log.debug('Automatically setting the NODE_ENV environment variable to "production".');
sails.log.debug();
process.env.NODE_ENV = 'production';
} else {
throw flaverr({ name: 'userError', code: 'E_INVALID_NODE_ENV' }, new Error('When the Sails environment is set to "production", NODE_ENV must also be set to "production" (but it was set to "' + process.env.NODE_ENV + '" instead).'));
}
}
}
/**
* Returns function which is fired when Sails is ready to go
*
* @api private
*/
function ready__(cb) {
return function(err) {
if (err) {
return cb && cb(err);
}
sails.log.silly('The router & all hooks were loaded successfully.');
// If userconfig hook is turned off, still load globals.View on GitHub (pinned to 7b76422cc2)
Solutions
- Run the app with NODE_ENV=production when the Sails environment is production (e.g. `NODE_ENV=production node app.js`).
- Unset the conflicting NODE_ENV so Sails can auto-set it to production.
- Set both consistently in your deployment platform's env settings or start script.
- Stop hardcoding environment in config files; derive both from NODE_ENV.
Example fix
// before (package.json) "start": "node app.js" // NODE_ENV=development from host // after "start": "NODE_ENV=production node app.js"
Defensive patterns
Strategy: validation
Validate before calling
if (process.env.sails_environment === 'production' || config.environment === 'production') { if (!process.env.NODE_ENV) { process.env.NODE_ENV = 'production'; } else if (process.env.NODE_ENV !== 'production') { throw new Error('NODE_ENV must be production'); } } Try / catch
try { await sails.lift(); } catch (e) { if (e.code === 'E_INVALID_NODE_ENV') { console.error('Set NODE_ENV=production before lifting'); process.exit(1); } throw e; } Prevention
- Always set NODE_ENV explicitly in production deployments.
- Don't hardcode environment in config/local.js.
- Keep Sails environment and NODE_ENV driven from one source of truth (NODE_ENV).
- Add a deploy-time env check in CI/CD before lift.
When it happens
Trigger: Starting sails with `sails_environment=production` (config or env) while NODE_ENV is e.g. "development", "staging", or any non-production string.
Common situations: Hardcoding environment: 'production' in config/local.js while running with NODE_ENV=development locally; CI/production containers where NODE_ENV defaults to development; deploying to hosts that set NODE_ENV themselves.
Related errors
- Detected Sails environment of `production`, but Node environ
- rc(name): name *must* be string
- `--dev` option is deprecated: Please do not use it.
- Cannot call res.render() - `req._sails.renderView` was not a
- The 2-ary usage of `res.redirect()` is no longer supported i
AI-assisted analysis of balderdashy/sails@7b76422cc2 (2026-09-01).
Data as JSON: /api/errors/08699d01056f9329.
Report an issue: GitHub.