How do I run a simple file script from using yarn in windows

Viewed 16185

I'm new to the whole Node.js world but this probably simple problem is driving me nuts. It's basically a simplified version of a problem I'm having with a bigger project.

File structure

package.json
test1

My package.json looks like so:

{
  "name": "Temp",
  "version": "1.0.0",
  "main": "index.js",
  "license": "MIT",
  "scripts": {
    "testscript": "test1",
    "testrun":"yarn testscript"
  }
}

test1 just contains a simple console.log('test') command.

Now when I do yarn testrun I get the following error:

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.

If the file has an extension (i.e. test1.js), then it recognises it and works.

Before anyone says add the file extension, well this is just the simplest use case I've created. There's a much bigger project with this sort of thing in it and seems to work ok for all my Linux and Mac friends.

1 Answers

You want node test1. What you have right now is trying to run a non-existent program called "test1". You want the existent program node to run with the argument "test1" which will tell node to run your JavaScript file named "test1.js" (or whatever the extension).

Consider the documentation: yarn scripts

So:

{
  "name": "Temp",
  "version": "1.0.0",
  "main": "index.js",
  "license": "MIT",
  "scripts": {
    "testscript": "node test1",
    "testrun":"yarn testscript"
  }
}
Related