30

So I'm attempting to do this Node.js tutorial, and it says to create three .js files from the command line.

touch server.js client.js test.js

Except I get the following error:

'touch' is not recognized as an internal or external command, operable program or batch file.

Not sure what is wrong here. I've installed Node.js as well as npm and browserify. I've created the package.json file correctly.

I suppose I could go into the project directory, right click and make a new file that way, but that defeats the purpose doesn't it?

What is the actual command to create a new file in the command line?

I'm using Windows 7.

ckpepper02
  • 3,297
  • 5
  • 29
  • 43
  • 6
    `touch` has nothing to do with node.js. See [Windows equivalent of the Linux command 'touch'?](http://superuser.com/questions/10426/windows-equivalent-of-the-linux-command-touch) – Ram Oct 13 '14 at 15:53
  • 1
    Working with node on Windows you really might want something like cygwin -> https://www.cygwin.com/ Just because most of the resources you see will refer to unix commands. Also, make sure you install apt-cyg when you install cygwin so you don't have to run the installer each time you want to add a package/command. It takes a little bit to get to where you want to be with it but I have found it to be worth it a million times over. – Resist Design Apr 25 '15 at 17:25
  • 1
    Possible duplicate of [Windows equivalent of 'touch' (i.e. the node.js way to create an index.html)](http://stackoverflow.com/questions/30011267/windows-equivalent-of-touch-i-e-the-node-js-way-to-create-an-index-html) – Vlad Bezden Mar 17 '17 at 15:48
  • I already provided answer to this question at: [Windows equivalent of 'touch' (i.e. the node.js way to create an index.html)](http://stackoverflow.com/a/37756874/30038) – Vlad Bezden Mar 17 '17 at 15:50

11 Answers11

58

That command is for a unix environment. You can use the following to create an empty file with similar functionalities that touch has in windows:

echo $null >> server.js in the desired directory

ltalhouarne
  • 4,586
  • 2
  • 22
  • 31
  • 4
    No need to include $null and >>. Simply give the command echo > filename with extension – Nishat Lakhani Dec 05 '17 at 10:11
  • this created a file with $null written in it. PowelShell (vscode) didn't show it, and I got "invalid or unexpected token" error. When creating through CMD the value is visible (windows 10) – nanquim Nov 25 '18 at 20:38
10

You can use the following command:

echo> index.js
Hukmaram
  • 523
  • 5
  • 11
4

touch is generally present on *nix platforms but not Windows. You will be able to follow the tutorial without it.

The tutorial author is using it to create empty files. You could achieve the same by simply saving files with the same names using an editor.

Alternatively, if you really want to use it in Windows, try Cygwin.

joews
  • 29,767
  • 10
  • 79
  • 91
4

I know this is an old post, but the question is still relevant and there is a way to integrate the touch command into Windows, using the touch-for-windows npm package. It can be installed globally via node/npm with npm install -g touch-for-windows.

As someone who uses a pretty customized terminal across multiple machines, Cygwin didn't provide some of the other features I often use. The echo command (and accepted answer) provided by ltalhouarne works, but I felt was cumbersome to use. This npm package, though old, operates exactly in Windows as the touch command in *nix.

Jason Holtzen
  • 183
  • 2
  • 10
2

You can use : copy nul > Gulpfile.js

GeoMar
  • 21
  • 1
1

Use the below command example to create any file in cmd:

type NUL > index.js

(here index.js is the file I want to create)

Lee Taylor
  • 7,761
  • 16
  • 33
  • 49
Nawaz
  • 11
  • 1
0

You can also use the following command:

copy con [filename.extension]

[Note: Don't include square brackets]

That's it!

Nishat Lakhani
  • 733
  • 1
  • 8
  • 20
0

Follow the link For installation in Windows https://www.npmjs.com/package/touch-for-windows Installation In command prompt type: npm install -g touch-for-windows.

Usage After installing, you can run this application via the command line, as shown below.

C:\pythonapp>touch index.html Successfully created 'index.html'

Worked for me

  • From Review: Hi, while links are great way of sharing knowledge, they won't really answer the question if they get broken in the future. Add to your answer the essential content of the link which answers the question. In case the content is too complex or too big to fit here, describe the general idea of the proposed solution. Remember to always keep a link reference to the original solution's website. See: [How do I write a good answer?](https://stackoverflow.com/help/how-to-answer) – sɐunıɔןɐqɐp Oct 23 '19 at 08:05
0

cd > filename.txt works too... if u create txt files, then it will have the file path on them. Just remember to delete them.

0

If you want a cross-platform solution in an environment where you have Node.js installed, you can as well run javascript code with node:

node -e "require('fs').writeFileSync('server.js', '')"
node -e "require('fs').writeFileSync('client.js', '')"
node -e "require('fs').writeFileSync('test.js', '')"

The commands above will create your 3 files with no content. You can also replace the '' by any content you would like to have in the file.

For more complex logics, you can move your code into a javascript file and execute it like:

node path/to/script.js
pmrotule
  • 9,065
  • 4
  • 50
  • 58
0

If you use git, there is already a bash installed inside

c:\Program Files\Git\bin\bash.exe

You can use it to work without installing cygwin. It contains the touch command.

Fxp
  • 115
  • 8