node.js 配置节点 npm package.json 以便“npm test”适用于 unix 和 windows

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20013989/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 16:17:26  来源:igfitidea点击:

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

windowsnode.jsunixnpm

提问by user949300

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 testto work, package.jsonhad to look like this: (there may be other options???)

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

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

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

而不是所有基于 Unix 的书籍中的内容,

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

如何将 package.json 设置为在 Windows 和 Unix 上工作?我假设 Travis-CI 运行 Unix,所以,如果我将构建链接到它,它会随着 Windows 版本而爆炸。

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

我找到了一个两年前的帖子,有人要求为此提供一个功能。那条线似乎消失了。这个SO 问题似乎很接近,但这并不是我想要的,坦率地说,我无法理解答案。:-( 有人能澄清一下吗?

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

不幸的是,你不能去npm test testOnWindowsUseThisnpm testOnWindowsUseThis。它没有解决 Travis-CI 问题。但至少下载模块的人可以(希望)看到发生了什么。

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

有什么更好的想法吗?我是唯一还在 Windows 下开发的人吗???:-)

回答by tandrewnichols

I've always been able to npm install -g mochaor npm install mochaand then just add

我总是能够npm install -g mochanpm install mocha然后添加

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

到 package.json。这可能适用于也可能不适用于每个环境。我知道,例如,对于接线员,您必须使用 bin/mocha。此外,如果您找不到解决此问题的方法,请为 Unix 设置您的测试脚本,然后添加一个名为“wintest”的第二个脚本或可以在 Windows 中执行您需要执行的任何操作的脚本。您可以随意命名脚本。默认的(测试、启动等)可以与npm [command];一起使用。任何非标准的(如 wintest)都可以与 一起使用npm run-script [command],它们仍然可以工作。

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/.binand when you run npm run [some-command], npm helpfully adds node_modules/.binto 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/.binto the frontof 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).

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

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-scriptstill works as well. I'd expect it to, but I haven't tried it.

不知道在 npm 历史的哪一点发生了变化,但npm run <script-name>现在可以工作(不再需要这样做npm run-script <script-name>)。可能run-script仍然有效。我期待它,但我还没有尝试过。

回答by Frank Nocke

How can I set package.json up to work on both Windows and Unix?

如何将 package.json 设置为在 Windows 和 Unix 上工作?

If you

如果你

  • use Windows
  • dislike -g global install
  • 使用 Windows
  • 不喜欢 -g 全局安装

...this is a working solution

...这是一个有效的解决方案

"scripts": {
  "test": "node node_modules/mocha/bin/mocha.js"
},

Notes:

注意事项

  • putting nodein front shouldn't harm, and can help on Windows (.js extension is not necessarily registered to the nodejus executable, unless you set it so. Could open a text editor, an IDE or (worse) windows scripting host, Internet Explorer…)
  • Adressing the script directly saves you from needing a global install. (Not judging if this is a good practice)
  • forward slashes help running under linux (obviously) and do work under windows (in this scenario. Also avoids a windows pitfall: backslashes if used, would need to be doubled – since they are interpreted as escaping the following letter if used solitary).
  • 放在node前面应该不会造成伤害,并且可以在 Windows 上提供帮助(.js 扩展名不一定注册到 nodejus 可执行文件,除非您将其设置为如此。可以打开文本编辑器、IDE 或(更糟糕的)Windows 脚本主机、Internet Explorer ……)
  • 直接寻址脚本可以让您免于进行全局安装。(不判断这是否是一个好习惯)
  • 正斜杠有助于在 linux 下运行(显然)并在 windows 下工作(在这种情况下。也避免了 windows 陷阱:如果使用反斜杠,则需要加倍 - 因为如果单独使用它们,它们被解释为转义以下字母)。

回答by igorludi

Don't use global solution, I suggest you follow what the Mocha guys say:

不要使用全局解决方案,我建议你按照摩卡人的说法

"scripts": {  
    "test": "node_modules/.bin/mocha -w"
 },

回答by 1j01

Use npm i mocha --save-dev

npm i mocha --save-dev

This will save the module as a development dependency and npm will automatically set up the executables to be used within the scriptsobject. If you want to use the executables outside of the scripts defined in package.json, you can install it globally as well, although note that you may end up with different versions of the package.

这会将模块保存为开发依赖项,npm 将自动设置要在scripts对象中使用的可执行文件。如果您想使用 package.json 中定义的脚本之外的可执行文件,您也可以全局安装它,但请注意,您最终可能会得到不同版本的包。

If you only install it globally, other people won't be happy if they try to run your tests (with the standard npm test)

如果你只在全球范围内安装它,如果其他人尝试运行你的测试(使用标准npm test),他们将不会高兴

回答by Bill

The new way with new npm version after 6.x, you needn't install mocha with globalmode any more.

6.x 之后的新版本 npm 的新方式,你不需要再安装 mocha with globalmode 了。

"scripts": { "test": "npx mocha" }

npx is installed automatically with new npm installation. It will search mochafrom node_modules/.binor $PATH

npx 会随着新的 npm 安装自动安装。它将mochanode_modules/.bin或 $PATH搜索

reference: https://www.npmjs.com/package/npx

参考:https: //www.npmjs.com/package/npx