We need to integrate Karma test runner into TeamCity and for that I'd like to give sys-engineers small script (powershell or whatever) that would:
pick up desired version number from some config file (I guess I can put it as a comment right in the karma.conf.js)
check if the defined version of karma runner installed in npm's global repo
if it's not, or the installed version is older than desired: pick up and install right version
run it: karma start .\Scripts-Tests\karma.conf.js --reporters teamcity --single-run
So my real question is: "how can one check in a script, if desired version of package installed?". Should you do the check, or it's safe to just call npm -g install everytime?
I don't want to always check and install the latest available version, because other config values may become incompatible
To check if any module in a project is 'old' you should do:
npm outdated
'outdated' will check every module defined in package.json and see if there is a newer version in the NPM registry.
Here is an example, showing that xml2js (that is in node_modules/ in the current directory) is outdated, because a newer version exists (0.2.7):
xml2js@0.2.7 node_modules/xml2js current=0.2.6
If you want to check for outdated modules and install newer version then you can do:
npm update (for all modules) or npm update xml2js (only checks/updates xml2js)
Have a look at the NPM docs:
npm outdated will identify packages that should be updated, and npm update <package name> can be used to update each package. But npm update <package name> will not update the versions in your package.json which is an issue.
The best workflow is to:
npm update to install the latest versions of each packageCheck out npm-check-updates to help with this workflow.
npm-check-updates to list what packages are out of date (basically the same thing as running npm outdated)npm-check-updates -u to update all the versions in your package.json (this is the magic sauce)npm update as usual to install the new versions of your packages based on the updated package.jsonThere is also a "fresh" module called npm-check:
npm-check
Check for outdated, incorrect, and unused dependencies.

It also provides a convenient interactive way to update the dependencies.
When installing npm packages (both globally or locally) you can define a specific version by using the @version syntax to define a version to be installed.
In other words, doing:
npm install -g karma@0.9.2
will ensure that only 0.9.2 is installed and won't reinstall if it already exists.
As a word of a advice, I would suggest avoiding global npm installs wherever you can. Many people don't realize that if a dependency defines a bin file, it gets installed to ./node_modules/.bin/. Often, its very easy to use that local version of an installed module that is defined in your package.json. In fact, npm scripts will add the ./node_modules/.bin onto your path.
As an example, here is a package.json that, when I run npm install && npm test will install the version of karma defined in my package.json, and use that version of karma (installed at node_modules/.bin/karma) when running the test script:
{
"name": "myApp",
"main": "app.js",
"scripts": {
"test": "karma test/*",
},
"dependencies": {...},
"devDependencies": {
"karma": "0.9.2"
}
}
This gives you the benefit of your package.json defining the version of karma to use and not having to keep that config globally on your CI box.