配置节点npm package.json,以便"npm test"在UNIX和Windows上均可使用 [英] Configure node npm package.json so that "npm test" works on both unix and windows

查看:111
本文介绍了配置节点npm package.json,以便"npm test"在UNIX和Windows上均可使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经开发了一个node.js npm模块,是在Windows下开发的.今天我写了一些摩卡测试.经过许多努力后,npm test似乎必须工作,package.json看起来必须像这样:(可能还有其他选择?)

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" }

而不是所有基于Unix的书中的

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

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

如何设置package.json在Windows和Unix上均可使用?我假设Travis-CI运行Unix,因此,如果我将构建链接到该版本,它将随Windows版本一起崩溃.

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.

我发现了一个已有两年历史的线程,有人为此要求功能.那条线似乎消失了.这个 SO问题似乎已经结束,但这并不是我所要解决的问题想要,坦率地说,我无法理解答案. :-(有人可以澄清吗?

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?

暂时,我要走

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

很遗憾,您不能使用npm test testOnWindowsUseThisnpm testOnWindowsUseThis.而且它不能解决Travis-CI问题.但是,至少有一个下载模块的人可以(希望)看到正在发生的事情.

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.

还有更好的主意吗?我是唯一还在Windows下进行开发的人吗??? :-)

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

推荐答案

我一直能够npm install -g mochanpm install mocha然后添加

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

到package.json.这可能在每个环境中都可能起作用,也可能不起作用.我知道,例如,在巡线员中,您必须使用bin/mocha.另外,如果找不到解决方法,请为Unix设置测试脚本,然后添加另一个名为"wintest"的脚本,或者执行Windows中所需功能的脚本.您可以根据需要命名脚本.默认值(测试,开始等)可以与npm [command]一起使用;否则,不能使用.任何非标准的工具(例如wintest)都可以与npm run-script [command]一起使用,它们仍然可以使用.

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:

在全局安装模块时,该模块可在PATH(或Windows等效的任何版本)上使用.当您安装项目依赖项时,如果该模块具有任何二进制文件,则这些二进制文件将符号链接到node_modules/.bin,并且在您运行npm run [some-command]时,npm会为该命令将node_modules/.bin添加到PATH 中.因此,在全局安装Mocha时,"test": "mocha spec"使用全局安装的Mocha来运行测试.如果是项目依赖项,则使用node_modules/.bin中的依赖项.我发现的一个问题是npm将node_modules/.bin添加到PATH的 front 中,因此本地二进制文件始终优先于全局二进制文件. 几乎所有时间,这就是您想要的,但是值得一提的是它的工作方式(我最近有一个与此相关的错误).

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).

不确定npm历史记录中的哪一点发生了变化,但是npm run <script-name>现在可以工作了(不再需要执行npm run-script <script-name>了).可能run-script仍然可以正常工作.我希望它可以,但我还没有尝试过.

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.

这篇关于配置节点npm package.json,以便"npm test"在UNIX和Windows上均可使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆