I cannot use npm start to start an express server on windows - node.js

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.

Related

Nodemon crashed while setting up express server

while setting up an express server, I tried to run it, bt is showing an error, the exact phrase I typed on the terminal is (npm run dev), I checked whether my port is already under work or if there same name file in my laptop and whether my package.json is in the same folder of my server.js .. nothing is working, being a newbie it's my first project. I would be most grateful if the community can help me.
server.js window - showing error
package.json code
You're trying to require express and your package.json doesn't include it so I assume you don't have it installed
npm install express should fixt it

Why server restart is needed in Node.Js for every change?

I am very new in Node.Js. I just started node.js basic tutorial. But when I change my code I have to restart the server all the times. But is there any way where no need to restart the server again and again.
Nodemon is the best solution for this.
Install nodemon like this "npm i nodemon"
Then restart your project with nodemon, "nodemon app"
You are good to go...
You can install node-supervisor to restart automatically your server when you change the code.
I'm not sure on the details of the compilation process. But I think it's correct to say that on app start, your source code is parsed into computer instructions represented in memory and executed. During runtime source code files are not re-parsed. And so changing the source code will have no effect on the running application. Unless the application re-parses a file prior to execution of the code in that file. Possibly a service worker... But I'm not sure and that would be an exception.
A good way of thinking of nodejs and javascript files (imo) is that the javascript files are configuration for nodejs. Which is a c++ app. So if the configuration changes you need to restart node to read the new configuration.
There are tools such as nodemon that will monitor the source code for file saves and trigger the node application to restart.
Check out Nodemon.
nodemon will watch the files in the directory in which nodemon was started, and if any files change, nodemon will automatically restart your node application.
nodemon does not require any changes to your code or method of development. nodemon simply wraps your node application and keeps an eye on any files that have changed. Remember that nodemon is a replacement wrapper for node, think of it as replacing the word "node" on the command line when you run your script.

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.

Resources