I am using npm grunt env and load-grunt-config modules in my project. grunt env processes the environment variables for you, and load-grunt-config processes, well, loads the grunt configuration for you. You can put your tasks in other files, then load-grunt-config will link them and load grunt and use them for you. You can also create the aliases.js file, with tasks that you want to combine into one task, working one after another. It is similar to the grunt.registerTask task in the original Gruntfile.js . I put all my grunt tasks in a separate grunt/ folder in the root folder with the main Gruntfile , without additional subfolders, as suggested by load-grunt-config README.md on Github. Here is my slimmed-down Gruntfile :
module.exports = function(grunt) { 'use strict'; require('time-grunt')(grunt); // function & property declarations grunt.initConfig({ pkg: grunt.file.readJSON('package.json') }); require('load-grunt-config')(grunt, { init: true, loadGruntConfig: { scope: 'devDependencies', pattern: ['grunt-*', 'time-grunt'] } }); };
Theoretically, setting all of these files to the correct path for loading load-grunt-config should be exactly the same as with Gruntfile.js . However, it seems I'm a little confused. It seems that the environment variables set in the env task are not set for subsequent grunt tasks, but set by time, the node processes its tasks, in this case the express server.
Task
grunt env :
module.exports = { // environment variable values for developers // creating/maintaining site dev: { options: { add: { NODE_ENV: 'dev', MONGO_PORT: 27017, SERVER_PORT: 3000 } } } };
grunt-shell-spawn task:
// shell command tasks module.exports = { // starts up MongoDB server/daemon mongod: { command: 'mongod --bind_ip konneka.org --port ' + (process.env.MONGO_PORT || 27017) + ' --dbpath C:/MongoDB/data/db --ipv6', options: { async: true, // makes this command asynchronous stdout: false, // does not print to the console stderr: true, // prints errors to the console failOnError: true, // fails this task when it encounters errors execOptions: { cwd: '.' } } } };
grunt express task:
module.exports = { // default options options: { hostname: '127.0.0.1', // allow connections from localhost port: (process.env.SERVER_PORT || 3000), // default port }, prod: { options: { livereload: true, // automatically reload server when express pages change // serverreload: true, // run forever-running server (do not close when finished) server: path.resolve(__dirname, '../backend/page.js'), // express server file bases: 'dist/' // watch files in app folder for changes } } };
aliases.js file ( grunt-load-config way to combine tasks so that they run one after another):
module.exports = { // starts forever-running server with "production" environment server: ['env:prod', 'shell:mongod', 'express:prod', 'express-keepalive'] };
part of backend/env/prod.js (Express configuration for the environment loaded if NODE_ENV set to "prod" is modeled after MEAN.JS ):
'use strict'; module.exports = { port: process.env.SERVER_PORT || 3001, dbUrl: process.env.MONGOHQ_URL || process.env.MONGOLAB_URI || 'mongodb://konneka.org:' + (process.env.MONGO_PORT || 27018) + '/mean' };
part of backend/env/dev.js (express environment configuration for the dev environment, loaded if the `NODE_ENV variable is not set or is set to" dev "):
module.exports = { port: process.env.SERVER_PORT || 3000, dbUrl: 'mongodb://konneka.org:' + (process.env.MONGO_PORT || 27017) + '/mean-dev' };
backend/page.js (my Express configuration page, also modeled after MEAN.JS):
'use strict'; var session = require('express-session'); var mongoStore = require('connect-mongo')(session); var express = require('express'); var server = express(); ...
When I start grunt server , I get:
$ cd /c/repos/konneka/ && grunt server Running "env:prod" (env) task Running "shell:mongod" (shell) task Running "express:prod" (express) task Running "express-server:prod" (express-server) task Web server started on port:3000, hostname: 127.0.0.1 [pid: 3996] Running "express-keepalive" task Fatal error: failed to connect to [konneka.org:27018] Execution Time (2014-08-15 18:05:31 UTC) loading tasks 38.3s βββββββββββββββββββββββββββββββββ 79% express-server:prod 8.7s ββββββββ 18% express-keepalive 1.2s ββ 2% Total 48.3s
Now I canβt get the database to connect in the first place, but so far Iβm not paying attention. Note that the server is running on port 3000, which means that during the execution of the grunt express:prod SERVER_PORT not installed, so the port gets the value 3000. There are many other similar examples in which the environment variable is not set, so my application uses the value default. However, note that session trying to connect to the database on port 27018 (and does not work), so MONGO_PORT is ultimately installed.
If I just tried the grunt server task, I could write it to load-grunt-config , running the tasks in parallel, and not one after another or some other error, but even when I try to execute tasks one at a time, one, for example, running grunt env:prod shell:mongod express-server:prod express-keepalive , I get similar (incorrect) results, so either grunt or grunt env also runs tasks in parallel or something else happens.
What's going on here? Why are environment variables not set correctly for subsequent grunt tasks? When are they eventually installed, and why then, and not at another time? How can I get them to install grunt for the tasks themselves, and not after, assuming there is even a way?