I am completely new to donejs and created the donejs app using yeoman, and then created a few components. For deploying the application, I ran "node build" and a dist folder was created, which contained a bundles folder and a steal.production.js file.
What is the way to deploy this dist folder over a nodejs server which basically serves the application and also, I dont want any watching in the console, it should basically just start serving over the port, so that the devops can run scripts after that.
build.js
var stealTools = require("steal-tools");
var buildPromise = stealTools.build({
config: __dirname + "/package.json!npm"
}, {
bundleAssets: true
});
From the dist folder :
λ ls
bundles/ steal.production.js
Scripts in my package.json file :
"scripts": {
"test": "testee test.html --browsers firefox --reporter Spec",
"start": "donejs grunt && done-serve --port 8080",
"grunt": "grunt",
"develop": "done-serve --develop --port 8080",
"build": "donejs grunt && donejs develop"
},
After running donejs start :
C:\Users\saljain\Documents\work\statusui\status\status (master)
λ donejs start
> status#0.0.0 start C:\Users\saljain\Documents\work\statusui\status\status
> donejs grunt && done-serve --port 8080
> status#0.0.0 grunt C:\Users\saljain\Documents\work\statusui\status\status
> grunt
Running "less:development" (less) task
>> 1 stylesheet created.
Done.
done-serve starting on http://localhost:8080
It is serving on 8080 but the console is blocked on this, devops team is saying that the console should not be blocked so that they can run scripts after starting the server.
I can see a start script, so try out donejs start if everything else is in order.
Here's some more options as well according to their docs:
To test the production build: NODE_ENV=production donejs start
https://donejs.com/Guide.html#production-build
Related
I have an express API which I am hosting on Heroku. I created two projects: one project points at my Master Branch (Prod) and the other at my Development branch (Dev).
I have two scripts in my package JSON:
"scripts": {
"devStart": "export NODE_ENV=development && nodemon server.js",
"start": "export NODE_ENV=production && node server.js"
}
How can I make it so, that the development branch runs "DevStart" and Master runs "start". Currently Prod is working fine as node start is the default script.
I understand I can put commands in my procfile, however since both Dev and Prod use the same codebase, I would need to change this with each commit. Is there a dynamic way to do this?
Ok, This might not be the correct way but here is how I did it.
I seen you can set the NODE_ENV variable for the Heroku app using terminal:
heroku config:set NODE_ENV=development --app {app-name}
My package JSON though was overwriting this when the app deployed.
"scripts": {
"devStart": "export NODE_ENV=development && nodemon server.js",
"start": "export NODE_ENV=production && node server.js"
},
I changed it to:
"scripts": {
"localStart": "export NODE_ENV=development && nodemon server.js",
"start": "node server.js"
},
This now resolves the problem. No need for the Procfile or to setup environment variables within Heroku.
On Heroku I have two apps, one for production which I don't change the NODE_ENV variable as it defaults to production, and another for Development, which I do change the NODE_ENV variable to 'Development'.
Each app is hooked up to a different branch within the same code base ( Development(Development) & Master(Production) ). Any deploys to either branch will cause a rebuild using the correct NODE_ENV variable for each respective branch.
In Fastify framework is there anyway to reflesh the browser when changes happen on save.
In Express we have npm livereload as a middleware to listen to backend changes in Express. Are there any similar functions in Fastify or do I have to write my own registered plugin to automatically reflesh the browser on backend changes?
The livereload package that you mentioned is a general-purpose tool that works with Fastify as well. I was able to make it run with the following packages:
typescript Running tsc -w -outdir dist/ to watch the .ts files and transpile them into .js files
nodemon Running nodemon -r dotenv/config dist/server.js to watch the .js files and restart the web server
livereload Running livereload ./dist/ -w 500 so that the browser refreshes once the server restarted
concurrently To tie it all together in the package.json
"scripts": {
"build": "tsc",
"start": "node -r dotenv/config dist/server.js",
"dev:tsc": "tsc -w -outdir dist/",
"dev:watch": "nodemon -r dotenv/config dist/server.js",
"dev:livereload": "livereload ./dist/ -w 500",
"dev": "concurrently -k -p \"[{name}]\" -n \"TypeScript,App,LiveReload\" -c \"yellow.bold,cyan.bold,green.bold\" \"yarn dev:tsc\" \"yarn dev:watch\" \"yarn dev:livereload\" "
},
Note that you need to include the following <script> tag in your .html files to make livereload work
<script>
document.write(
'<script src="http://' +
(location.host || "localhost").split(":")[0] +
':35729/livereload.js?snipver=1"></' +
"script>"
);
</script>
This was enough to make my small Fastify project run, your milage may vary.
Yes, there is the fastify-cli module for that:
https://github.com/fastify/fastify-cli
it has the --watch option that you can use to live reload your backend on file changes.
In your package.json add this script:
"dev": "fastify start -l info --watch --pretty-logs app.js",
Note that app.js must expose this interface:
module.exports = function (fastify, ops, next) {
next()
}
I am new in nodejs framework .I read the tutorial, but I want to know how to create a build in node js, in other words I need a script which create my build folder.
I follow these steps
create index.js in root directory add some code.
then add this line of code
import express from 'express';
const app = express();
app.listen(3000,function () {
console.log(`app is listening on 3000`)
})
in my package.json I added start and build script
"scripts": {
"start": "nodemon ./index.js --exec babel-node -e js",
"build": "mkdir dist && babel src -s -d dist"
},
when I do npm run start .my application run fine and I am able to debug also.
Now I want to deploy this application on production Need build.so how to generate build using babel
when I run npm run build I am getting error
src doesn't exist
You 'build' command is like 'mkdir ... && babel src ....', then in the photo it does not have a src folder. So you can simply create a src folder and move index.js to src/, or change the command to 'mkdir dist && babel ./ -s -d dist'. I did not test, but it should work.
I'm a beginner developing with Nodejs and React.
Right now, I've got a first version of my web application which works correctly in development environment, but I'm trying to build a version for production environment but I've got this error
ReferenceError: document is not defined
The scripts of my package.json are:
"scripts": {
"dev-webpack": "webpack-dev-server --hot --mode development",
"clean": "rm -rf ./dist",
"dev": "npm run build-dev && cross-env NODE_ENV=development nodemon --exec babel-node src/server/server.js --ignore ./src/client",
"build-dev": "npm run clean && npm run compile-dev",
"compile-dev": "NODE_ENV=development webpack -d --config ./webpack.config.babel.js --progress",
"compile": "NODE_ENV=production webpack -p --config ./webpack.config.babel.js --progress",
"build": "npm run clean && npm run compile",
"start": "npm run build && node ./dist/assets/js/bundle.js"
},
And I try to create the version for production environment with the command npm run start
I have been looking for information about the problem and it seems it's due because I have no Browserify my web application. But, I don't know how to do this correctly nor the steps to follow to do it correctly.
I am seeking a list of the steps required to build a correct version for production environment.
Edit I:
These are the static files generated with "build" script:
The React application is designed to be run in a browser.
When you run dev-webpack you are running an HTTP server and pointing a browser at it.
When you run build you are creating a static JavaScript file. You need to deploy it to a web server (along with the associated HTML document) and then point a browser at the HTML document.
You are currently trying to execute bundle.js with Node and not a browser.
You need to serve your index.html file. You can use serve to host the HTML file.
I have a simple Dockerfile to create an image to act as a dev environment for me node app. The problem is the port binding between the container and my machine doesn't work as expected.
Dockerfile:
FROM node:10.12.0
EXPOSE 8080
From this file I create an image with the following command:
docker build -t node:10.12.0.bern ./images/node/
I start a container with the following command:
docker run --rm --name=run-client \
-v $(CURDIR)/client:/app/client:rw \
-v $(CURDIR)/kawax-js:/app/kawax-js:rw \
-v $(CURDIR)/enigma:/app/enigma:rw \
-v $(CURDIR)/client-init/init-client.sh:/usr/local/bin/init-client.sh:rw \
-p 8080:8080 \
node:10.12.0.bern \
/bin/sh -c "sh /usr/local/bin/init-client.sh"
and here is my script init-client.sh:
#!/usr/bin/env bash
cd /app/enigma/
npm link
cd /app/kawax-js/
npm link
cd /app/client/
npm link enigma
npm link kawax-js
npm run dev
which at the end runs my dev script from the package.json file:
"scripts": {
"dev": "npm run dev:server & npm run sass:watch & npm run lint:watch",
"build": "npm run client:build && npm run sass:build",
"dev:server": "webpack-dev-server --mode development --hot",
"dev:watchOnly": "webpack --progress --watch -d",
"client:build": "webpack --mode production",
"client:minify": "NODE_ENV=production npm run client:build",
"client:analyze": "___WEBPACK_ANALYZE__=true webpack",
"sass:build": "node-sass --source-map true --include-path scss scss/app.scss dist/css/bernstein.css",
"sass:watch": "npm run sass:build & nodemon -e scss -x 'npm run sass:build'",
"lint": "eslint src/**",
"lint:changed": "git status | grep .js\\$ | awk '{print $NF}' | xargs eslint ",
"lint:watch": "nodemon -e js -x 'npm run lint -- --fix'",
"test": "jest --verbose",
"test:watch": "jest --verbose --debug --watchAll",
"deploy:staging": "./scripts/deploy.staging.sh",
"deploy:production": "./scripts/deploy.production.sh"
},
Running this script on my machine or the container will start webpack server at port 8080. There is no issues at all starting the server on my machine or from the docker container (logs from the container):
Rendering Complete, saving .css file...
Wrote Source Map to /app/client/dist/css/bernstein.css.map
Wrote CSS to /app/client/dist/css/bernstein.css
[nodemon] clean exit - waiting for changes before restart
clean-webpack-plugin: /app/client/dist/js has been removed.
ℹ 「wds」: Project is running at http://localhost:8080/
ℹ 「wds」: webpack output is served from /assets
ℹ 「wds」: Content not from webpack is served from /app/client/public
ℹ 「wds」: 404s will fallback to /index.html
I can see all the bundling was done correctly if I look to my mounted volume. Running docker ps I get:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
db2c5d45c9a6 node:10.12.0.bern "sh /usr/local/bin/i…" 2 minutes ago Up 2 minutes 0.0.0.0:8080->8080/tcp run-client
The problem is when I go to 0.0.0.0:8080 on my browser I see nothing. I don't get it, why is the port binding not working although the server has started and running fine in the container?
Watch out for webpack-dev-server! It is bound to localhost by default.
Change it to :
"dev:server": "webpack-dev-server --mode development --hot --host 0.0.0.0"
You need to update your webpack-dev-server (in your package.json command and webpack.config.json) configuration to bind host to 0.0.0.0 instead of localhost (default!). localhost is just an alias in your operating system, it is not available within the container.
https://webpack.js.org/configuration/dev-server/#devserver-host
Important change
replace Project is running at http://localhost:8080/
should be running at http://0.0.0.0:8080/
in your app.
Use netstate -tulpn . OR ss -tln on the host machine and find the port 8080 , and make sure it is bound to 0.0.0.0 or the ip of the host machine.
Also , in you browser , access it like this if its bound to localhost only:
http://127.0.0.1:8080
OR this if is bound to host ip or 0.0.0.0
http://host-ip:8080
also make sure you can curl it on 8080 both inside the container and on host using either 127.0.0.1 or localhost and host IP