Define multi run config with same package.json file - node.js

I have an Electron application that run into a node.js server. And I also want to work with "nodemon" for live execution node server.
This 2 previous sentences getting up 3 different execution possible into start key in package.json file :
electron .
node server.js
nodemon server.js
I would like quickly switch between those execution command. I can create a Run/Config template with PhpStorm but all of those save command run with the same package.json file that wrote the start command.
How can I use the same package.json for working with 3 execution environments?

Related

Waiting for webpack bundling before launching nodemon

I have a nodejs project written in Typescript. Therefore, i have webpack using a typescript loader that transpiles my code in Javascript and bundles it in a server.js file (in a dist folder)
When in developement conditions, my webpack runs with its watcher ON and so does nodemon.
Problem is, when i launch my script for the first time combining webpack and nodemon, since webpack is in watch mode it doesn't have an exit code saying that everything is ok, nodemon script can be started. If i run them simultaneously, nodemon will launch faster than webpack and since server.js file doesn't yet exist, it will crash at the start.
I want to launch thanks to one single command both scripts but make nodemon command wait for the bundling to be done.
First of all, when please provide some code when submitting questions.
and since server.js file doesn't yet exist
I think you should work around your setup a little bit s.t. webpack doesn't create your server.js file if you want to do this.
Basically you can chain multiple commands in a script like so webpack -d && nodemon index.js. This will launch node after webpack completes. However if you setup webpack in watch mode -w it never exists, so you can't chain another command to it. So webpack -d -w && nodemon index.js never gets to the nodemon part.
A solution to the above is to chain them using only &, which I guess you are doing, but in this way they both start at the same time. Hence, if you make your setup independent (webpack doesn't interfere with nodemon starting script) you can list them like so.
If for whatever reasons you can't do this or don't want to, your only option is with 2 separate scripts that you launch manually one after the other.
If I were you, I would just use nodemon-webpack-plugin:
Uses Nodemon to watch and restart your module's output file, but only
when webpack is in watch mode (ie, --watch).
Saves the need for installing, configuring and running Nodemon as a
seperate process.

How does NPM start an Angular and Typescript application?

Would it be possible to get an explanation of how npm start works?
I have an Angular 2 application written in Typescript. I enter npm start in the console and this both compiles and launches the application using Node's built in web server.
However what is the starting point for this? Which files does the npm start command read in order to compile and start the application? Also why do I use npm start and not e.g. node start? I understood that NPM was just the package manager for node itself.
What I understand so far:
I have a tsconfig.js file which tells the TypeScript compiler what to do.
I have a packages.json file which tells node which packages to download.
Where do the following files fit into this:
main.ts
app/app.module.ts - which I understand is the starting point for my application.
How do all of these components fit together to form the application?
npm start is just an alias for npm run start, which runs whatever command is in scripts.start in your package.json.
It neither knows nor cares about TypeScript, or Angular, or anything like that - it just executes whatever script it's given on the command line.

How to test express api on Travis

As mention in a previous post, the following lines only works on local machine for testing.
node app.js
karma start karma.conf.js --single-run
If I put it into the .travis.yml, it make Travis hanging on the "node app.js" line and fail the test after timeout. I think this is because node is supposed to start the app.js and keep listening. On the local machine, I can open 2 terminal windows and run them separately but I am not sure how to do it on Travis. Can anyone help?
node app.js & will make Travis run app.js in the background (this should also work locally). The "&" bit is a standard piece of shell syntax to make a process run in the background.

How to use Node Inspector (node-debug) with Express 4?

I am new to NodeJS and I just discovered node inspector.
The command I usually run in order to start my application is npm start (I use the Express framework) but when I want to debug, I need to run node-debug followed by a specific file.
In the official documentation of node inspector it says that in order to start the debugger I need to type :
$ node-debug app.js
But when I do it simply debugs the app.js file instead of running the server and starting the application.
What is the command I should run in order to debug the whole express application instead of only the app.js file?
When we type npm start the express framework looks at package.json file and executes the file next to the start : tag.
For express, this directory is ./bin/www so the correct command is node-debug bin/www ran from the parent directory instead of node-debug app.js.

I cannot use npm start to start an express server on windows

I am new to node. I am trying to run an express server. I get no errors when installing express but when i run npm start or node app (as all the beginner tutorials point) nothing seems to be happening. The only way i can start the server is by typing node /bin/www. My operating system is Windows. Any advice?
The Express scaffold script generates a package.json file with a start script field that points to the app.js file it also created. Since Express 4 was released the scaffold generator script has been moved to its own package. https://github.com/expressjs/generator
All npm start does is look in the package.json file for a starting script to pass to node. You can see this in the documentation.
Running npm start with a package.json like this:
"scripts": {
"start": "app.js"
}
Is exactly equivalent to running node app.js.
I managed to solve my issue by changing the code page of cmd-dos. By using chcp 850 or chcp 65001 in cmd thus changing codepage to latin - utf8 the issue is gone.

Resources