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",
}
Related
This is part of my project.json
"scripts": {
"prestart": "babel-node buildScripts/startMsg.js",
"start": "npm-run-all --parallel security-check open:src lint:watch",
"open:src": "babel-node buildScripts/srcServer.js",
"lint": "esw webpack.config.* src buildScripts --color",
"lint:watch": "npm run lint -- --watch",
"security-check": "nsp check",
"localtunnel": "lt --port 3000",
"share": "npm-run-all --parallel open:src localtunnel"
}
When I run my tasks manually like:
"nsp check" or "npm run lint -- --watch"
it works fine I see output in the console.
Problem is when I'm running my whole app "npm start" or "npm run lint:watch" these tasks are seems not running although I'm not getting any errors in the console and app seems to run fine I'm getting expected content.
What could be the problem here? and what is the difference if run nsp check or npm run security-check shouldn't the same thing happened?
Example when what shows my console when I execute these two above:
console output
I'm trying to deploy my Express server to my Heroku app, but every time I do, I get an error and it crashes.
I setup my Procfile with the following code: web: node /src/server.js
Where server.js is not in the root of the directory. However, when I try to start the server, I get an error in the logs: Error: Cannot find module '/src/server.js'
When I run the app locally, I have to run npm start in order for it to run the server correctly. Here is the relevant "scripts" portion of my package.json file:
"scripts": {
"start": "NODE_ENV=dev nodemon ./src/server.js --exec babel-node",
"prebuild": "rimraf dist",
"build": "babel ./src -d dist --presets es2015,stage-2 --ignore spec.js,node_modules",
"serve": "NODE_ENV=production node dist/server.js",
"start_babel": "babel-node ./server.js --preset=babel-preset-es2015",
"test": "echo \"Error: no test specified\" && exit 1",
"lint": "eslint ."
}
Since the Procfile overrides the package.json script commands, why is it having trouble finding my file when I've explicitly set it?
I'm not sure about the leading slash. Does using ./src/server.js help at all?
I have a single-page application which is written in NodeJS and deploy on Heroku.
I define my Procfile like:
web: PORT=$PORT webpack -p --config ./config/webpack/serverProd.js --progress && node ./dist/server.js
The reason why I do not use heroku-postbuild deploy hook is because I can not get the PORT variable which is assigned by Heroku dynamically, and this is the only way I can access PORT variable.
But when I run heroku run bash and search around in the container of Heroku...
ta da!
I can not found my server.js which it should be exist in the /app/dist/ directory.
I am pretty sure there is something I misunderstand. Can anyone point that out?
Really appreicate!
This is how I setup my nodejs\npm project on heroku
Procfile
web: npm run build && node dist/index.js
package.json
"scripts": {
"clean": "rm -rf dist && mkdir dist",
"serve": "node dist/index.js",
"start": "nodemon --inspect src/index.js --exec babel-node",
"build": "npm run clean && npm run build-server",
"build-server": "babel src -d dist",
"test": "mocha --compilers js:babel-register"
},
Try to start the app from dist/server.js instead of ./dist/server.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
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"
.....