Create single binary from express + react app with zeit pkg? - node.js

How do I package an express + react folderstructure into a single binary with zeit?
I can run each of them like this:
node server.js
npm run start client
I can start both like this:
scripts element in package.json:
"myapp": "concurrently --kill-others \"node server\" \"npm run --prefix client start\"",
then - npm run myapp in the same folder as package.json.
What I would like to achieve is to apply zeit/pkg somehow so that I have a single binary that I can run that starts both servers in the same way as npm run myapp.
Anyone who knows how?

I don't think zeit/pkg accepts multiple entry points based on their documentation
The entrypoint of your project is a mandatory CLI argument. It may be:
Path to entry file. Suppose it is /path/app.js, then packaged app will work the same way as node /path/app.js
Path to package.json. Pkg will follow bin property of the specified package.json and use it as entry file.
Path to directory. Pkg will look for package.json in the specified directory. See above.
Maybe the better route would be to do some server-side rendering via their Next.js framework? Then you would only have to package your app via the NodeJS entry point.

Related

How to use lerna with dotenv package?

I have a monorepo managed using Lerna. I need to use multiple environment variables to start my web server. My npm script to start the server is:
"scripts": {
"dev:start": "lerna run --parallel dev:start"
}
I am also trying to use dotenv package to load environment variables for .env file. Since, dotenv is purely development helper to set env vars, I have installed it as a devDependency of top package.json. dotenv utilizes node.js preload script like: node -r dotenv/config server_script.js.
But with Lerna, I can no longer invoke preload script. Is there any way to use dotenv with lerna? Or alternately, how to run preload scripts with lerna?
But with Lerna, I can no longer invoke preload script
That's not true. Lerna allows you to pass arguments to the npm script. You just need to pass it with a pair of double dashes:
packages/ServerPkg/package.json:
"dev:start": "node foo.js"
package.json
"dev:start": "lerna run dev:start -- -- -r /path/to/dotenv"
Should execute:
node foo.js -r /path/to/dotenv

Package.json for server and client

I want to install package.json for client side from my server side package.json as the server side is using node and client side is using angular 2
directory structure
server-app
--bin
--node_modules
--package.json
--client-app
--app
--node_modules
--package.json
now the problem is:
I have to run this command npm install from server app folder and also from server-app/client-app folder separately this will create deployment issues
what I want is to run only one time npm install from i.e server-app and it will automatically install the server-app package.json and client-side-app
package.json too.
Any help will be highly appreciated
I think what you need is a npm module called concurrently.
With concurrently installed in your root folder you can run multiple custom npm scripts.
For example: you can create 2 separate scripts that are installing the dependencies (client-install and server-install) and then create install-all-deps script that will run both scripts one after another and install all deps in both directories.
{
"scripts": {
"client-install" : "cd client && npm install",
"server-install" : "cd server && npm install",
"install-all-deps": "concurrently \"npm run server-install\" \"npm run client-install\""
}
}
Here is the npm module https://www.npmjs.com/package/concurrently. Quoting doc:
Run multiple commands concurrently. Like npm run watch-js & npm run
watch-less but better.
Hope this helps.
Structure your application in the following way,
app
--server-app
--client-app
--node_modules
--package.json
This way you can have single package.json file

Heroku build for node app with unusual folder structure

The project is divided into a backend code and react native client code.
Both are sharing one github project. It looks like this:
backend/
--- src/
--- package.json
client/
--- src/
--- package.json
For my heroku instance, I want to run only the backend code, but at the same time want to use continues integration feature from github.
Is there a way to make heroku run npm install & start only from the backend folder?
Create a package.json in the root of the whole project (the parent dir of backend). You can do this with npm init --yes.
Give that top-level package.json file two scripts:
"scripts": {
"postinstall": "cd backend && npm install",
"start": "cd backend && npm start"
}
Should do the trick.

Node - how to run app.js?

I am very new to Node.js and I tried to run a project (made by other developer) by having a command in terminal node app.js. But I encountered below error, do you have any idea how to run this project?
I followed few instructions here to run a project.
Error logs below:
Junryls-Mac-mini:app junrylmaraviles$ node app.js
/Users/junrylmaraviles/Desktop/myfolder/mysubfolder/app/app.js:1
(function (exports, require, module, __filename, __dirname) { define('src/app'
^
ReferenceError: define is not defined
at Object.<anonymous> (/Users/junrylmaraviles/Desktop/myfolder/mysubfolder/app/app.js:1:63)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:902:3
Assuming I have node and npm properly installed on the machine, I would
Download the code
Navigate to inside the project folder on terminal, where I would hopefully see a package.json file
Do an npm install for installing all the project dependencies
Do an npm install -g nodemon for installing all the project dependencies
Then npm start OR node app.js OR nodemon app.js to get the app running on local host
use nodemon app.js ( nodemon is a utility that will monitor for any changes in your source and automatically restart your server)
The code downloaded may require you to install dependencies first. Try commands(in app.js directory): npm install then node app.js. This should install dependencies and then start the app.
Just adding this.
In your package.json, if your "main": "index.js" is correctly set. Just use node .
{
"name": "app",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
...
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
...
},
"devDependencies": {
...
}
}
To run app.js file check "main": "app.js" in your package.json file.
Then run command $ node app.js That should run your app.
Node is complaining because there is no function called define, which your code tries to call on its very first line.
define comes from AMD, which is not used in standard node development.
It is possible that the developer you got your project from used some kind of trickery to use AMD in node. You should ask this person what special steps are necessary to run the code.
To run a node js project you can run the project by below commands
node app.js
But if you want to run your project with npm start then you need to pass "start": "Node app.js" in the scripts of the package.json file
So, your package.json file will look like below
"scripts": { "start": "node app.js", "test": "test" }
Once you are done with the changes then you just need to save the file and then go to the terminal hit the npm start command you will see that the project started as its working on the node app.js command
Refer to below image for clarification
You can also see in the below image that your project runs on both command node app.js as well as npm start
If the Node Js Project :
Normally we can run,
>node app
(or)
Install nodemon dependency (npm i -g nodemon)
>nodemon app.js
(or)
In Package.json, inside the scripts has "start":"nodemon app.js"
>npm start
you have a package.json file that shows the main configuration of your project,
and a lockfile that contains the full details of your project configuration such as the urls that holds each of the package or libraries used in your project at the root folder of the project......
npm is the default package manager for Node.js....
All you need to do is call $ npm install from the terminal in the root directory where you have the package.json and lock file ...since you are not adding any particular package to be install ..... it will go through the lock file and download one after the other, the required packages from their urls written in the lock file if it isnt present in the project enviroment .....
you make sure you edit your package.json file .... to give an entry point to your app..... "name":"app.js" where app.js is the main script .. or index.js depending on the project naming convention...
then you can run..$ Node app.js or $ npm start if your package.json scripts has a start field config as such "scripts": { "start": "Node index.js", "test": "test" }..... which is indirectly still calling your $ Node app.js
in package.json file add the script
"start":"node filename.js"
and run in terminal - > npm start
Node manages dependencies ie; third party code using package.json so that 3rd party modules names and versions can be kept stable for all installs of the project. This also helps keep the file be light-weight as only actual program code is present in the code repository. Whenever repository is cloned, for it to work(as 3rd party modules may be used in the code), you would need to install all dependencies.
Use npm install on CMD within root of the project structure to complete installing all dependencies. This should resolve all dependencies issues if dependencies get properly installed.
const express = require('express')
const app = express()
const port = 3000
app.get('/', (req, res) => res.send('Hello World!'))
app.listen(port, () => console.log(`Example app listening on port ${port}!`))

How do I make "node ." work?

I've run node . in various NodeJS apps in the past, and it seems to know to run index.js, server.js, etc. Is there a package.json setting (or something similar) that I can configure so that NodeJS knows which file should run?
npm does have a one configuration for that:
"main": "app.js"

Resources