如何在 Windows 中使用纱线运行简单的文件脚本

Big*_*g D 6 windows node.js yarnpkg

我是整个 Node.js 世界的新手,但这个可能很简单的问题却让我抓狂。这基本上是我在一个更大的项目中遇到的问题的简化版本。

文件结构

package.json
test1
Run Code Online (Sandbox Code Playgroud)

我的 package.json 看起来像这样:

{
  "name": "Temp",
  "version": "1.0.0",
  "main": "index.js",
  "license": "MIT",
  "scripts": {
    "testscript": "test1",
    "testrun":"yarn testscript"
  }
}
Run Code Online (Sandbox Code Playgroud)

test1 仅包含一个简单的console.log('test')命令。

现在,当我这样做时,yarn testrun出现以下错误:

PS C:\dev\temp> yarn testrun
yarn run v1.9.4
$ yarn testscript
$ test1
'test1' is not recognized as an internal or external command,
operable program or batch file.
error Command failed with exit code 1.
Run Code Online (Sandbox Code Playgroud)

如果文件有扩展名(即 test1.js),那么它会识别它并运行。

在有人说添加文件扩展名之前,这只是我创建的最简单的用例。有一个更大的项目包含此类内容,并且似乎对我所有的 Linux 和 Mac 朋友来说都可以正常工作。

zer*_*298 7

你要node test1。您现在正在尝试运行一个名为“test1”的不存在的程序。您希望现有程序节点使用参数“test1”运行,这将告诉节点运行名为“test1.js”(或任何扩展名)的 JavaScript 文件。

考虑文档:yarn 脚本

所以:

{
  "name": "Temp",
  "version": "1.0.0",
  "main": "index.js",
  "license": "MIT",
  "scripts": {
    "testscript": "node test1",
    "testrun":"yarn testscript"
  }
}
Run Code Online (Sandbox Code Playgroud)