1

Currently I run npm scripts using local deps this way:

package.json:

  "scripts": {
    "test": "node ./node_modules/karma/bin/karma start",
    "node-test": "node ./node_modules/jasmine/bin/jasmine",
    "build": "node ./node_modules/gulp/bin/gulp build"
  },

I don't want to use global deps, since I can forgot to add deps to the package.json. This way when a local dep is missing, then I got an error message and I don't have problems because some deps are not installed globally, e.g. karma plugins.

Is there a better (shorter) way to define npm scripts using the local libs? Is this travis compatible?

edit:

If it wasn't obvious I have the same libs installed globally, but I want to use the local installs by these projects. That means when I start karma with karma start then the globally installed version will start the karma server, which means that if I don't have all of the karma plugins globally installed, then I got error.

Another problem that I have windows, so the solutions described here: How to use package installed locally in node_modules? do not work. Windows does not recognize the #!/bin/sh and the #!/usr/bin/env node head sections and there is no sh command as far as I can tell. At least not in webstorm terminal. Git bash has the sh command, but I want to run these npm scripts from webstorm terminal.

One possible solution could be to fix somehow webstorm so it could use sh from terminal. After that I could use $(npm bin) I assume. But that's just a guess. I am not sure whether this can be done.

Community
  • 1
  • 1
inf3rno
  • 24,976
  • 11
  • 115
  • 197

1 Answers1

4

npm automatically puts prepends the path ./node_modules/.bin to your PATH env before it executes commands run by using npm run (including the two "magic" shortcuts npm start and npm test)

npm scripts docs

You can just set this up with:

"scripts": {
   "test": "karma start",
   "node-test": "jasmine",
   "build": "gulp build"
}

Assuming that you have karma, jasmine and gulp-cli listed in either your devDependencies or dependencies (so that they're install when doing npm install)

And yes, it is travis-compatible. Here is an example of a package that is tested on travis using tap which is installed locally as a module:

https://github.com/scriptoLLC/couchdown/

tkone
  • 22,092
  • 5
  • 54
  • 78
  • `karma start` started the global karma to me. Which caused error, because some of the dependencies weren't globally installed. That's why I am asking this question... – inf3rno Oct 22 '15 at 04:27
  • if you have everything installed locally it'll use the local version since the path gets added to the beginning. [In this gist](https://gist.github.com/toddself/ab1ae5c16a038f2f4f84) I've got tap installed globally AND locally. Then using `which` I ask where it will be running tap from. If i run using `npm test` I get the local version. If I run directly from the command line i get the global version. It will ONLY use the global version if the local version doesn't exist. – tkone Oct 22 '15 at 16:20
  • I edited the question. The problem that webstorm terminal on windows behaves differently than the git bash. E.g. it does not have a which command. If I try `which karma` with git bash from the project root I got the global copy. What operation system do you use? – inf3rno Oct 22 '15 at 17:02
  • I am on OSX. You will not be easily able to run bash/sh scripts on Windows except through various unix-emulation layers (like the git bash you have). Webstorm's terminal is likely either the DOS prompt or powershell. However, the path situation should be the same. – tkone Oct 22 '15 at 17:19
  • It isn't on windows, at least on my machine both the webstorm terminal and the git bash loads the global instead of the local. Hmm I might send a bug report about this, but before that I'll update to node 4 from node 0.12. Maybe they already fixed this. – inf3rno Oct 22 '15 at 18:28
  • According to grassick this is a karma plugin loader related problem: https://github.com/karma-runner/karma/issues/1649 So it does not have anything to do with npm. Can you confirm this? – inf3rno Nov 05 '15 at 07:55
  • By karma you have to install karma-cli https://github.com/karma-runner/karma-cli to be able to run locally from the command line. The other libs don't have this problem as far as I know. – inf3rno Mar 17 '16 at 16:54