Running a Node.JS script from any directory? - javascript

How can I run Node.js like node? at the moment I have to Cd into the directory of my js script, is there a way to run it from any where?

Just run the command and include the full file path:
# alex # alex in ~ [16:53:43] tty:s002 L:1 N:14 C:0
$ node /full/path/to/my/application/file/index.js

There can be possible answer like the one suggest by #ajtrichards, its one way to achieve what you like.
In other ways you can follow full path approach and create a .bat file, one time effort, after that you need only to click that file to run your node app.
Other way, If supposed in your app structure your server folder is of different shape i.e. main file is located at inner leve (its possible in some cases), you can configure package.json to handle your server execution with npm start.
Above are just possible suggestion.

Related

Beginner having trouble running Javascript on Visual Studio Code

New coder here. As the title suggests, I spent the past two hours trying to run a simple Javascript on VSC with no avail. Could someone help me set my sandbox up? Here is a screenshot.
Much appreciated!
kt
Downloaded VSC
Downloaded nodejs
Entered "node scriptname.js" in terminal
Error message above
Your specific error is caused by you running node from the console and providing the wrong path to test.js.
You are in the Desktop directory and just specifying a file name, so Node is looking for test.js in the Desktop directory.
You have saved test.js in a directory called Coding Practice.
You need to either:
cd to the correct directory or
Provide the path to the directory as part of the second argument
Typically, when using VS Code, you would pick the Open Folder option from the File menu to work within your project's root directory (i.e. Coding Practice) which will provide you with a file list and do things like open the terminal in that directory by default.
Once you solve that problem you will run into your second problem.
The contents of test.js isn't JavaScript!
It's an HTML document with JavaScript embedded in it.
You need to:
Give it a .html file extension and
Open it using a web browser and not with Node.js (the traditional way to do that from within VS Code is with the live server extension but you'll really want to have VS code open in the right directory (as above) for that.)
You can't even remove the HTML from the file and run it with Node.js because alert (the function you call) is a Web API and not core JavaScript nor a Node.js API.

Create component directory with all necessary files in a few clicks

I work with React and typical app structure, where I have src/components directory and where I store React components.
I want to simplify a way of creating new components. I want in a few clicks or in one command create a component directory with all necessary files included. And each file should have its own template.
Like:
src/
components/
ExampleComponent/
index.ts
ExampleComponent.tsx
ExampleComponent.test.tsx
ExampleComponent.stories.tsx
ExampleComponent.scss
Is there any way to solve this task? Maybe WebStorm or VScode has templates like this?
You should be able to write your own bash script to be able to do this without too much trouble.
#!/bin/bash
# Create directory and all files
mkdir $1
cd $1
touch index.ts
touch $1.tsx
touch $1.test.tsx
touch $1.stories.tsx
touch $1.scss
It would probably be a good idea to add some error checking to the script like making sure it is given a command line argument, but that should give you something to start with.

How to execute files directly from Notepad++

For the past few weeks, I have been working with Node JS a lot, and i have found it pretty annoying to have to execute the file through cmd every time i want to test.
To make my life easier, I have made a batch file that executes my file for me. My question is how do I execute files directly from notepad++?
Is there a menu for it? Currently on my left I have a menu that lets me navigate between my files easily. On my right, I have a NppFTP UI that lets me connect to the FTP server, if I need to work on files that are not on my system.
I highly suspect that there is a way to execute files directly from Notepad++.
Although my answer doesn't directly answer your question, it will resolve the problem you were facing in the first place.
There is something called nodemon which will restart your node process as soon as you save your changes. It's very simple to use:
To install :
npm install -g nodemon
To use:
nodemon app.js
or Just [it automatically grabs the default app.js file or other]
nodemon
You can learn more about this here:
http://nodemon.io/

Is there a way to link an external library from outside the project directory?

I have a jquery library in my project but need to put it in linux /usr/share/js directory and not inside of my project directory.
Is there a way to do a linking in the index.html?
Including in the index.html:
<script src="/usr/share/js/jquery.min.js"></script>
doesn't help, as it looks into my working dir and I don't have a 'usr' dir
/usr/share is in the root directory and not in my working one. I don't use any php or the stuff, is there a simple way to solve this?
You need to back out of the file you are currently in by using the ../ command which goes back one directory. Depending on how many files deep the index.html file is in your project, you may need more then one like so:
<script src="../../../../usr/share/js/jquery.min.js"></script>
This will direct you 4 files out of where your index.html file is located, you may need more or less depending just do some searching through your directories and adjust accordingly.
You can create symlinks for files like this, to allow access to serve from not your document root.
The web server is also needs to be configured to follow symlinks when serving.
More info for Apache:
http://httpd.apache.org/docs/2.4/urlmapping.html

call to functiolity just one time in node js

There is method which I need to run just once when the node application is started ,I think about to put it in the server.js which is my executable file to the application but Im not sure that this is the right way to go,which other option I can use in this case?
Depending on when specifically it's required to be run, it may be suitable to put the function into a separate javascript file, and execute it as part of your npm start script within your package.json?
Based on your comment below stating it is a child process. It would be appropriate to execute it from the server file, but best practice would be to abstract the function out into a separate file, require it into your server file and then call it.

Categories