'Create file with command line in Node
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.
Solution 1:[1]
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
Solution 2:[2]
You can use the following command:
echo> index.js
Solution 3:[3]
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.
Solution 4:[4]
You can use : copy nul > Gulpfile.js
Solution 5:[5]
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.
Solution 6:[6]
You can also use the following command:
copy con [filename.extension]
[Note: Don't include square brackets]
That's it!
Solution 7:[7]
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
Solution 8:[8]
cd > filename.txt works too... if u create txt files, then it will have the file path on them. Just remember to delete them.
Solution 9:[9]
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
Solution 10:[10]
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow