The primary question is whether npm start
and npm test
should be setup for developer convenience and other commands like npm run heroku
and npm run travis
should be setup for services. Or should npm start
and npm test
should be kept for production and test environments respectively and customer scripts like npm run dev:start
and npm run dev:test
should be created for development.
The secondary question is... what should they be named?
Below are examples of the scripts at the various phases of the course
This version would be used at the beginning of the course, before testing has been introduced
"scripts": {
"start": "node server.js"
},
Introduce this setup during the during express but before mocha
"scripts": {
"start": "node server.js",
"dev:start": "nodemon server.js"
}
Introduce this setup for mocha testing
- Keep
npm start
andnpm test
reserved for Heroku and Travis. Createnpm run dev:start
andnpm run dev:test
scripts for development.
"scripts": {
"start": "node server.js",
"test": "cross-env NODE_ENV=test mocha",
"dev:start": "nodemon server.js",
"dev:test": "nodemon --exec npm test"
},
The proposed solutions use process.env.NODE_ENV
to determine the correct database connection and suppress logging. It is considered bad practice to set process.env.NODE_ENV
in code or use globals like so we'll set it in the command using cross-env
so it works cross platform. This also assumes the use of a .env
file and the dotenv
package for database credentials and jwt secrets.
Side Note: Never use
mocha --watch
. It may run faster but it causes a the app to create multiple processes and database connections. IMHO, the overhead of adding graceful shutdown scripts for--watch
(which don't work for SIGINT or SIGTERM) is not worth the effort.
Propose: Introduce test coverage using nyc
formerly known as istanbul
as an advanced/optional lesson.
"scripts": {
"start": "node server.js",
"test": "cross-env NODE_ENV=test mocha",
"dev:start": "nodemon server.js",
"dev:test": "nodemon --exec npm test",
"dev:cover": "nodemon --exec nyc --reporter=lcov --reporter=text-summary npm test"
},
I like the idea of bringing
dotenv
into the curriculum, I think it's essential. The last startup I worked at, I remember them having a security incident and it turned out their credentials were in their repo (this at a company of 200 people).Regarding the start script, I owned a hosting company for ten years so I favor anything we can do to make deployment more standard and redundant. To me that means
"start": "node server.js"
which works by default, and I like the idea of introducing a Procfile but I'd say let's do it for the sake of introducingforever
as an optional way of improving availability, and leave"start": "node server.js"
in place as a fallback.I also like
npm run dev
because to me"dev": "nodemon server.js"
makes it super obvious how to start the server in development mode. I've only seen semi-colons used in script names when those scripts are being called by other scripts, but maybe I just haven't seen enough projects and need to get used to the idea.Personally, I wouldn't want my tests to run every time I save a file, I don't need that kind of anxiety! Plus, I think a good linting setup alleviates a lot of the need for that. So, I can do without a
dev:test
script and would just want a script like"cover": "nyc --reporter=lcov --reporter=text-summary npm test"
for test coverage. However I get that's a personal preference and it's cool that nodemon can be used for more than just restarting Node.