I have problem trying to run node app in Windows 7.I have installed express generator globally and with it help created a test_app directory and installed the modules. In the package.json scripts look like this:
"scripts": {
"start": "node ./bin/www"
}
However, when i run npm start it just returns this in console:
test_app#0.0.1 start C:\work\test_app
node ./bin/www
If i try to run node app.js nothing happens. Before there wasn't problems with the express, until the moment when i had to install express-generate globally.
Related
app.js (root is not file owner)
var fs = require('fs');
fs.mkdirSync('/root/tmp');
package.json
{
"scripts": {
"app": "node app.js"
}
}
When I run as root npm run app I got
> app
> node app.js
node:internal/fs/utils:344
throw err;
^
Error: EACCES: permission denied, mkdir '/root/tmp'
But it works when I run just node app.js. Which setting/option of npm can help me run npm run app without errors?
Why npm run change user? How to force npm not to change user?
I can replicate the behavior you see on Linux if I run the commands in a shell I've run via sudo, like this: sudo bash. As in the question, npm run app fails but node app.js works.
The difference is that npm run app runs the executable JavaScript file npm-cli.js from the npm package, which looks like this:
#!/usr/bin/env node
require('../lib/cli.js')(process)
That tells us it's running node via env.
So I got to thinking: What user does that run as? So I added "who": "whoami" to package.json and did npm run who. The answer is: It runs as my normal account, not as root, even when I run that from a shell I've launched via sudo bash. My normal user account can't (of course) create subdirectories of root.
I created a web application in azure to host my application js node. (Azure web application)
In my project I have an api in express that is in the app.js, however in the same project I have another file which is a cronjob.
In my package.json I have the following configuration for script:
"scripts": {
"start": "node app.js"
}
When deploying through github, the api that is in the app.js works perfectly.
My question: How do I run cronjob.js simultaneously with app.js?
You can start multiple application by using "pm2" node_module.
After installing pm2 module you can start your application by using the following command in terminal.
pm2 start app.js && pm2 start cronjob.js
You may also use forever node module.
Another option to running multiple scripts simultaneously is npm-run-all.
Install with:
npm install --save npm-run-all
Then setup your "scripts" section in your package.json like so:
"scripts": {
"app": "node app.js",
"cronjob": "node cronjob.js",
"start": "npm-run-all --parallel app cronjob"
}
And start with npm start as usual.
If the only requirement is that, I think there is no need to use another tool. Simply, you can achieve this with a single ampersand &.
"scripts": {
"start": "node app.js & node cronjob.js"
}
I'm completely new to deploying front-end code and thus the question.
I have a React App which I need to run as a background process, however I'm a little confused about the how to do this.
I run a npm script
npm run build
to build, minify and serve the project on a server.
The relevant code for the build process is this.
"prebuild": "npm-run-all clean-dist test lint build:html",
"build": "babel-node tools/build.js",
"postbuild": "babel-node tools/distServer.js"
This is the code inside the distServer.js
import express from 'express';
import path from 'path';
import open from 'open';
import compression from 'compression';
const port = 3000;
const app = express();
app.use(compression());
app.use(express.static('dist'));
app.get('*', function(req, res){
res.sendFile(path.join(__dirname, '../dist/index.html'));
});
app.listen(port, function(err){
if(err){
console.log(err);
}else{
open(`http://localhost:${port}`);
}
});
This works and the project runs, however the moment I close my terminal the project stops.
The build process creates, three files,
index.html
index.js
styles.css
Now if I navigate to the index.html and open it in a browser, but naturally, nothing shows up. So I'm assuming that I'd have to run it as a node process. How do I do this on the production server and run it as a background process so that even if I exit the terminal the app continues to run.
I have checked this issue,
How to make a node.js application run permanently?
But this has a javascript file as the entry point, in my case it's a html file. I'm not sure how can I modify my scripts to run the front-end app permanently as a background process. Any help appreciated.
Your Javascript file (distServer.js) is your entry point – it's the file that you run to start your server. Your HTML file (index.html) is only served as a response to the requests.
babel-node is OK for development, but it's not suitable for production. You can precompile your Javascript files to vanilla Javascript, then use forever or pm2 as described in the question you already linked to in order to keep the server running even after you close your terminal.
How you organize your source files and compiled files is up to you, but here's one way to do it (quote from the documentation for an example Node server with Babel):
Getting ready for production use
So we've cheated a little bit by using babel-node. While this is
great for getting something going. It's not a good idea to use it in
production.
We should be precompiling your files, so let's do that now.
First let's move our server index.js file to lib/index.js.
$ mv index.js lib/index.js
And update our npm start script to reflect the location change.
"scripts": {
- "start": "nodemon index.js --exec babel-node --presets es2015,stage-2"
+ "start": "nodemon lib/index.js --exec babel-node --presets es2015,stage-2"
}
Next let's add two new tasks npm run build and npm run serve.
"scripts": {
"start": "nodemon lib/index.js --exec babel-node --presets es2015,stage-2",
+ "build": "babel lib -d dist --presets es2015,stage-2",
+ "serve": "node dist/index.js"
}
Now we can use npm run build for precompiling our assets, and npm run serve for starting our server in production.
$ npm run build
$ npm run serve
This means we can quickly restart our server without waiting for
babel to recompile our files.
Oh let's not forget to add dist to our .gitignore file.
$ touch .gitignore
dist
This will make sure we don't accidentally commit our built files to
git.
I'm having the same issue as this question which wasn't really resolved as the original questioner abandoned this route. I'm trying to run a node app on Azure using Babel6. The package.json file I'm using has the following in it:
"scripts": {
"start": "node ./node_modules/babel-cli/bin/babel-node.js server.js"
}
I've checked using the Azure console and the babel-cli node module is installed and the server.js file is in wwwroot. Despite this I get the following when I commit to Azure:
remote: Start script "./node_modules/babel-cli/bin/babel-node.js
server.js" from package.json is not found.
The npm version running on Azure is 3.10.3, node version is 6.6.0. Can anyone advise on how to get this up and running. Any help much appreciated!
It seems that to run node.js applications in ES2015 on Web Apps. We need to compile them to ES5 version, you can leverage Custom Deployment Script to achieve this.
Here is example repos on GitHub, which leverages gulp and custom deployment script to compile the node.js from ES6 to ES5 via the Azure deployment task.
Specially, this example defines several scripts in package.json for the deployment task calling:
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "gulp nodemon",
"build": "gulp build"
},
And manually call the npm scripts in deploy.cmd before KuduSync task move the deployment folder to production folder.
:Deployment
echo Handling node.js deployment.
:: 0. Select node version for build
call :SelectNodeVersion
:: 1. Install build dependencies
pushd "%DEPLOYMENT_SOURCE%"
call :ExecuteCmd !NPM_CMD! install --production
IF !ERRORLEVEL! NEQ 0 goto error
popd
:: 2. Run build command
pushd "%DEPLOYMENT_SOURCE%"
call :ExecuteCmd !NPM_CMD! run-script build
IF !ERRORLEVEL! NEQ 0 goto error
popd
I have installed node for windows/64bit and I can run server with example form node homepage(node is working). But when I setup express app it's not able to start server. Express is install with npm install -g express command, and express test, npm install commands instal all necessary files but when I type node app.js in cmd it just skippes line. All path variables are setup and everything is working exept this.
instead of runnig node app.js try npm start.
express generator now put file to start the server in bin/www so you have to say node bin/www to start the app.
If you will see your package.json you will get something like this
"scripts": {
"start": "node ./bin/www"
}
so to start the server you can say npm start or node bin/www.