Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Configure node npm package.json so that "npm test" works on both unix and windows

I have developed a node.js npm module, developing under Windows. Today I wrote some Mocha tests. After many struggles, it seemed that for npm test to work, package.json had to look like this: (there may be other options???)

"scripts": { "test": "node node_modules/mocha/bin/mocha" } 

instead of what's in all the Unix based books,

"scripts": { "test": "./node_modules/.bin/mocha" } 

How can I set package.json up to work on both Windows and Unix? I'm assuming that Travis-CI runs Unix, so, should I link the build to that, it will blow up with the Windows version.

I found a two year old thread where somebody requested a feature for exactly this. That thread seemed to die out. This SO question seems to be close, but it isn't exactly what I want and, frankly, I can't understand the answer. :-( Can anybody clarify?

For the time being, I am going

"scripts": {       "test": "node node_modules/mocha/bin/mocha",       "testOnUnixUseThis"    : "./node_modules/.bin/mocha (I think)",       "testOnWindowsUseThis" : "node node_modules/mocha/bin/mocha"   }, 

Unfortunately, you cant go npm test testOnWindowsUseThis or npm testOnWindowsUseThis. And it doesn't fix the Travis-CI issue. But at least a person who downloads the module can (hopefully) see what is going on.

Any better ideas? Am I the only person still developing under Windows??? :-)

like image 977
user949300 Avatar asked Nov 16 '13 02:11

user949300


People also ask

Is it npm test or npm run test?

TL;DR there is no difference. It's just a shortcut for npm tests which run the test command in the package. json file. npm run test performs the same action in this case.

What is the use of npm test command?

The npm test command is used to test a package. This command runs the test script that is contained in a package, if you provided one.

Does npm install automatically add to json?

json, you can re-run npm init -y and the repository field will be added automatically to your package. json. Re-running npm init is additive.


1 Answers

I've always been able to npm install -g mocha or npm install mocha and then just add

"scripts": {     "test": "mocha spec" } 

to package.json. That may or may not work in EVERY environment. I know, for instance, with lineman, you have to use bin/mocha. Also, if you don't find a way around this, set your test script up for Unix and then add a second script called "wintest" or something that does whatever you need it to do in Windows. You can name your scripts whatever you want. The default ones (test, start, etc.) can be used with npm [command]; any non-standard ones (like wintest) can be used with npm run-script [command], and they will still work.

A little back story on how/why this works:

When you install a module globally, it's available on PATH (or whatever the windows equivalent is). When you install a project dependency, if that module has any binaries, those are symlinked to node_modules/.bin and when you run npm run [some-command], npm helpfully adds node_modules/.bin to PATH for that command. So when mocha is installed globally "test": "mocha spec" uses your globally installed mocha to run tests. When it's a project dependency, it uses the one in node_modules/.bin. The one gotcha I've found with this is that npm adds node_modules/.bin to the front of PATH, so local binaries will always take precedence over global ones. Almost all of the time, this is what you want, but it's worth knowing that that's how it works (I recently had a bug related to this).

EDIT:

Not sure at what point in npm history this changed, but npm run <script-name> now works (don't need to do npm run-script <script-name> anymore). Possibly run-script still works as well. I'd expect it to, but I haven't tried it.

like image 137
tandrewnichols Avatar answered Oct 05 '22 17:10

tandrewnichols