How to build node js applciation? - node.js

it is generic question.
I know that if I have java project I can run mvn clean install and it will build the project and the artifacts will be jar or war in target folder
if U have node.js application How I can build it ? I know that I have package.json that could run with npm install and the artifices are node modules but I guess there is a way to build the result of the node application . what is the type of the result ?

How to build node js application?
You don't really "build" a regular node.js application. There is no compile step. There is no separate executable. Whatever your main entry Javascript file is, you would just run it with node main.js and everything else will be loaded from there as your Javascript files load other modules. The Javascript interpreter will compile your JS files on the fly as they are encountered. If there is a syntax error when the file is loaded, it will throw an exception at that time.
For the simplest possible hello world application, you'd just put this into a text file named main.js:
console.log("Hello World");
And, then type this at the command line:
node main.js
And, you would see your output in the console. No compile step. No build step.
There are transpilers and there are packagers that can do special things, but none of that is needed for a regular node.js app.
but if I write something wrong in the code.How i can check it for example like mvn
Javascript is an interpreted language (like PHP, Python, Perl, etc...), not like Java. You will likely get a run-time error when you run your app if you write something wrong in the code. Of course, there are all sorts of tools that will check things for you before you run your code (such as linters), but Javascript does not work like Java in that regard.
If you want a "typed" and "checked" language, then you can use TypeScript which is "compiled" into Javascript that node.js can run and the compile step for TypeScript will check your syntax for you, enforce data typing, etc...

Related

Compile a typescript node js app including dependencies

I have created a Node.js app using typescript and trying to compile it. Currently, the build only contains the source files in the src folder. But I also want to include the dependencies like express, body-parser, etc from node_modules in the final build. This is similar to how webpack compiles all the files together creating bundles.
How can I do this using the typescript compiler only? Or it can be done using webpack only?
Please help.
Typescript compiler only compiles typescript to javascript. You have to manage yourself the build as you want.
I would use a post script to organize your program. Here is a example of pre and post script using npm run build Pre and post script gist

Clientside Javascript in Typescript Express projects

I always wondered how I can properly add the clientsided javascript in my express project. I use Typescript and I would also like to take advantage of the Typescript typings (for jquery for instance) when writing my clientside javascripts.
My project structure looks like this:
root
dist
src
helpers
models
registration
router.ts
form.pug
profile
router.ts
profile.pug
wwwroot
css
js
images
What I have done until today:
I created all clientsided javascript files in wwwroot/js (e.g. jquery.min.js, registration-form.js) and I loaded them into the header of the appropriate pages.
Disadvantages:
I had to write ES5 javascript which is compatible with the browsers we would like to support
I couldn't put the javascript files where they logically belong to (e. g. I'd rather put my registration-form.js into src/registration/ instead of the wwwroot)
No Typescript possible :(. No typescript typings, no transpiling to ES5 etc.
In some tutorials I saw they would simply run npm install --save jquery and import it in their clientsided files. So I feel like I must have missing some pretty important stuff, but I couldn't find any tutorials about it.
My question:
What is the "right way / best practice" to write clientsided javascript in Typescript / Express applications (which should also elliminate also the mentioned disadvantages)?
Using TypeScript on the client side is not much different from the server side.
Here is what you can do:
Create client folder for client-side typescript sources
Put tsconfig.json into client folder and configure it to produce "es5" code (target: es5)
Install jquery types (npm install --save-dev #types/jquery)
That's it, now you can write your client side code in TypeScript.
You can compile server-side code with tsc -p ./src (having server-side tsconfig.json under src) and compile client-side code with tsc -p ./client.
I made a simple example of such app, check it here. I put the simple script to build everything into package.json, so you can run npm run-script complie to get both server and client code complied into /dist folder. Then run it with npm start.
Further steps:
Automate your flow: you should be able to start your app locally and then just edit source TypeScript files and the app should be reloaded automatically. This can be done with webpack / gulp / grunt or custom shell script that can be triggered once any of your source file has been changed and saved.
If you find yourself writing good amount of client-side code, check also angular (https://angular.io/docs). It uses TypeScript as preferred language for client-side development and you'll be able to build much more powerful client-side app using it. You may choose another library as well (react, vue.js, etc), see the examples on the TypeScript site.

Lint node.js code on save

I want to lint my node.js code when saving the file (so I don't have to to run npm run eslint manually). If I were to write the frontend, I'd use webpack to bundle and lint my files on save. However, as I currently don't need to bundle my Node.js code (or do I?), I'm not sure if this is the way to go or if I have any other alternative?
How is this usually done with Node.js? I wasn't able to find any answer to that question using Google's or Stackoverflow's search but I might have looked for the wrong thing.
You could use gulp or grunt or any other build tool to watch your project and run es-lint on save.
Or you could just use a text-editor or ide with a js-lint plugin.

nodejs unit testing framework that does not require an external test runner

All of the unit testing frameworks that I know of / can find require a test runner.
They all require you to globally install and run some program that runs your tests. Is there a well supported testing framework that can run as an npm require() ?
I need one like this because I want to be able to debug my tests, and it is much easier for me to do this though webstorm. Also, the project I'm working on is very small and I don't want to get fancy
The node.js project itself simply uses the built-in assert module and JavaScript exceptions. They have a fairly straightforward script that runs every .js file in a directory tree and if the file doesn't throw any exceptions, the test is considered passing. You could use something like that.
However, although most frameworks do have a command line runner, you absolutely never need to install them (or anything) with -g. If you understand the basic concept of the unix PATH environment variable, you can npm install --save-dev mocha (for example) and then run your tests with ./node_modules/.bin/mocha. No -g required.
See also http://peterlyons.com/problog/2012/09/managing-per-project-interpreters-and-the-path

How to do a dojo build using nodejs?

I'm currently using the dojotoolkit and its build system.
I read the new build tutorial for 1.8 at http://dojotoolkit.org/documentation/tutorials/1.8/build/.
In the tutorial it mentions that you can speed up your build by using nodejs.
The build tool itself relies on Java (and, optionally, Node.js for even faster builds), so make sure that have you that installed as well.
But it fails to mention how to do this. Anyone know how this works?
I normally run it like this:
> node dojo/dojo.js load=build --profile myprofile.profile.js --release
This would build a release for the profile contained in myprofile.profile.js. It assumes you are in a directory, which contains both dojo and util as sub-directories. It also assumes that the path to node is set correctly.
If node is not configured in the path variable, you will need to use the full path to node:
> <path to node here> dojo/dojo.js load=build --profile myprofile.profile.js --release
On windows the path is normally C:\Program Files\nodejs\ but you might have to configure it as C:\PROGRA~1\nodejs\ to get it working.
Windows Notes:
The build scripts do not work with Node on Windows (except using Cygwin). If you are using Windows you can get it to work via the following patch:
Windows Patch
Use the attached node-win.patch file to edit the files: util/build/main.js and util/build/transforms/writeOptimized.js. The patch has worked for me 100% of the time and it a simple matter editing a few lines of code.
I've personally found the alternative to Node, using Rhino, useless. It always fails to detect the build paths correctly, no-matter what I set basePath to. I would strongly advise using Node over Rhino as it is more reliable and easier to setup.
The buildscript util/buildscripts/build.sh checks if node is in your path and if so uses it.
This is currently not working under Windows (http://bugs.dojotoolkit.org/ticket/15413).

Resources