I'm building a set of microservices that are run on Docker and orchestrated with Kubernetes. I'm building the services in Node.js and so for each service I have a package.json file that has a version. I'd like to use this version when building my docker images, and to keep the build command simple I'd like to have it be a script in package.json. However, I want to keep the version number DRY (i.e. avoid spreading the version number throughout the package.json file, or having any other copies). For example, if I have the following package.json, how could I reference the version in the script to avoid repeating it?
{
"name": "sample-service",
"version": "1.0.0",
"description": "Sample service",
"dependencies": {
...
},
"scripts": {
"docker": "docker build -t prefix/image-name:$version ."
}
}
Obviously, using $version in the tag there doesn't work so is there another way? Just in case it makes a difference, I'm using yarn rather than npm.
Yes First get the version of package.json
Then export the version to the variable so you can access it as a variable.
Here is your package.json
{
"name": "sample-service",
"version": "1.0.0",
"description": "Sample service",
"dependencies": {
},
"scripts": {
"docker": "export version=$(node -e \"console.log(require('./package.json').version)\") || set version=$(node -e \"console.log(require('./package.json').version)\") ; echo version is: $version; docker build -t prefix/image-name:$version ." }
}
If you run this you will get some thing like
Related
I'm running Postman tests via Newman using files that are in a Git repository. That works fine. My issue is I have multiple sets of tests, each test has it's own Jenkins job that triggers when a build is successful. I'm trying NOT to use multiple git repositories to host these files.
The tests are run using a package.json file like this:
{
"name": "postmanfiles",
"version": "1.0.0",
"description": "project to store postman collection",
"main": "index.js",
"scripts": { "api-tests": "newman run --verbose --timeout 8000000 --reporters 'cli,testrail' 'SearchAPI.postman_collection.json' -e 'My_Environment.postman_environment.json'" },
"keywords": [ "Postman", "CI" ],
"author": "Joe Smith",
"license": "ISC",
"dependencies": { "newman": "^6.14.10"
}
I have multiple Postman collections, for example, 'SearchAPI.postman_collection.json', 'ModifyAPI.postman_collection.json', etc for each set of tests.
In order to not use multiple Git repos for these tests I need multiple package.json files in one repository because I have five different Postman collections to run. Each Jenkins job triggers off of separate builds. An easy way to do this would be to have five different uniquely named package.json files in one Git repository.
But can I do that and if so how?
Or is there an easier way to do this?
I got this to work by specifying multiple tests inside package.json and then having unique Jenkins scripts in the repository, one for each set of tests.
"scripts": {
"api-tests": "newman run --verbose --timeout 8000000 --reporters 'cli' 'SearchAPI.postman_collection.json' -e 'CTV_Environment.postman_environment.json'",
"site-tests": "newman run --verbose --timeout 8000000 --reporters 'cli' 'SiteAPI.postman_collection.json' -e 'CTV_Environment.postman_environment.json'"
},
As far as I know you can't. Which is why we use tools like Grunt/Gulp or Webpack to get complex build workflows done.
How can I replace $INIT_CWD in a Node.js run script with something "generic" that also works on Windows?
package.json in root
{
"name": "foo",
"version": "2.0.0",
...
"scripts": {
..
"start": "live-server $INIT_CWD/foobar --port=8080"
}
}
Test
This works fine on Linux and macOS; serves files from test/foobar.
$ cd test
$ npm start
However, on Windows it would have to be %INIT_CWD% instead of $INIT_CWD.
How can I modify package.json to be OS-agnostic?
The/one solution is to use cross-env-shell from https://www.npmjs.com/package/cross-env.
Run scripts that set and use environment variables across platforms
"devDependencies": {
"cross-env": "^6.0.3"
},
"scripts": {
"start": "cross-env-shell live-server $INIT_CWD/foobar --port=8080"
}
How would it be possible to set environment variables for Mocha tests under windows OS? I'm only able to add only 1 variable but not more, example:
"name": "node-app",
"version": "1.0.0",
"description": "some app",
"main": "index.js",
"scripts": {
"integration-test": "SET TEST_MODE=handler&mocha tests/test_cases/*.js --reporter spec"
},
"author": "",
This can be done under windows using cross-env without changing the source code, we only need to install it as a dev dependency and then add it to the script line. But still under other linux we can simply do this :
"scripts": {
"integration-test": "env KEY1=YOUR_KEY1 KEY2=YOUR_KEY2 mocha test"
},
I wonder if it is possible to make it happen for windows without additional libraries?
There's a package on npm solving this, called cross-env.
From the documentation:
{
"scripts": {
"build": "cross-env NODE_ENV=production webpack --config build/webpack.config.js"
}
}
You can also set multiple variables easily.
No extra library:
before(function (): void {
process.env.YOUR_VAR = 'yourVarValue';
});
I built simple command line application using commander.js for Node.js platform.
Now I want to compile it to simple exe file, Which I can execute directly.
Means I want single executable file for complete application
This is my application structure
APP_ROOT
| - package.json
| - node_modules
| - node_modules/.bin/myapp.bat
| - node_modules/myapp/bin/myapp
| - node_modules/myapp/bin/myapp-action1
| - node_modules/myapp/bin/myapp-action2
Thanks
This is, How i packages my node.js command line app to single executable
Install pkg module using npm i -g pkg
This is my package.json File
json
{
"name": "my-app-exe",
"version": "1.0.0",
"description": "Myapp-Cli tool as executable",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [
"myapp",
"cli",
"exe"
],
"author": "Shisht",
"license": "MIT",
"devDependencies": {
"myapp": "1.0.0"
},
"bin": "node_modules/myapp-cli/bin/cli",
"pkg": {
"assets": "node_modules/**/*"
},
"help": "pkg . --target host --output myapp-1.0.0-x64.exe --debug"
}
Command used to package myapp to myapp.exe
pkg . --target host --output myapp-1.0.0-x64.exe --debug
It's impossible to run a Node application without some kind of Node runtime to run it on - therefore, if you wish to distribute your program as a standalone .exe, you will have to bundle Node itself into said executable as well as your code. There are various tools that will do this for you, such as EncloseJS.
I have just started with nodejs and app engine. I am following the tutorial
https://cloud.google.com/nodejs/tutorials
1) I have created a project then cloned the nodejs-mvms-quickstart repository.
2) Opened Development/source code and opened the nodejs-mvms-quickstart/1-hello-world
3) and started the node server
npm start
This is working fine. Now I want to deploy my nodejs application from my local server without copying to github and cloning on the server.
For this I am using following command
gcloud app deploy
but this is showing following errors:
If this is your first deployment, this may take a while...done.
Beginning deployment of service [default]...
Building and pushing image for service [default]
WARNING: No node version specified. Please add your node version, see https://docs.npmjs.com/files/package.json#engines
Some files were skipped. Pass `--verbosity=info` to see which ones.
You may also view the gcloud log file, found at
[C:\Users\Sunil Garg\AppData\Roaming\gcloud\logs\2017.05.07\20.38.59.221000.log]
.
Started cloud build [1cd2979e-527b-4d68-b430-31471534246e].
To see logs in the Cloud Console: https://console.cloud.google.com/gcr/builds/1c
d2979e-527b-4d68-b430-31471534246e?project=help-coin
ERROR: gcloud crashed (error): [Errno 10054] An existing connection was forcibly
closed by the remote host
If you would like to report this issue,please run the following command:
gcloud feedback
To check gcloud for common problems,please run the following command:
gcloud info --run-diagnostics
Here is my package.json
{
"name": "myapp",
"version": "1.0.0",
"description": "myapp",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": ""
},
"author": "",
"license": "ISC",
"bugs": {
"url": ""
},
"homepage": "",
"dependencies": {
"body-parser": "^1.15.0",
"connect-timeout": "^1.8.0",
"express": "^4.13.4",
"generic-pool": "^2.4.2",
"multer": "^1.2.0",
"mysql": "^2.10.2",
"requestify": "^0.1.17"
}
}
What is the issue? Am I missing something?
Try adding the following 2 parameters to the package.json
Add the version of node your application is compatible with.
"engines": {
"node": "4.6.0"
}
since you're using npm start you might need this in the script parameter as well since npm start is calling command stated in start in script param, change app.js according your application, like node quickstart.js. Always make sure to specify the path if your package.json is in the root directory and the .js is in a sub directory as well.
"scripts": {
"start": "node app.js"
}