SyntaxError: Unexpected identifier while deploying React application on Docker container - node.js

I'm new to ReactJS world and helping one team with deployment of their React application to the Docker container. I have done following tasks.
Created Ubuntu machine in EC2 and installed Docker engine.
Copied the entire React code in one of the folder, added DockerFile and Docker-compose yml file. Team has few other applications to deploy together and that is why I've included docker-compose file. But, right now just deploying one React application.
After doing docker-compose up container is getting created and exiting immediately. I then checked the logs of exited container (shown below).
/usr/src/app/src/index.js:1 import React from 'react';
^^^^^
SyntaxError: Unexpected identifier
at Module._compile (internal/modules/cjs/loader.js:723:23)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:831:12)
at startup (internal/bootstrap/node.js:283:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:623:3)
I researched on this error and looks like this is because of compiling, missing webpack or something in React application. I'm not sure what to fix in React and where.
Here is package.json file in case if you can help to figure out the problem.
{
"name": "xxxx",
"version": "0.1.0",
"private": true,
"dependencies": {
"#fortawesome/fontawesome-svg-core": "^1.2.30",
"#fortawesome/free-solid-svg-icons": "^5.14.0",
"#fortawesome/react-fontawesome": "^0.1.11",
"#rjsf/core": "^2.3.0",
"#sentry/browser": "^5.23.0",
"#testing-library/jest-dom": "^4.2.4",
"#testing-library/react": "^9.5.0",
"#testing-library/user-event": "^7.2.1",
"FormData": "^0.10.1",
"append-query": "^2.1.0",
"axios": "^0.19.2",
"babel-loader": "8.1.0",
"bootstrap": "^4.5.2",
"es6-promise": "^4.2.8",
"install": "^0.13.0",
"lodash": "^4.17.20",
"log": "^6.0.0",
"mdbreact": "^4.27.0",
"node-sass": "^4.14.1",
"npm": "^6.14.8",
"react": "^16.13.1",
"react-bootstrap": "^1.3.0",
"react-bootstrap-tabs": "^2.0.0",
"react-confirm-alert": "^2.6.2",
"react-data-table-component": "^6.11.0",
"react-dom": "^16.13.1",
"react-fontawesome": "^1.7.1",
"react-jsonschema-form": "^1.8.1",
"react-popup": "^0.10.0",
"react-redux": "^7.2.0",
"react-router": "^5.2.0",
"react-router-dom": "^5.2.0",
"react-scripts": "3.4.3",
"react-stepzilla": "^6.0.2",
"react-stepzilla-redux": "0.0.3",
"react-toastify": "^6.0.8",
"react-tooltip": "^4.2.8",
"reactstrap": "^8.5.1",
"redux": "^4.0.5",
"redux-logger": "^3.0.6",
"redux-thunk": "^2.3.0",
"styled-components": "5.1.0",
"validate": "^5.1.0"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"devDependencies": {
"#babel/preset-env": "^7.11.5"
}
}
Here is the content of DockerFile
FROM node:10
# FROM alpine:latest
WORKDIR /usr/src/app
# add node_modules/.bin:$path
ENV PATH /usr/src/app/node_modules/.bin:$PATH
# Install app dependencies
COPY package*.json ./
RUN npm install
# Bundle app source
COPY . .
#
EXPOSE 80
CMD npm start
And, docker-compose file looks like below.
version: "3"
services:
web:
build: ./pdf_web_frontend
command: "node src/index.js"
ports:
- "800:80"
Do you see any problem in anything above? TIA.
Note: I have already seen this link SyntaxError: Unexpected identifier importing React (Javascript)

After a lot of research, I could deploy this react app. Here are the issues.
Issue was with the dockerfile and docker-compose file. I added completely new files with different content.
React app was deploying to some miserable port. So, I explicitly added PORT 8080 in package.json file. This is not a clean solution but this worked.
Below are the new files.
package.json
{
"name": "xxxx",
"version": "0.1.0",
"private": true,
"dependencies": {
"#fortawesome/fontawesome-svg-core": "^1.2.30",
"#fortawesome/free-solid-svg-icons": "^5.14.0",
"#fortawesome/react-fontawesome": "^0.1.11",
"#rjsf/core": "^2.3.0",
"#sentry/browser": "^5.23.0",
"#testing-library/jest-dom": "^4.2.4",
"#testing-library/react": "^9.5.0",
"#testing-library/user-event": "^7.2.1",
"FormData": "^0.10.1",
"append-query": "^2.1.0",
"axios": "^0.19.2",
"babel-loader": "8.1.0",
"bootstrap": "^4.5.2",
"es6-promise": "^4.2.8",
"install": "^0.13.0",
"lodash": "^4.17.20",
"log": "^6.0.0",
"mdbreact": "^4.27.0",
"node-sass": "^4.14.1",
"npm": "^6.14.8",
"react": "^16.13.1",
"react-bootstrap": "^1.3.0",
"react-bootstrap-tabs": "^2.0.0",
"react-confirm-alert": "^2.6.2",
"react-data-table-component": "^6.11.0",
"react-dom": "^16.13.1",
"react-fontawesome": "^1.7.1",
"react-jsonschema-form": "^1.8.1",
"react-popup": "^0.10.0",
"react-redux": "^7.2.0",
"react-router": "^5.2.0",
"react-router-dom": "^5.2.0",
"react-scripts": "3.4.3",
"react-stepzilla": "^6.0.2",
"react-stepzilla-redux": "0.0.3",
"react-toastify": "^6.0.8",
"react-tooltip": "^4.2.8",
"reactstrap": "^8.5.1",
"redux": "^4.0.5",
"redux-logger": "^3.0.6",
"redux-thunk": "^2.3.0",
"styled-components": "5.1.0",
"validate": "^5.1.0"
},
"scripts": {
"start": "PORT=8080 react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"devDependencies": {
"#babel/preset-env": "^7.11.5"
}
}
Dockerfile
FROM node:alpine
WORKDIR /app
COPY package.json /app
RUN yarn install
COPY . /app
CMD ["yarn", "run", "start"]
docker-compose.yml
version: "3"
services:
client:
stdin_open: true
build:
context: .
dockerfile: Dockerfile
ports:
- "8080:8080"

Related

React app taking too long to start the development server

I am using a react dashboard template from themeforest. Here is the package.json content:
{
"name": "acorn-react",
"version": "1.0.1",
"private": true,
"proxy": "http://localhost:3000",
"dependencies": {
"#emoji-mart/data": "^1.0.2",
"#fullcalendar/bootstrap": "^5.9.0",
"#fullcalendar/daygrid": "^5.9.0",
"#fullcalendar/interaction": "^5.9.0",
"#fullcalendar/react": "^5.9.0",
"#fullcalendar/timegrid": "^5.9.0",
"#glidejs/glide": "^3.4.1",
"#reduxjs/toolkit": "^1.6.0",
"#testing-library/jest-dom": "^5.13.0",
"#testing-library/react": "^11.2.7",
"#testing-library/user-event": "^13.1.9",
"#yaireo/tagify": "^4.12.0",
"autosuggest-trie": "^2.1.1",
"axios": "^0.21.1",
"axios-mock-adapter": "^1.19.0",
"chart.js": "^3.5.1",
"chartjs-adapter-luxon": "^1.0.0",
"chartjs-plugin-crosshair": "^1.2.0",
"chartjs-plugin-datalabels": "^2.0.0",
"chartjs-plugin-streaming": "^2.0.0",
"clamp-js": "^0.7.0",
"classnames": "^2.3.1",
"date-fns": "^2.22.1",
"emoji-mart": "3.0",
"formik": "^2.2.9",
"fuse.js": "^6.4.6",
"intro.js": "^4.2.2",
"intro.js-react": "^0.5.0",
"luxon": "^2.0.1",
"overlayscrollbars": "^1.13.1",
"overlayscrollbars-react": "^0.2.3",
"pixabay-api": "^1.0.4",
"plyr-react": "^3.0.8",
"quill": "^1.3.7",
"rc-slider": "^9.7.2",
"react": "^16.13.0",
"react-autosuggest": "^10.1.0",
"react-bootstrap": "^2.0.0-rc.0",
"react-circular-progressbar": "^2.0.4",
"react-contexify": "^5.0.0",
"react-countdown": "^2.3.2",
"react-datepicker": "^4.1.1",
"react-datetime": "^3.1.1",
"react-dom": "^16.13.0",
"react-dropzone-uploader": "^2.11.0",
"react-flow-renderer": "^9.6.2",
"react-fuzzy-highlighter": "^1.0.0",
"react-helmet": "^6.1.0",
"react-hook-mousetrap": "^2.0.4",
"react-image-lightbox": "^5.1.4",
"react-intl": "^5.20.2",
"react-masonry-css": "^1.0.16",
"react-number-format": "^4.6.4",
"react-paginate": "^8.1.3",
"react-rating": "^2.0.5",
"react-redux": "^7.2.4",
"react-router-dom": "^5.2.0",
"react-scripts": "^4.0.3",
"react-select": "^4.3.1",
"react-simple-maps": "^3.0.0",
"react-slick": "^0.29.0",
"react-sortablejs": "^6.0.0",
"react-syntax-highlighter": "^15.4.4",
"react-table": "^7.7.0",
"react-tag-autocomplete": "^6.2.0",
"react-tenor": "^2.2.0",
"react-toastify": "^7.0.4",
"react-tooltip": "^4.2.21",
"react-world-flags": "^1.5.0",
"redux-undo": "^1.0.1",
"reduxjs-toolkit-persist": "^7.0.1",
"slick-carousel": "^1.8.1",
"sortablejs": "^1.14.0",
"web-vitals": "^0.2.4",
"yup": "^0.32.9"
},
"scripts": {
"start-js": "react-scripts start --dev",
"start-css": "sass --style=compressed --watch
src/sass/styles.scss:public/css/styles.css",
"start": "concurrently \"npm run start-js\" \"npm run start-css\"",
"build-js": "react-scripts build --prod",
"build-css": "sass --style=compressed --no-source-map
src/sass/styles.scss:public/css/styles.css",
"build": "concurrently \"npm run build-css\" \"npm run build-js\"",
"test": "react-scripts test",
"eject": "react-scripts eject",
"lint": "eslint ./src/**/*.js",
"lint:fix": "eslint ./src --fix",
"precommit": "lint-staged",
"concurrently": "concurrently"
},
"eslintConfig": {
"extends": [
"react-app"
]
},
"husky": {
"hooks": {
"pre-commit": "lint-staged"
}
},
"lint-staged": {
"src/**/*.{js,jsx,ts,tsx,json,css,scss,md}": [
"prettier --write",
"git add"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"devDependencies": {
"#babel/core": "^7.18.2",
"#babel/preset-env": "^7.18.2",
"#babel/preset-react": "^7.17.12",
"babel-loader": "^8.1.0",
"babel-plugin-module-resolver": "^4.1.0",
"concurrently": "^6.2.0",
"eslint": "^7.28.0",
"eslint-config-airbnb": "^18.2.1",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-import": "^2.23.4",
"eslint-plugin-jsx-a11y": "^6.4.1",
"eslint-plugin-prettier": "^3.4.0",
"eslint-plugin-react": "^7.24.0",
"eslint-plugin-react-hooks": "^4.2.0",
"husky": "^6.0.0",
"lint-staged": "^11.0.0",
"prettier": "2.3.1",
"sass": "^1.42.1"
}
}
After I run npm start, it takes about a minute or two until I see Starting the development server and localhost starts loading in browser. And then it takes about 30 minutes until the page is loaded. (Yes 30 minutes!) . I know that the list of dependency is quite long but still I expect it to start in maximum 5 minutes. I have searched a lot for similar issues and tried to apply the suggested solutions:
Allocated more memory: --max_old_space_size=4096
Updated nodejs and npm
Closed all browser tabs before starting the app
Notes:
When I ran npm install there were dependency errors, so I had to do npm install --force
I am running this on Windows 10 , my pc has 8GB of RAM.
Thanks to everyone who will help!
I think the problem is not with the package.json. the problem is you have something that makes it slower, which is the package prettier, the same thing happened to me, the problem was the prettier package that made my app slow by taking too much time in analyzing the format and makes the format better according to the language, so this process takes times, having too much files and prettier checks all of the file again and again, the more files you have the more time it will take. So i recommend removing prettier. You can just download a vscode extension of prettier that does the job better.

how to run multiple watch script in docker

I'am moving my react apps into docker, I am working in legacy project and we have multiple react apps. We are attaching script with react apps in script tags on every page whose need to use this files. For docker we want to use Express to serve our files.
Is there any way to run multiple watch commands ?
here is my package.json file:
{
"name": "docker apps",
"version": "3.0.0",
"description": "apps",
"license": "ISC",
"author": "devs",
"main": "main.js",
"engines": {
"node": ">=14.7.0",
"npm": ">=6.14.13"
},
"scripts": {
"requirements-check": "node checkNodeVersion.js",
"react": "webpack --env.mode production --env.app react",
"react-watch": "webpack --env.mode development --env.app react --watch",
"serve": "nodemon server.js",
"serve:prod": "node server.js --port=80",
"build": "npm run react",
"start": "npm run react-watch",
"test": "jest",
"lint": "eslint .",
"dev-start": "concurrently \"npm run react-watch\" \"npm run serve\"",
"dev-build": "concurrently \"npm run build\" \"npm run serve:prod\"",
"lint-payment": "eslint payment/",
"lint-formLibrary": "eslint formLibrary/",
"lint-fileManager": "eslint fileManager/",
"lint-blockLibrary": "eslint LPBlockLibrary/",
"lint-contracts": "eslint contracts/",
"lint-payment:fix": "eslint --fix payment/",
"lint-formLibrary:fix": "eslint --fix formLibrary/",
"lint-fileManager:fix": "eslint --fix fileManager/",
"lint-blockLibrary:fix": "eslint --fix LPBlockLibrary/",
"lint-contracts:fix": "eslint --fix contracts/",
"lint:fix": "eslint --fix ."
},
"dependencies": {
"#mikecousins/react-pdf": "^5.5.1",
"#tkaramanski/fmcomponents": "^1.6.2",
"#types/testing-library__jest-dom": "^5.9.5",
"axios": "^0.21.1",
"bootstrap": "^4.1.3",
"classnames": "^2.2.6",
"cors": "^2.8.5",
"express": "^4.17.1",
"gulp-cli": "^2.2.0",
"gulp-install": "^1.1.0",
"handlebars-loader": "^1.7.1",
"jsdom": "^16.6.0",
"lodash": "^4.17.11",
"postcss": "^8.1.4",
"postcss-js": "^3.0.1",
"prop-types": "^15.7.2",
"react": "^16.9.0",
"react-bs-notifier": "^5.0.0",
"react-datepicker": "^4.1.1",
"react-dnd": "^7.3.2",
"react-dnd-html5-backend": "^7.2.0",
"react-dom": "^16.9.0",
"react-dropzone": "^7.0.1",
"react-hook-form": "^4.9.6",
"react-hot-loader": "^4.12.18",
"react-infinite-scroll-component": "^4.2.0",
"react-jss": "^8.6.1",
"react-popper-tooltip": "^4.3.1",
"react-redux": "^7.1.3",
"react-router-dom": "^5.2.0",
"react-scrollbar": "^0.5.4",
"reactstrap": "^6.5.0",
"redux": "^4.0.4",
"redux-devtools-extension": "^2.13.8",
"redux-persist": "^6.0.0",
"redux-thunk": "^2.3.0",
"reselect": "^4.0.0",
"resolve-url-loader": "^3.1.2",
"semver": "^5.7.1",
"styled-components": "^5.3.0",
"typesafe-actions": "^5.1.0"
},
"devDependencies": {
"#babel/core": "^7.13.14",
"#babel/plugin-proposal-class-properties": "^7.13.0",
"#babel/plugin-proposal-optional-chaining": "^7.13.12",
"#babel/plugin-transform-runtime": "^7.13.10",
"#babel/preset-env": "^7.13.12",
"#babel/preset-react": "^7.13.13",
"#babel/preset-stage-0": "^7.8.3",
"#babel/preset-typescript": "^7.13.0",
"#testing-library/jest-dom": "^5.14.1",
"#testing-library/react": "^11.2.5",
"#tkaramanski/fmcomponents": "^1.5.1",
"#types/jest": "^26.0.22",
"#types/react": "^17.0.3",
"#types/react-datepicker": "^4.1.4",
"#types/react-dom": "^17.0.2",
"#types/react-lazyload": "^3.1.0",
"#types/react-redux": "^7.1.16",
"#types/react-router-dom": "^5.1.8",
"#types/styled-components": "^5.1.7",
"#typescript-eslint/eslint-plugin": "^4.17.0",
"autoprefixer": "^9.8.0",
"babel-eslint": "^10.1.0",
"babel-jest": "^26.6.3",
"babel-loader": "^8.2.2",
"babel-plugin-macros": "^3.0.1",
"babel-plugin-styled-components": "^1.12.0",
"babel-polyfill": "^6.26.0",
"babel-preset-es2015": "^6.24.1",
"babel-preset-react": "^6.24.1",
"clean-webpack-plugin": "^3.0.0",
"concurrently": "^5.2.0",
"copy-webpack-plugin": "^6.0.1",
"css-loader": "^3.6.0",
"enzyme": "^3.11.0",
"enzyme-adapter-react-16": "^1.15.4",
"eslint": "^7.10.0",
"eslint-config-airbnb": "^18.2.1",
"eslint-config-airbnb-typescript": "^12.3.1",
"eslint-plugin-import": "^2.23.4",
"eslint-plugin-jsx-a11y": "^6.3.1",
"eslint-plugin-react": "^7.21.3",
"eslint-plugin-react-hooks": "^4.2.0",
"extract-loader": "^5.1.0",
"file-loader": "^6.0.0",
"gulp": "^4.0.2",
"gulp-babel": "^8.0.0",
"gulp-cli": "^2.2.0",
"gulp-concat": "^2.6.1",
"gulp-sourcemaps": "^2.6.5",
"gulp-uglify": "^3.0.2",
"husky": "^4.3.0",
"identity-obj-proxy": "^3.0.0",
"jest": "^26.6.3",
"mini-css-extract-plugin": "^0.9.0",
"moment": "^2.26.0",
"postcss-loader": "^3.0.0",
"prettier": "^2.4.1",
"pump": "^3.0.0",
"react-copy-to-clipboard": "^5.0.2",
"react-test-renderer": "^16.13.1",
"react-tippy": "^1.4.0",
"redux-mock-store": "^1.5.4",
"renamer": "^1.1.4",
"sass": "^1.35.1",
"sass-loader": "^8.0.2",
"style-loader": "^1.2.1",
"ts-jest": "^26.5.4",
"ts-loader": "^8.1.0",
"typescript": "4.2.3",
"url-loader": "^4.1.0",
"webpack": "^4.43.0",
"webpack-cli": "^3.3.11",
"webpack-fix-style-only-entries": "^0.5.0",
"webpack-merge": "^4.2.2",
"wildcards-entry-webpack-plugin": "^2.1.0"
},
"husky": {
"hooks": {
"pre-commit": "npm run requirements-check",
"pre-push": "npm run lint"
}
},
"jest": {
"setupTestFrameworkScriptFile": "./setupTests.js",
"setupFiles": [
"./payment/src/__mocks__/data.js"
],
"collectCoverageFrom": [
"payment/src/__tests__/*.{js,jsx}",
"LPBlockLibrary/src/__testes__/*{ts,tsx}"
],
"moduleNameMapper": {
"\\.(png|jpg|gif|ttf|eot|svg)$": "<rootDir>/config/jest/fileMock.js"
},
"transform": {
"\\.tsx?$": "ts-jest",
"\\.jsx?$": "babel-jest"
}
}
}
here is my docker:
FROM node:14.17.0
WORKDIR /frontend-apps
ENV PATH /frontend-apps/node_modules/.bin:$PATH
COPY package.json \
package-lock.json \
.npmrc ./
RUN npm config set strict-ssl false
RUN npm install
RUN npm config set strict-ssl true
COPY . ./
EXPOSE 80
CMD [ "npm", "run", "dev-start"]
here is my docker-compose file.
version: '3.7'
services:
frontend-docker-app:
container_name: docker-front-apps
build:
context: .
dockerfile: Dockerfile.test
volumes:
- '.:/frontendApps'
- '/frontendApps/node_modules'
ports:
- 3000:3000
in docker file I try to run multiple commands with concurrently
"react-watch": "webpack --env.mode development --env.app react --watch",
"serve": "nodemon server.js",
"dev-start": "concurrently \"npm run react-watch\" \"npm run serve\"",
I solved my problem, it turned out that I accidentally remove my nodemon package from package.json, and I had bad docker compose config. I changed it to this version:
version: '3.7'
services:
frontend-docker-app:
container_name: docker-front-apps
working_dir: /var/www/app
build:
context: .
dockerfile: Dockerfile.test
volumes:
- .:/var/www/app
- '/frontendApps/node_modules'
tty: true
ports:
- 3000:3000
environment:
- CHOKIDAR_USEPOLLING=true
and changed cmd command from Docker file to this:
EXPOSE 80
CMD [ "npm", "run", "dev-build"]
and everything works fine.

Unable to deploy in ec2 Ubuntu MERN stack

I'm attempting to deploy my MERN stack on an ec2 Ubuntu instance. Everything worked until I installed my project dependencies. My project worked fine in my local but for some reason, I can't fix the dependency issue in the instance (prod):
sudo yarn build
yarn run v1.22.5
$ react-scripts build
Creating an optimized production build...
Failed to compile.
Failed to load plugin 'flowtype' declared in 'package.json ยป eslint-config-react-app': Cannot find module 'eslint/lib/rules/no-unused-expressions'
Require stack:
- /var/www/ubunturadiumlaw/node_modules/eslint-plugin-flowtype/dist/rules/noUnusedExpressions.js
- /var/www/ubunturadiumlaw/node_modules/eslint-plugin-flowtype/dist/index.js
- /var/www/ubunturadiumlaw/node_modules/#eslint/eslintrc/lib/config-array-factory.js
- /var/www/ubunturadiumlaw/node_modules/#eslint/eslintrc/lib/index.js
- /var/www/ubunturadiumlaw/node_modules/react-scripts/node_modules/eslint/lib/cli-engine/cli-engine.js
- /var/www/ubunturadiumlaw/node_modules/react-scripts/node_modules/eslint/lib/cli-engine/index.js
- /var/www/ubunturadiumlaw/node_modules/react-scripts/node_modules/eslint/lib/api.js
- /var/www/ubunturadiumlaw/node_modules/eslint-webpack-plugin/dist/getESLint.js
- /var/www/ubunturadiumlaw/node_modules/eslint-webpack-plugin/dist/linter.js
- /var/www/ubunturadiumlaw/node_modules/eslint-webpack-plugin/dist/index.js
- /var/www/ubunturadiumlaw/node_modules/eslint-webpack-plugin/dist/cjs.js
- /var/www/ubunturadiumlaw/node_modules/react-scripts/config/webpack.config.js
- /var/www/ubunturadiumlaw/node_modules/react-scripts/scripts/build.js
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
I tried to install the dependency individually, with npm, with yarn, and apt-get nothing worked.
Here's my package.json file:
"version": "0.1.0",
"private": true,
"dependencies": {
"#fortawesome/fontawesome-free": "^5.15.1",
"#popperjs/core": "^2.5.3",
"#testing-library/jest-dom": "^5.11.4",
"#testing-library/react": "^11.1.0",
"#testing-library/user-event": "^12.1.10",
"algoliasearch": "^4.5.1",
"algoliasearch-helper": "^3.2.2",
"axios": "^0.19.2",
"babel-eslint": "^10.1.0",
"babel-loader": "^8.1.0",
"bootstrap": "^4.5.3",
"brew": "0.0.8",
"compression": "^1.7.4",
"cors": "^2.8.5",
"crypto": "^1.0.1",
"eslint-config-react-app": "^6.0.0",
"eslint-plugin-react": "^7.21.5",
"express": "^4.17.1",
"file-system": "^2.2.2",
"flowtype": "^2.0.0",
"git": "^0.1.5",
"gridfs-stream": "^1.1.1",
"helmet": "^4.1.1",
"https-localhost": "^4.6.3",
"instantsearch.css": "^7.4.5",
"jquery": "^3.5.1",
"method-override": "^3.0.0",
"mongodb": "^3.5.9",
"mongoose": "^5.9.20",
"multer": "^1.4.2",
"multer-gridfs-storage": "^4.2.0",
"node-json-db": "^1.1.0",
"node-sass": "^4.14.1",
"nodemon": "^2.0.4",
"popper.js": "^1.16.1",
"react": "^17.0.1",
"react-autosuggest": "^10.0.2",
"react-bootstrap": "^1.3.0",
"react-create-app": "^2.0.6",
"react-dom": "^17.0.1",
"react-instantsearch-dom": "^6.7.0",
"react-router-dom": "^5.1.2",
"react-scripts": "4.0.0",
"react-tappable": "^1.0.4",
"react-toastify": "^6.0.4",
"rfs": "^9.0.3",
"serve": "^11.3.2",
"typescript": "^4.0.5",
"web-vitals": "^0.2.4",
"yarn": "^1.22.10"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
}, "eslintConfig": {
"extends": [
"react-app"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}
What is wrong?
Ec2 Ubuntu 20.04
Using Node.js as Server
React
Mongo

NodeJS - TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received undefined

I have a Node/React project in my Webstorm that won't run because of this error. I had to reinstall Windows and start fresh with my development. I got the code back into the IDE, but when I start up the Node server, I am getting the following error: TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received undefined
More context for that error:
[nodemon] starting `babel-node src/node-server/index.js`
internal/validators.js:122
throw new ERR_INVALID_ARG_TYPE(name, 'string', value);
^
TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received undefined
at validateString (internal/validators.js:122:11)
at Object.join (path.js:375:7)
at Object.<anonymous> (C:\Projects\Production-Orchestrator\src\node-server\/index.js:17:15)
I went to index.js and here is lines 1-17:
// npm run server
import dotenv from 'dotenv';
import express from 'express';
import path from 'path';
import sql from 'mssql';
import cors from 'cors';
import http from 'http';
import { setupWebSocket } from './ws/setupWebSocket.js';
const useWebSockets = true;
dotenv.config();
const dbConfig = {
user: process.env.DB_USER,
password: process.env.DB_PASS,
server: path.join(process.env.DB_SERVER, process.env.DB_HOST),
I am running npm run server to start up my node server.
And here is my package.json if it helps:
{
"name": "my-app",
"version": "0.1.0",
"private": true,
"description": "my app",
"homepage": ".",
"repository": {
"type": "git",
"url": "git+https://github.com/xxxxxxx"
},
"license": "UNLICENSED",
"scripts": {
"buildp": "env-cmd -f .env.production react-scripts build",
"buildt": "env-cmd -f .env.test react-scripts build",
"buildw": "webpack --config ./webpack.config.js --mode production",
"eject": "react-scripts eject",
"eslint-check": "eslint --print-config src/components/search/Search.js | eslint-config-prettier-check",
"server": "nodemon --exec babel-node src/node-server/index.js",
"start": "SET REACT_APP_WS_PORT=3001 & react-scripts start",
"startw": "webpack-dev-server --config ./webpack.config.js --mode development --open",
"stylelint": "stylelint **/*.scss",
"test": "react-scripts test",
"ws": "node --experimental-modules src/node-server/websocket.js",
"ws2": "nodemon --exec babel-node src/node-server/websocket.js"
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"dependencies": {
"#babel/preset-react": "^7.8.0",
"#emotion/core": "latest",
"#fortawesome/fontawesome": "^1.1.8",
"#fortawesome/fontawesome-svg-core": "^1.2.27",
"#fortawesome/free-regular-svg-icons": "^5.12.1",
"#fortawesome/free-solid-svg-icons": "^5.12.1",
"#fortawesome/react-fontawesome": "^0.1.8",
"#popperjs/core": "^2.0.6",
"#react-pdf/renderer": "^1.6.8",
"#types/react": "^16.9.19",
"animate.css": "^3.7.2",
"axios": "^0.19.2",
"babel-loader": "^8.0.6",
"bootstrap": "^4.4.1",
"bufferutil": "^4.0.1",
"cors": "^2.8.5",
"dayjs": "^1.8.20",
"device-detector-js": "^2.2.1",
"dotenv": "^8.2.0",
"express": "^4.17.1",
"express-ws": "^4.0.0",
"file-saver": "^2.0.2",
"javascript-time-ago": "latest",
"jquery": "^3.4.1",
"jsbarcode": "^3.11.0",
"mssql": "^6.1.0",
"popper.js": "^1.16.1",
"print-js": "^1.0.63",
"prop-types": "^15.7.2",
"react": "^16.12.0",
"react-animations": "^1.0.0",
"react-beautiful-dnd": "^12.2.0",
"react-beforeunload": "^2.2.1",
"react-collapse": "^5.0.1",
"react-confirm-alert": "^2.6.1",
"react-custom-scrollbars": "^4.2.1",
"react-detect-offline": "^2.4.0",
"react-dnd": "^10.0.2",
"react-dnd-html5-backend": "^10.0.2",
"react-dom": "^16.12.0",
"react-modal": "^3.11.1",
"react-notifications-component": "^2.3.0",
"react-popup": "^0.10.0",
"react-radio-group": "^3.0.3",
"react-router-dom": "^5.1.2",
"react-scripts": "^3.4.3",
"react-select": "^3.0.8",
"react-spinners": "^0.8.0",
"react-spring": "^8.0.27",
"react-time-ago": "^5.0.7",
"react-transition-group": "^4.3.0",
"sort-package-json": "^1.40.0",
"styled-components": "^4.4.1",
"typescript": "^3.7.5",
"utf-8-validate": "^5.0.2",
"ws": "^7.2.5"
},
"devDependencies": {
"#babel/cli": "^7.8.4",
"#babel/core": "^7.8.4",
"#babel/node": "^7.10.5",
"#babel/preset-env": "^7.8.4",
"#welldone-software/why-did-you-render": "^4.2.5",
"css-loader": "^3.4.2",
"dotenv-cli": "^3.2.0",
"env-cmd": "^10.1.0",
"eslint": "^6.8.0",
"eslint-config-prettier": "^6.10.0",
"eslint-loader": "^3.0.3",
"eslint-plugin-babel": "^5.3.0",
"eslint-plugin-prettier": "^3.1.2",
"file-loader": "^5.0.2",
"html-loader": "^0.5.5",
"html-webpack-plugin": "^3.2.0",
"node-sass": "^4.13.1",
"nodemon": "^2.0.4",
"prettier": "^1.19.1",
"react-hot-loader": "^4.12.19",
"sass-loader": "^8.0.2",
"stylelint": "^13.2.0",
"stylelint-config-rational-order": "^0.1.2",
"stylelint-config-standard": "^19.0.0",
"stylelint-order": "^4.0.0",
"stylelint-scss": "^3.14.2",
"url-loader": "^3.0.0",
"webpack": "^4.44.1",
"webpack-cli": "^3.3.11"
},
"proxy": "http://localhost:3000"
}
OK, I figured out the issue. I thought the error was telling me that path was undefined. When it fact it was saying the variables passed into path.join() were undefined. And that was because I forgot to add in my .env file to the root so it could grab those variables. Whoops!
In my case, the issue was the missing of an env variable. I did not have SECRET. So the SECRET env variable was missing and my test was trying to access it and as a result it was returning undefined
TypeError [ERR_INVALID_ARG_TYPE]: The "key" argument must be one of type string, TypedArray, or DataView. Received type undefined
For me was the Knex configuration file had a typo.
For testing I had
testing{
...
migrations: {
connection{
directory: './src/db/migrations',
tableName: 'knex_migrations',
}
},
...
}
when I needed
testing{
...
migrations: {
directory: './src/db/migrations',
tableName: 'knex_migrations',
},
...
}
My issue was that I added && after the env-cmd command, so that for my start script I got
"start": "env-cmd -e development && react-scripts start",
instead of
"start": "env-cmd -e development react-scripts start",
When I removed the && everything was fine.
This worked for me.
https://reactgo.com/typeerror-err-invalid-arg-type-react/
It says, that this error occurs when the react-scripts version is old.
So, delete all your current node modules, install the latest react-scripts using: 'npm install react-scripts#latest' and then install all the other dependencies.
I did the exact same thing, it worked.

Why npm run dev giving error even all dependencies are installed?

Problem:
Trying npm run dev to start the server but it's giving following error although exactly same code is working fine for other team members.
Error:
Error: Cannot find module 'braintree'
Require stack:
- C:\Users\Zainulabideen\Documents\GitHub\wine\dist\routes\api\braintree\clientToken.js
- C:\Users\Zainulabideen\Documents\GitHub\wine\dist\app.js
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:982:15)
at Function.Module._load (internal/modules/cjs/loader.js:864:27)
at Module.require (internal/modules/cjs/loader.js:1044:19)
at require (internal/modules/cjs/helpers.js:77:18)
at Object.<anonymous> (C:\Users\Zainulabideen\Documents\GitHub\wine\dist\routes\api\braintree\clientToken.js:5:17)
at Module._compile (internal/modules/cjs/loader.js:1158:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1178:10)
at Module.load (internal/modules/cjs/loader.js:1002:32)
at Function.Module._load (internal/modules/cjs/loader.js:901:14)
at Module.require (internal/modules/cjs/loader.js:1044:19) {
code: 'MODULE_NOT_FOUND',
requireStack: [
'C:\\Users\\Zainulabideen\\Documents\\GitHub\\wine\\dist\\routes\\api\\braintree\\clientToken.js',
'C:\\Users\\Zainulabideen\\Documents\\GitHub\\wine\\dist\\app.js'
]
}
[nodemon] app crashed - waiting for file changes before starting...
Relevant Code:
app.js
....
const brainTreeToken = require("./routes/api/braintree/clientToken");
app.get("/client_token", function (req, res) {
brainTreeToken.braintreeClient(req, res);
});
....
clientToken.js
var braintree = require("braintree");
import { useDispatch } from "react-redux";
const braintreeClient = (req, res) => {
var gateway = braintree.connect({
environment: braintree.Environment.Sandbox,
merchantId: "XXXXXXXXXXX",
publicKey: "XXXXXXXXXXXX",
privateKey: "XXXXXXXXXXX",
});
gateway.clientToken.generate({}, function(err, response) {
var clientToken = response.clientToken;
res.send(clientToken);
});
};
module.exports = {
braintreeClient: braintreeClient,
};
Package.json
{
"name": "XYZ",
"version": "0.0.10",
"private": true,
"description": "React APP",
"license": "ISC",
"author": "Temp",
"main": "index.js",
"scripts": {
"start": "node dist/app.js",
"start:prod": "npm i --production && npm run build && npm run server",
"dev": "echo Running App Under Watcher Nodemon Hot Deploy && nodemon dist/app.js",
"test": "jest --bail",
"test:watch": "jest --watch",
"build": "sh ./scripts/build-app.sh",
"build:dev": "SET NODE_ENV=development && npm run build",
"build:prod": "SET NODE_ENV=production npm run build",
"server": "node dist/app.js",
"server:dev": "SET NODE_ENV=development npm run server",
"server:prod": "SET NODE_ENV=production npm run server",
"docker": "sh ./scripts/build-docker.sh",
"webpack": "rimraf ./build && webpack --config tools/webpack.config.js",
"lint": "eslint src/**/*.js",
"hotstart": "nohup node src/server.js > nohup.out 2> nohup.err < /dev/null & react-scripts start"
},
"eslintConfig": {
"extends": "react-app"
},
"dependencies": {
"awesome-notifications": "^3.0.3",
"babel-eslint": "^10.0.2",
"body-parser": "^1.19.0",
"bootstrap": "^4.3.1",
"console-stamp": "^0.2.9",
"cookie-parser": "~1.4.3",
"cors": "^2.8.5",
"debug": "~2.6.9",
"eslint": "^6.0.1",
"eslint-config-react-app": "^4.0.1",
"eslint-loader": "^2.2.1",
"eslint-plugin-flowtype": "^3.11.1",
"eslint-plugin-import": "^2.18.0",
"eslint-plugin-jsx-a11y": "^6.2.3",
"eslint-plugin-react": "^7.14.2",
"express": "^4.17.1",
"express-session": "^1.16.2",
"fbemitter": "^2.1.1",
"font-awesome": "^4.7.0",
"http-errors": "~1.6.2",
"i18next": "^19.0.1",
"i18next-browser-languagedetector": "^4.0.1",
"i18next-xhr-backend": "^3.2.2",
"moment": "^2.24.0",
"morgan": "^1.9.1",
"nodemon": "^1.19.1",
"path": "^0.12.7",
"pem": "^1.14.2",
"prop-types": "^15.7.2",
"proxy": "^1.0.1",
"react": "^16.12.0",
"react-bootstrap": "^1.0.0-beta.12",
"react-datepicker": "^2.9.6",
"react-dev-utils": "^9.0.1",
"react-dom": "^16.12.0",
"react-flexbox-grid": "^2.1.2",
"react-hooks-paginator": "^0.4.0",
"react-i18next": "^9.0.10",
"react-iframe": "^1.8.0",
"react-js-pagination": "^3.0.2",
"react-notifications-component": "^2.0.7",
"react-redux": "^7.1.1",
"react-router": "^5.0.1",
"react-router-dom": "^5.0.1",
"react-router-redux": "^4.0.8",
"react-scripts": "^3.3.1",
"react-select": "^3.0.8",
"react-spinners": "^0.6.1",
"react-switch": "^5.0.1",
"react-table": "^6.10.0",
"react-toastify": "^5.4.1",
"reactstrap": "^8.4.0",
"redux": "^4.0.4",
"redux-saga": "^1.1.1",
"request": "^2.88.2",
"simple-react-validator": "^1.2.2",
"socket.io": "^2.2.0",
"underscore": "^1.9.2"
},
"devDependencies": {
"babel-cli": "^6.26.0",
"babel-core": "^6.26.3",
"babel-loader": "^7.1.5",
"babel-plugin-transform-class-properties": "^6.24.1",
"babel-plugin-transform-object-rest-spread": "^6.26.0",
"babel-plugin-transform-runtime": "^6.23.0",
"babel-preset-env": "^1.7.0",
"babel-preset-es2015": "^6.24.1",
"babel-preset-react": "^6.24.1",
"babel-preset-stage-0": "^6.24.1",
"braintree-web": "^3.58.0",
"css-loader": "^3.2.0",
"eslint": "^6.5.0",
"eslint-config-prettier": "^6.3.0",
"eslint-config-react-app": "^4.0.1",
"eslint-loader": "^2.2.1",
"eslint-plugin-flowtype": "^3.11.1",
"eslint-plugin-import": "^2.18.2",
"eslint-plugin-jsx-a11y": "^6.2.3",
"eslint-plugin-prettier": "^3.1.0",
"eslint-plugin-react": "^7.14.3",
"eslint-plugin-react-hooks": "^1.7.0",
"extract-text-webpack-plugin": "^3.0.2",
"file-loader": "^4.3.0",
"html-webpack-plugin": "^3.2.0",
"jest": "^24.8.0",
"mini-css-extract-plugin": "^0.7.0",
"node-sass": "^4.13.1",
"paypal-checkout": "^4.0.311",
"prettier": "^1.18.2",
"puppeteer": "^1.17.0",
"rimraf": "^2.7.1",
"sass-loader": "^7.1.0",
"url-loader": "^2.0.0",
"webpack": "^4.34.0",
"webpack-cli": "^3.3.6",
"webpack-dev-server": "^3.7.1"
},
"nodemonConfig": {
"ext": "js, html, scss",
"ignore": [
"node_modules/*",
"build/*",
"dist/*",
"scripts/*",
".git"
],
"exec": "sh ./scripts/build-watch.sh"
}
}
Operating System and Terminal:
Windows 10, git bash
Approaches tried:
1) npm install
2) npm install --save-dev
3) Deleted node_modules folder again tried npm install
4) Deleted package-lock.json, node_modules folder and again tried npm install
5) Checked if particular is present in node_modules, it's present.
6) Using git-bash terminal, tried switching to cygwin terminal.
7) As using Windows, tried restarting machine. (Pardon, if it hurted you :( )
8) Verified folder structure, it's fine.
Note:
Codebase was working fine for before talking pull, after pull it's creating problem but it's working completely fine for other team members even after pull.
I don't see braintree in package.json, it is working in your team member's pc because they maybe installed it separately. Sometimes I tend to forget adding it to package.json when I develop a feature because it takes experimenting with bunch of packages so the package is in my node_modules folder already. Also there is a possibility that someone forgot to git add the package.json.
Anyway, To add to your package.json do and push,
npm i braintree --save
and push it after git add package.json

Resources