Setting node env variable in gitlab runner - node.js

Im trying to use gitlab runner to test and build my node server but I've run into a small issue when trying to automate tests. In my package.json I have scripts
"scripts": {
"start": "node app.js",
"test-init": "node ./node_modules/jasmine/bin/jasmine.js init",
"test": "set NODE_ENV=Dev&& node ./node_modules/jasmine/bin/jasmine.js"
},
So NODE_ENV=Dev will load a different settings file. One that uses the mongodb url "mongodb://mongo/DBName" and when I run npm test on localhost the server crashes(as its supposed to) because it cant connect to mongo using the Dev setttings file. But when I run the project in GitLab on a runner it wont connect to the db as it uses the non-dev settings file which has a url. Is there any reason in the GitLab-ci why the NODE_ENV is not being set?
Below is my GitLab-ci.yml
image: node:latest
stages:
- build
- test
cache:
paths:
- node_modules/
services:
- mongo
install_dependencies:
stage: build
script:
- npm install
artifacts:
paths:
- node_modules/
test_with_lab:
stage: test
script:
- npm run test-init
- npm test

This is because the docker images run on gitlab are linux based and therefore the set command won't work.
There are two solutions.
Solution 1
Use cross-env npm module as documented here by
doing the following:
Install cross-env like so:
npm install --save-dev cross-env
Then edit your package.json to this:
"scripts": {
"start": "node app.js",
"test-init": "node ./node_modules/jasmine/bin/jasmine.js init",
"test": "cross-env NODE_ENV=Dev node ./node_modules/jasmine/bin/jasmine.js"
},
Solution 2
Just modify the script for linux instead, much quicker and simpler. Here is how it should look. Note npm run test won't work on windows anymore. To avoid this use the first solution above.
"scripts": {
"start": "node app.js",
"test-init": "node ./node_modules/jasmine/bin/jasmine.js init",
"test": "NODE_ENV=Dev node ./node_modules/jasmine/bin/jasmine.js"
},
Note: solution 1 is better in the long run while solution 2 is quick but dirty

Related

npm run points to different .env file

We are working on nodeJs/ExpressJs we have configured multiple .env files for development and production and pointing it to package.json for different execution process, we have naming conversation issues at scripts.
Whenever we run npm run prod it takes to preprod configuration. what could be the issues?
Update: we have figured that the suffix of the script key is the same in the next script, after update/rename preprod to preProd the both runs fine. but why?
Eg :
"scripts": {
"dev": "clear; env-cmd -f ./config/hostedDev.env nodemon --exec babel-node index.js",
"prod": "clear; env-cmd -f ./config/prod.env nodemon --exec babel-node index.js",
"preprod": "clear; env-cmd -f ./config/preprod.env nodemon --exec babel-node index.js"
},
Apparently the issue is with the word 'pre'.
If you would have noticed it runs both preprod and prod commands (pre running first).
If you change the script name to 'postprod' the postprod script will run later.
So, I guess npm uses 'pre' as to run before the 'prod' script and then running 'prod' script itself.

babel watch on docker

i set docker instance with node.
i want to develop on this instance and use babel to "compile" my node code.
i use #docker/cli to compile with watch flag and i use nodemon with -L flag.
for some reason, nodemon is watching file changes great but not babel.
any idea?
this is my docker-compose.yml
main-app:
build: ./mainApp
user: "root"
command: yarn run start:watch
environment:
NODE_ENV: production
PORT: 8080
volumes:
- ./mainApp:/app
- /app/node_modules
ports:
- '8080:8080'
this is package.json:
"scripts": {
"build": "babel src --out-dir public",
"serve": "node public/server.js",
"build:watch": "babel --watch src -d public -s",
"serve:watch": "nodemon -L public/server.js",
"start:watch": "concurrently -k \"npm run build:watch\" \"npm run serve:watch\""
},
"dependencies": {
"express": "^4.16.1"
},
"devDependencies": {
"#babel/cli": "^7.0.0-beta.35",
"#babel/core": "^7.0.0-beta.35",
"#babel/preset-env": "^7.0.0-beta.35"
},
as you can see i use concurrently to run them both.
what can be the problem babel is not watching my files?
PS: it works fine on my local machine
babel-watch didn't worked out for me.
As I was compiling code through babel cli and outputting in some another directory (to be used by second docker container)
I ended up using nodemon exec option
In my package.json, created new script especially for docker:
"docker-build:watch": nodemon -L --watch src --exec 'npm run build:watch'
and then using npm run docker-build:watch instead of npm run build:watch
Babel CLI uses Chokidar to watch file changes, to make it work inside a linux image you need to:

CHOKIDAR_USEPOLLING=true babel --watch


You can read more about this here
I was having a similar issue and ended up using 'babel-watch'. IT still required me to use the -L flag to enable poling to get it to work in Docker. I have not tried it, but the same approach may work with babel itself.
Take a look at the babel-watc readme for more details. https://github.com/kmagiera/babel-watch#troubleshooting
You filesystem configuration doesn't trigger filewatch notification
(this could happen for example when you have babel-watch running
within docker container and have filesystem mirrored). In that case
try running babel-watch with -L option which will enable polling for
file changes.

How to deploy an npm project to Heroku?

I currently have an npm project built with vue-cli and socket.io server.
This is how my project is structured:
Project/
|--node_server/
| |--server.js
|--src/
| |--main.js
| |--App.vue
| |--other .vue files and folders
I do not know how to deploy this app on Heroku because I will need to run two scripts while deploying that is node server.js (in the node_server folder) and npm run build or npm run dev (in the root folder).
What are the steps on how to deploy it successfully? Heroku takes my project from github whenever I push and builds it automatically. I have tried deploying but it ends up with an error page.
Let's say you need to build the front-end with build script first, and then you need to run nodejs server with start script which is node server.js.
...
"scripts": {
"build": "gulp or something idk"
"prestart": "npm run build",
"start": "node node_server/server.js",
},
...
But if you need to run these two scripts at the same time you can achieve this with something like that:
...
"scripts": {
"build": "gulp or something idk"
"start": "npm run build & node node_server/server.js",
},
...
I hope it would be helpful.
For an example, you can take a look at the package.json of one of my project: https://github.com/hisener/pirate-radio/blob/master/package.json
For more information, please refer https://docs.npmjs.com/misc/scripts

Stop a node server in Travis CI

So, my code contains unit tests that require a server. So I have sever up and running, my tests pass, but the problem is that I don't know how to stop server, so my build fails because it gets timed out.
Here is my configuration file
language: node_js
node_js:
- "7.10.0"
services:
- mongodb
env:
- PORT=6655 IP="localhost" NODE_ENV="test"
script:
- npm start &
- sleep 25
- npm test
Now, how do I stop the node server?
Edit: Here are my npm scripts
"scripts": {
"build-js": "webpack --config webpack.config.js",
"test": "node node_modules/.bin/mocha ./server/test --require ./server/test/testHelper.js --watch --reporter spec --timeout 10000 --compilers js:babel-core/register",
"start": "npm run build-js && babel-node app.js myApp --presets es2015,stage-2 && nodemon --watch cms/client",
}

npm: how to run test & lint on each change?

I am using a bare npm ( no grunt/gulp) approach to develop my new MEAN project.
My config is like the following:
File package.json:
...
"start": "nodemon ./bin/www",
"lint": "jshint **/*.js",
"test": "mocha --recursive",
"dependencies": {
...
},
"devDependencies": {
...
},
Before starting, I run an npm start and nodemon starts monitoring my project tree for changes, triggering a restart after each source file change.
So far so good.
But what if I'd like to include - say - the lint and/or the test stages on each restart?
I didn't find any clue nor in the nodemon page nor in the npm one...
So you should have a definition of the start in your package.json to first run lint, then test then the actual run server.
You can find an example in following post:
http://substack.net/task_automation_with_npm_run
you should run the 'npm run monitor' command to start the monitoring and the restart should call the npm run start script.
but basically you want to have (based on your package.json)
"scripts": {
"start": "npm run lint & npm run test & node ./myfile.js",
"lint": "jshint **/*.js",
"test": "mocha --recursive",
"monitor": "nodemon ./bin/www"
.....

Resources