Beginner having trouble running Javascript on Visual Studio Code - javascript

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.

Related

Not able to fork external JavaScript server file in Electron

so I am making an application that requires a backend API, and it uses certain node_modules which don't work when compiling with Electron. To fix this, I put the API code into a separate JavaScript file, which I am attempting to fork using child_process.
I have gotten this to work when compiling, but it immediately stops working after I move the "win-unpacked" folder or try to install the app using the compiled installer.
I have checked, and it is not the path that is wrong, it is correctly pointing to the file. From testing, it appears that the file actually does get forked, but immediately exits with the status code 1.
I can't use require(./filepath.js) because that will just include the code in the compiler, which doesn't work with the modules I am using.
I am hoping someone knows what is wrong and what I should do to fix it, or have any ideas for other ways to run the server code without including it in the compiler.
I am using Vue.js 3 and vue-cli-electron-builder version 2.1.1
The server I am attempting to run is a express server.

WebStorm Error: Please specify path to JavaScript file correctly

I am using WebStorm on Windows to work on node.js projects.
Recently, when I open one of my projects that are located on the OneDrive directory I got the 'Error: Please specify path to JavaScript file correctly' when I run a .JS file. However if I move the same project folder to an other directory (Desktop for example) it works correctly.
PS: Nodemon keeps restarting forever when running on the onedrive project.
How can I fix it?
Please see https://youtrack.jetbrains.com/issue/IDEABKL-7619#comment=27-2504574 for possible workaround.
And, if possible, use some VCS for sharing instead of OneDrive, see https://intellij-support.jetbrains.com/hc/articles/206544839.

can't open javascript files with phantomjs via bash

I just started experimenting with phantomjs via terminal/bash Version 2.5.3 (343.7), on osx 10.10.4. The ready to use binary was installed.
I was able to set phantomjs on my PATH with symbolical linking. Prior to that I had to directly launch the executable or enter its absolute path.
When I try to launch a .js file with phantomjs, for example hello.js, I get " Can't open 'hello.js' ". If I enter the absolute path of the .js file it works:
console.log('Hello, world! It works :D');
phantom.exit();
Johns-MacBook-Pro:~ john$ phantomjs hello.js
Can't open 'hello.js'
VS
console.log('Hello, world! It works :D');
phantom.exit();
Johns-MacBook-Pro:~ john$ phantomjs /Users/john/Documents/phantomjs-1.9.2-macosx/examples/hello.js
Hello, world! It works :D
Johns-MacBook-Pro:~ john$
My question is: Do I need to symbolically link the folder where the .js files
are located with /usr/local/bin/ to ensure that it is also on my PATH or is there another way that
the .js files can be launched with phantomjs, without having to enter the absolute path every time?
I did search before posting this question but I'm uncertain as to how I should interpret everything I found :( The most significant, and somewhat relevant information I found referenced to node.js as the default to execute .js files in terminal.
Please note that currently, the hello.js file I am using is located in
/Users/john/Documents/phantomjs-1.9.2-macosx/examples/hello.js
and phantomjs is located in
/Users/john/Documents/phantomjs-1.9.2-macosx/bin/phantomjs
Please let me know if you need further information and thank you for taking the time to read my question! :)
You need to be in the folder that the file is in to run it directly. If you are in the folder /Users/john/Documents/phantomjs-1.9.2-macosx/examples/ you should be able to run phantomjs hello.js.
# Change directory to where the file is you want to run in phantom
cd /Users/john/Documents/phantomjs-1.9.2-macosx/examples/
# Run the file with phantom
phamtomjs hello.js

How would I refer to a javascript file in a c# library project from an ASP.NET MVC project?

normally I would go
<script src="~/Scripts/Controls/SomeScript.js"></script>
and this finds SomeScript.js in the "Scripts/Controls" folder of the same project that the ASP.NET .cshtml page is in.
The time has come however to share the javascript file between two projects. I need to move it to a library project.
What would the value of the src attribute have to be to locate the javascript file in a completely different project in the same solution? Do I have to do something else? How is this problem usually solved?
I have tried "Copy to output directory" on the "SomeScript.js" properties page and this copies the script and its containing folder to the 'bin' directory. However chrome reports "Not allowed to load local resource" when trying to access .js files in the bin folder.
The folder which your scripts exist must be within the project tree, especially if you are planning on publishing the app to a remote server. Visual Studio provides an option to Add Existing Items as a link, which would allow you to symlink items stored outside your project tree into a folder within your project tree. Any changes to the original file will be reflected in your project; however if the file is deleted, the link will remain, broken.
You must also add the following to the end of the project .csproj file before the Project close tag.
<Target Name="CopyLinkedContentFiles" BeforeTargets="Build">
<Copy SourceFiles="%(Content.Identity)" DestinationFiles="%(Content.Link)" SkipUnchangedFiles="true" OverwriteReadOnlyFiles="true" Condition="'%(Content.Link)' != ''" />
</Target>
This edit to the .csproj file will cause visual studio to actually copy the linked file to the location of the link at the end of the build.
In this way, you can still reference ~/Scripts/Controls/SomeScript.js in your project.
use this reference to resolve your issue: Server.MapPath("."), Server.MapPath("~"), Server.MapPath(#"\"), Server.MapPath("/"). What is the difference?
seems that the best way would be to use / at the beginning - that is the root directory.. you can manage your way down from there

VS2013: Publish minified bundle created on files outside of the project

I use Visual Studio 2013 and .NET 4.5 for an MVC project.
I've learning to use AngularJS via several videos on Pluralsight and one of them walks through the process of using Grunt to clean the output directory, then use ngmin to min-safe the Javascript files.
My process is using a gruntfile.js to clean and run ngmin against the javascript files in my solution, then put them in a directory called app_built. This is executed via a batch file in the pre-build for the project and then I include it via a ScriptBundle with IncludeDirectory pointing to the app_built directory. My intent is to use the Bundling features of .NET 4.5 to do the rest of the minification and concatenation of the Javascript after all the files have been min-safed via Grunt.
I specify the path to the min-safed files with the following:
bundles.Add(new ScriptBundle("~/bundles/minSafed")
.IncludeDirectory("~/app_built/", "*.js", true));
If I run this on my local machine, it runs fine without a hitch. The Javascript is minified and bundled as I'd expect and the resulting web application runs fine as well.
If I publish the website to a remote server, I get a server error that the "Directory does not exist. Parameter name: directoryVirtualPath". I assume this error is saying that it's unable to find the directory populated with my many *.js files. I also assume this is because they weren't published since they aren't part of the solution, even though the folder they reside in is a part of the solution (it's just empty within the solution explorer in Visual Studio).
If my assumption is correct, what can I do to add these files to my solution so they'll be published with the rest of my web application with minimal effort on my end each time?
And if I'm incorrect in the assumption, what I can I do to resolve this otherwise?
Thanks!
I never did find a great way of going about this. I found information at http://sedodream.com/2010/05/01/WebDeploymentToolMSDeployBuildPackageIncludingExtraFilesOrExcludingSpecificFiles.aspx that seems related, but I was unable to make it work.
Rather, since I knew the name of the outputted file, I simply created such an empty file in my project and referenced that where I needed to. I then had the pre-build task replace the contents of that file with the externally minified version and it would be packaged with the project as necessary, so it works well enough.

Categories