Create file with command line in Node - javascript

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.

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

You can use the following command:
echo> index.js

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.

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.

You can use :
copy nul > Gulpfile.js

You can also use the following command:
copy con [filename.extension]
[Note: Don't include square brackets]
That's it!

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

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

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

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.

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)

Related

How to compile neutralinojs project in a way that people can open it?

to open neutralino file on windows you'll need to write this:
CheckNetIsolation.exe LoopbackExempt -a -n="Microsoft.Win32WebViewHost_cw5n1h2txyewy"
in the cmd.
My question is: how to make the project executable even if you are not writing this long thing in the cmd?
If you follow the steps in Your First Neutralinojs App, you will have an executable found at <some-location>/myapp/dist/myapp/myapp-win.exe. You can click on this, or create a shortcut to it, so you won't have to write a command line argument.
Run the following command in cmd to build an executable:
neu build
That will create an exe in a dist folder within your project directory.

how to use jsdoc in local project

Goal
I want to use jsdocs with npm.
Background
I am new to npm and its plugins. Recently I found out about jsdoc and I tried using it, with no success.
What I tried
First I installed the package using npm's command npm install --save jsdoc, and the install completed successfully.
However, when I try to use it in my project (I am inside the project's folder) I type jsdoc example.js and I get the following error:
/home/ubuntu/.nvm/versions/node/v4.4.5/bin/jsdoc: No such file or
directory
This leads me to think that:
I am in the wrong directory. If so, where should I be? (other than the project root folder)
The installation had a problem, even though it was successfull.
I have also tried to run npm jsdoc example.js but I get another error (this time from npm, saying I am not using it correctly).
I also installed jsdoc globally (using the -g flag) and it worked, but I truly want to avoid this because not all projects are using jsdoc.
Summary
To conclude, I am looking for a way to run jsdoc locally to a project with npm. Does anyone know of a way to do this?
To run jsdoc in the command line, the location of the jsdoc needs to be known. So when you have installed jsdoc globally, system would be able to find the file.
However if you want to run it locally, you need to include the file location before the jsdoc command.
For example, try using ./node_modules/jsdoc/jsdoc.js example.js

How do I use JSDoc on Windows?

Forgive me if this is a daft question but I'm utterly baffled as to how I can use JSDoc on Windows. I'm aware of JSDoc-Toolkit but it's a bit out of date and the google code repository recommends to use JSDoc 3 instead. I have downloaded JSDoc from Github and unzipped the jsdoc-master folder onto my local disk but can't find any solid information as to how to use it to parse my files.
You can download it as an npm package for the Node.js JavaScript runtime environment.
Install Node.js which comes with npm
Open your a command line
Install JsDoc by typing the following command
npm install -g jsdoc
Run JsDoc / generate documentation. more info
jsdoc path/to/file.js
Configure jsdoc (Optional)
The installation is not good documented on the project-page.
It is much better exlained on the github-page.
I don't know very much about NodeJS/npm ecosystem. However, I did the following steps below and it worked for me (on windows):
Downloaded NodeJS zip file and extracted, it made a directory 'node-v16.15.1-win-x64' with node.exe and and npm.cmd inside
Executed command 'npm install -g jsdoc', under the 'node_modules' directory under the 'node-v16.15.1-win-x64' it installed the jsdoc (in a folder) and also made the jsdoc.cmd file inside 'node-v16.15.1-win-x64'.
This jsdoc.cmd works with the full path but it does not work without the full path
jsdoc gets installed and is working with the above steps but to access it from any where without giving the full path, I had to set the 'node-v16.15.1-win-x64' on Windows PATH, that works.
I am not sure if question of OP is answered but JSDoc works for me this way.
I'm not saying this is necessarily the best way, but it worked for me:
Install node.js
Open a command prompt
As a test, create a folder in your root drive (c:\test) and go to it (cd\test). I guess there was some sort of permission issue as I couldn't get the following steps to work in my desktop folder.
Install the JSDoc package: npm install jsdoc
There should be a folder in test called node_modules
Go to the .bin subfolder in node_modules
There should be a file called jsdoc.cmd. Simple use jsdoc myfile.js in the command prompt to execute the JSDoc script on your file

Change Directory in Node REPL not working?

Just earlier, I posted my question:
https://stackoverflow.com/questions/28336443/how-to-not-put-my-js-files-in-user-myuser-for-node-js
I have a file, hello.js, located in /Users/MyUser/Desktop/Node/
I can see that my default directory is /Users/MyUser/
Okay, so I get that I need to change my working directory. What I have been able to find so far is to use >process.chrdir('/Users/MyUser/Desktop/Node/');
Cool, that works, but now when I get out of the REPL shell, the directory resets.
The person who responded to my question said that I needed to run >node init and later npm install <name of dependency> --save
My first question: I have ran >node init and see that I can create this package.json file, what does this do exactly?
Secondly: I was told that I need to add dependancies. Could someone please explain to me what this means in Node terms? Does a dependancy simply mean a folder that I want node to include? Do I want to add this Node folder on my Desktop to be able to run my scripts?
I am currently trying to go through the learnyounode courses, however I do not want to have to save all of these test files in my /User/MyUser directory, so any advice would be greatly appreciated.
Thanks
I have ran >node init and see that I can create
this package.json file, what does this do exactly?
npm init is used to create a package.json file interactively. This will ask you a bunch of questions, and then write a package.json for you.
package.json is just a file that handle the project's dependencies and holds various metadata relevant to the project[ project description, version, license information etc]
I was told that I need to add dependencies. Could someone please
explain to me what this means in Node terms?
Lets say you're building an application that is dependent on a number of NPM modules, you can specify them in your package.json file this way:
"dependencies": {
"express": "2.3.12",
"jade": ">= 0.0.1",
"redis": "0.6.0"
}
Now doing npm install would install a package, and any packages that it depends on.
A package is:
a folder containing a program described by a package.json file
a gzipped tarball containing (1)
a url that resolves to (2)
a # that is published on the registry with (3)
a # that points to (4)
a that has a "latest" tag satisfying (5)
a that resolves to (2)
If you need to install a dependency that haven't been included in package.json, simply do npm install <packageName>. Whether or not you want to include this newly installed package in package.json is completely your take. You can also decide how this newly installed package shall appear in your package.json
npm install <packageName> [--save|--save-dev|--save-optional]:
--save: Package will appear in your dependencies.
--save-dev: Package will appear in your devDependencies.
--save-optional: Package will appear in your optionalDependencies.
Does a dependency simply mean a folder that I want node to include?
Umm, partly yes. You may consider dependencies as folders, typically stored in node_modules directory.
Do I want to add this Node folder on my Desktop to be able to run my
scripts?
No, node manages it all. npm install will automatically create node_modules directory and you can refer to those dependencies with
require() in your .js files
var express = require('express');
Node REPL simply provides a way to interactively run JavaScript and see the results. It can be used for debugging, testing, or just trying things out.
process.cwd() points to the directory from which REPL itself has been initiated. You may change it using process.chdir('/path'), but once you close the REPL session and restart, it would always re-instantiate process.cwd() to the directory from which it has been started.
If you are installing some packages/dependencies in node project1 and think those dependencies can also be useful for node project2,
install them again for project2 (to get independentnode_modules directory)
install them globally [using -g flag]. see this
reference packages in project2 as
var referencedDependency = require('/home/User/project1/node_modules/<dependency>')
Simply doing process.chdir('/home/User/project1/node_modules/') in REPL and referencing as
var referencedDependency = require('<dependency>') in your js file wont work.
>process.chdir('/Users/MyUser/Desktop/Node/'); change the working directory only for that particular REPL session.
Hope it helps!
This has nothing to do with node.js but is rather inherent in the design of Unix (which in turn influences the design of shells on other operating systems).
Processes inherit values from their parent's environment but their environments are distinct.
That terse description of how process environments work has a sometimes unexpected behavior: you cannot change your parent's environment. It was designed this way explicitly for security reasons.
What this means is, when you change the working directory in a process and quits that process your shell's working directory will not be affected. Indeed, your shell's working directory isn't affected even when the process (in this case, node REPL) is running.
This exact question is often asked by people writing shell scripts wanting to write a script that CDs into someplace. But it's also common to find this question asked by people writing other languages such as Perl, Tcl, Ruby etc. (even C).
The answer to this question is always the same regardless of language: it's not possible to CD from another program/script/process.
I'm not sure how Windows handles it so it may be possible to do it there. But it's not possible on Unixen.

Can't find Typescript compiler: Command "tsc" is not valid

Just installed Typescript extension to VS2012 and followed Install TypeScript for Visual Studio 2012 and then the tutorial to call the compiler:
> tsc greeter.ts
But when i try to compile .ts file where should i type: tsc greeter.ts? Tried it in VS command line and in windows console, always get the message that tsc is not recognized as command(Command "tsc" is not valid.).
If you're using tsc as a node module, make sure you've installed it with
npm install -g typescript
Then it should be available globally in your node command prompt
Ensure you have,
C:\Program Files (x86)\Microsoft SDKs\TypeScript\0.8.0.0
or,
C:\Program Files\Microsoft SDKs\TypeScript\0.8.0.0
on your path. If not, try restarting CMD.EXE and see if shows up with a fresh copy. If that fails, try adding one of the above manually to your path.
For folks on Windows with Visual Studio Code, who don't want to install full Visual Studio just for tsc.exe, I can suggest to simply download it from here: https://www.microsoft.com/en-us/download/details.aspx?id=48593.
This is a shame that this link is missing from the TypeScript download page.
Although the installer is called TypeScript for Visual Studio 2015, it works with Visual Studio Code as well.
After you downloaded and installed TypeScript, you should manually add its installation directory (C:\Program Files (x86)\Microsoft SDKs\TypeScript\2.0\ in my case) to the %PATH% environment variable.
Open environment variables editor:
Win + S; e, n, v, i, r, o; click Edit environment variables for your account.
In the window opened find Path user variable (note that it is not named %PATH% here, but still this is it). You probably have some path set there already. You just have to type ;, append the TypeScript install location and add one more ; in the end.
Screenshot for your reference:
After this is done, open Command Prompt and type in tsc -v. If tsc.exe's version is getting displayed, you're done. For this to work, restart Command Prompt and VS Code after making the change to the %PATH%.
P.S. If you get "error TS5057: Cannot find a tsconfig.json file at the specified directory: '.'", just create tsconfig.json file in the document root (that's probably where your .ts files are) with simple contents: {}. This means "an empty JSON file <...>. This will be sufficient for most people." (source).
Usually closing and reopen the command prompt solves the issues.
Check both of these folders to find out what TypeScript version you have:
C:\Program Files (x86)\Microsoft SDKs\TypeScript\
C:\Program Files\Microsoft SDKs\TypeScript\
Right-click the folder showing the version number then 'copy as path'.
Paste this into your system path (quick access - Windows key then type 'env'). Then open a new command prompt console as administrator and 'tsc' should work.
If you have installed typescript for a specific folder/project i.e. not globally,then you can should use tsc command with npx
e.g
npx tsc myfile.ts
npx is used to excute a package .
As new path variable, don't use the displayed tsc version number but the TypeScript Folder Name which is different.
Sample :
tsc -v display 2.4.1
but TypeScript directory is
C:\Program Files (x86)\Microsoft SDKs\TypeScript\2.4
Just delete AppData\Roaming\npm\tsc.ps1 file and the issue will be fixed.
In my case, I did install typescript globally by using -g flag but tsc was not recognized.
It turns out that below directory was not included in path environment variable:
C:\Users\Jitendra\AppData\Roaming\npm
Adding this directory to path variable worked for me.
In CMD type where tsc if it doesn't show a path like this C:\Program Files (x86)\Microsoft SDKs\TypeScript\typescript version you installed\ it means your tsc is not running from here.You need to change the environment path manually by following #TranslucentCloud 's answer .
You should add the following path
C:\Users\<user>\AppData\Roaming\npm
to the PATH variable. Notice that you should change the to your windows user.
This will work like a charm. Cheers!!
Try
npx tsc greeter.ts
I was getting the same error until i tried this.

Categories

Resources