Getting "Cannot find module '/app/dist'" error on deploying to Heroku - node.js

I have the following script in my package.json:
"scripts": {
"dev": "nodemon server --exec babel-node --presets es2015,stage-2",
"build": "babel ./server -d ./dist",
"start": "node ./dist",
"heroku-postbuild": "cd react-ui/ && npm install && npm install --only=dev --no-shrinkwrap && npm run build"
},
On deploying to Heroku I get the following error
Error: Cannot find module '/app/dist'
On local npm run dev, npm run build, and npm run start work fine.
Where is it getting the /app folder? How to fix this?
Thanks
Matloob

You should build your app npm run build before deploy to heroku. It will run heroku-postbuild first then start your app.

Try this :
"start:heroku": "node dist/YOUR-APP-NAME/server/main.js",

Related

NPM run build with React + Node + concurrently How to?

I have spend hours trying to figure this out any advice is welcome. The objective here is to assemble a postbuild script that will work on a nodeJS app running a react client.
React is on post 3000 and node is on 5000. So it requires the concurrently library. Below are two attempts do-postbuild and heroku-postbuild (both fail).
"scripts": {
"server": "nodemon server.js --ignore client",
"client": "npm start --prefix ../client",
"dev": "concurrently \"npm run server\" \"npm run client\" ",
"do-postbuild": "NPM_CONFIG_PRODUCTION=false npm install --prefix && npm run build --prefix client",
"heroku-postbuild": "cd ../client && npm install && npm install --only=dev --no-shrinkwrap && npm run build"
},
folder structure
client
server
|_package.json (above)
|_server.js
npm run dev - WORKS perfectly
When I attempt npm run heroku-postbuild it yields the following:
npm ERR! errno 1
npm ERR! ver1.02#1.0.0 heroku-postbuild: `cd client && npm install && npm install --only=dev --no-shrinkwrap && npm run build`
npm ERR! Exit status 1
When attempting to write npm run do-postbuild it throws an error like it is searching for client in the server folder
npm ERR! errno -2
npm ERR! enoent ENOENT: no such file or directory, open '/Users/sites/server/client/package.json'
npm ERR! enoent This is related to npm not being able to find a file.
npm ERR! enoent
This is not a HEROKU solution it is for general UBUNTU server with root access.
Solution here is you don't have to dockerize the app as a bundle (client and server together).
What worked for me was to treat the client and server as two different apps.
Client side:
npm run build locally from the same folder as contains your package.json file
then post the app build folder as very straight forward client side app with HTML CSS Javascript
Server side:
upload the server files (not including node_modules folder)
run npm i(from the folder with the package.json file)
I set up reverse proxy to map the port to a specific location on the server for the react to reach it
set up cron job to start server side (and check periodically to ensure it is running)
Thats it - works perfect.
Please add below Scripts and try, it will work 100%.
"scripts": {
"client-install": "npm install --prefix client",
"start": "node server.js",
"server": "nodemon server.js",
"client": "npm start --prefix client",
"dev": "concurrently \"npm run server\" \"npm run client\"",
"heroku-postbuild": "NPM_CONFIG_PRODUCTION=false npm install --prefix client && npm run build --prefix client"
}
Add this in your scripts
"client-install": "npm install --prefix client",

node_module and aws lambda functions

I have created the lambda application using node. The main issue I face is moving node_module with the npm build. Aws lambda excepts node modules also share the codeUri directory. So I use babel to move all my code .build directory including package.json. Which the best way to move the node_module to .build folder.
"scripts": {
"test": "cross-env NODE_ENV=test jest",
"build": "npm run clean:lib && mkdirp ./.build && npm run copyAndInstall && npm run build:babel",
"build:noclean": "npm run copyAndInstall && npm run build:babel && mkdirp ./.build ",
"build:lint": "eslint -c ./.eslintrc.json --ignore-path ./.eslintignore --ext .jsx,.js \"**\\*.+(js|jsx)\"",
"build:babel": "babel ./src --out-dir ./.build --ignore bower_components,coverage,node_modules,test,.dist,.build,temp,./start.js --comments false",
"clean:lib": "npm install rimraf && rimraf ./build",
"update:modules": "rimraf ./package-lock.json && rimraf ./node_modules && npm i",
"prepush": "npm run build:fix",
"build:fix": "eslint --fix -c ./.eslintrc.json --ignore-path ./.eslintignore --ext .jsx,.js \"**\\*.+(js|jsx)\"",
"copyAndInstall": "copyfiles package.json ./.build/
}
I even tried npm install --production --prefix ./.build" it give an error like
refusing to install package with name under a package npm err! also called did you name your project the same as the dependency you're installing?

Unable to run node.js app through Docker container

I have a node.js application which I am trying to run in docker. Here is the package.json file snippet.
"main": "lib/server.js",
"scripts": {
"clean": "cross-env rm -rf dist",
"build": "cross-env babel lib -d dist",
"start": "npm run clean && npm run build && npm run serve",
"serve": "node dist/index.js",
"dev": "cross-env NODE_ENV=development nodemon lib/server.js --exec babel-node",
"test": "echo \"Error: no test specified\" && exit 1",
"lint": "eslint --ext .js lib/ scripts/ --ignore-pattern node_modules/"
}
And here is the Dockerfile to build the image
#---- Base Node------
FROM node:10.15.1-alpine
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
COPY package*.json ./
COPY lib ./lib
RUN npm install --only=production && npm run build
EXPOSE 4201
CMD ["npm", "run", "serve"]
I get the following error when I try to run the image. Here is the screenshot of the error.
The node and npm version are the same on my machine and the base image that I am using to build my image.
When I run the same command locally in my machine it works
Here is the exact command that I am running on my machine and it works.
npm install --only=production
npm run build
npm run serve
Am I doing something wrong here ? Any help is appreciated.
You are using ES6 imports
import x from package
In order to do that, you should have Babel package installed, otherwise it won't know how to import that file

Restart debugging Node.js only after TSLint and TypeScript compilation are successful

I am trying to write a npm debug script such that when any source file is changed, tslint is executed, followed by tsc and finally node --inspect.
Currently, I have the following scripts in package.json:
"start": "node dist/app.js",
"start-debug": "node --inspect dist/app.js",
"build": "npm run tslint && npm run build-ts",
"tslint": "tslint -c tslint.json -p tsconfig.json",
"build-ts": "tsc",
"debug": "nodemon --exec \"(npm run build || exit 1) && npm run start-debug\""
The problem is that, when the lint process or compilation fails, the node.js instance is terminated. I would like to have the node.js instance restarted only when tslint and tsc exit successfully.
Any idea? Thanks.

Run npm build after beanstalk deploy

Anyone know how to get node AWS beanstalk to run npm run build command after its does its npm install.
I have the following scripts in my package.json but it doesn't seem to run the build or the start command just the install
"scripts": {
"build": "cross-env NODE_ENV=production webpack --config ./webpack/webpack.prod.config.js --progress --colors",
"start": "node server/app.js",
"dev": "nodemon server/app.js"
},

Resources