Getting below error after deploying docker image:
Cannot find module 'Sequelize'
Directory structure:
src
views
Dockerfile
package.json
Dockerfile:
FROM node:8
ENV PORT 3000
EXPOSE 3000
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
COPY package.json /usr/src/app
RUN npm install
COPY . /usr/src/app
CMD ["npm", "run", "prod"]
Package.json:
{
"name": "api",
"version": "0.0.0",
"private": true,
"main": "src/loader.js",
"scripts": {
"dev": "nodemon",
"prod": "node src/loader.js"
},
"dependencies": {
"sequelize": "^4.38.0",
"cookie-parser": "~1.4.3",
"cors": "^2.8.4",
"debug": "~2.6.9",
"express": "~4.16.0",
"express-graphql": "^0.6.12",
"graphql": "^0.13.2",
"graphql-relay": "^0.5.5",
"graphql-sequelize": "^9.0.1",
"http-errors": "~1.6.2",
"jade": "^1.11.0",
"morgan": "~1.9.0",
"mysql2": "^1.6.1"
},
"devDependencies": {
"nodemon": "^1.18.3"
}
}
Could it be a typo, ie can you verify that you are using require('sequelize') instead of require('Sequelize').
Maybe you need to explicitly add the module in your package.json
$ npm install --save sequelize
If the module is declared, it will be found by the dependent module that is causing you that error.
Hope it helps!
Related
I have a react + express project where I do server side rendering. My folder structure is like this
root
client
build
node_modules
public
src
package.json
node_modules
routes
server.js
package.json
I have two Heroku dynos app-dev and app-prod and I use app-dev for development and app-prod for production and their environments have been set to dev and prod accordingly. My package.json looks like this
{
"name": "name",
"version": "1.0.0",
"description": "",
"main": "server.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node server",
"server": "nodemon server",
"client": "npm start --prefix client",
"build": "cd client/ && npm install && npm run build",
"dev": "concurrently \"npm run server\" \"npm run client\""
},
"author": "",
"license": "ISC",
"dependencies": {
"body-parser": "^1.18.3",
"chalk": "^4.1.1",
"concurrently": "^6.1.0",
"connect-mongo": "^3.2.0",
"cookie-parser": "^1.4.3",
"cookie-session": "^2.0.0-beta.3",
"cors": "^2.8.5",
"dotenv": "^10.0.0",
"express": "^4.16.4",
"express-session": "^1.15.6",
"express-validator": "^6.11.1",
"mongoose": "^5.4.1",
"mongoose-findorcreate": "^3.0.0",
"morgan": "^1.10.0",
"passport": "^0.4.0",
"passport-google-oauth": "^2.0.0",
"passport-google-oauth20": "^2.0.0",
"passport-twitter": "^1.0.4",
"redux-thunk": "^2.3.0",
"http-proxy-middleware": "^2.0.0"
}
}
Now I have got a constants file in my client/src folder that had these
// Constants.js
const prod = {
url: {
SS_API_URL: 'https://prod.example.com',
},
keys:{
SS_CLIENT_ID: 'prodClientId',
SS_X_API_KEY: 'prodApiKey',
}
};
const dev = {
url: {
SS_API_URL: 'https://dev.example.com',
},
keys:{
SS_CLIENT_ID: 'devClientId',
SS_X_API_KEY: 'devApiKey',
}
};
export const config = process.env.NODE_ENV === 'development' ? dev : prod;
Now when I upload to Heroku (app-dev) and it builds it, after that it seems to be using the prod.example.com instead of dev.example.com API endpoint. Since the NODE_ENV is development I thought it would take the SS_API_URL for dev but it is taking the SS_API_URL for prod
You can add this variable on settings menu, "Config Vars", inside your heroku project. So you add NODE_ENV and value "production".
I have a working nodejs app that uses a MongoDb cluster on the cloud. I am trying to dockerize it using docker-compose up, but I keep getting this error:
nodejs |import Mongodb from 'mongodb';
nodejs | ^^^^^^
nodejs | SyntaxError: Unexpected token import
Here is my dockerFile:
FROM node:12.16.1 as build
WORKDIR /app
COPY . /app
RUN npm install
RUN npm run build
COPY . .
EXPOSE 80
CMD ["node", "app.js", "daemon off;"]
Here is my docker-compose.yml:
version: '3'
services:
nodejs:
build:
context: .
dockerfile: Dockerfile
image: nodejs
container_name: nodejs
restart: unless-stopped
env_file: .env
volumes:
- node_modules:/node_modules
command: node app.js
volumes:
node_modules:
Here is my package.JSON file:
{
"type": "module",
"name": "krakenchallenge",
"version": "1.0.0",
"description": "A Node.js app that will filter out valid transactions from given JSON files containging a list of transactions returned from running bitcoind’s rpc call `listsinceblock`. The app is configured to allow working with both SQL or mongoDB types.",
"main": "index.js",
"repository": {
"type": "git",
"url": "git+https://github.com/abuaesh/KrakenChallenge.git"
},
"keywords": [
"node",
"js",
"mongodb",
"sql",
"nosql",
"bitcoin",
"transaction",
"deposit"
],
"author": "Noha Abuaesh <noha.abuaesh#gmail.com>",
"license": "MIT",
"bugs": {
"url": "https://github.com/abuaesh/KrakenChallenge/issues"
},
"homepage": "https://github.com/abuaesh/KrakenChallenge#readme",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"dependencies": {
"#babel/cli": "^7.0.0-beta.46",
"#babel/core": "^7.10.4",
"#babel/plugin-proposal-class-properties": "^7.0.0-beta.46",
"#babel/plugin-proposal-object-rest-spread": "^7.0.0-beta.46",
"#babel/preset-env": "^7.10.4",
"babel-core": "^6.26.3",
"babel-loader": "^8.1.0",
"babel-polyfill": "^6.26.0",
"babel-preset-env": "^1.7.0",
"babel-preset-es2015": "^6.24.1",
"babel-preset-stage-0": "^6.24.1",
"bitcoin-address-validation": "^1.0.2",
"dotenv": "^8.2.0",
"fs": "0.0.1-security",
"mongodb": "^3.6.0",
"wallet-address-validator": "^0.2.4"
},
"devDependencies": {
"nodemon": "^1.18.10"
}
}
The import statement is supported from version 13.2.0 Browser compatibility Table
Or From version 12.0.0, Users must explicitly enable this, using the --experimental-modules runtime flag.
Make sure to rebuild your image. docker-compose up --build will build your images before starting the containers.
Learning node with express, downloaded and currently studing a structure template, but forget how to start the nodemon so that any update is automatically applied. I was given the command from where I got the template but lost the locaion and after readying up on some of the commands of npm, node,... I could not come up with how2. Thx
Currnet command to run the app is:
npm start
//package.json
{
"name": "eautoshop",
"version": "0.0.0",
"private": true,
"scripts": {
"start": "node ./bin/www",
"devstart": "nodemon ./bin/www"
},
"dependencies": {
"cookie-parser": "~1.4.4",
"debug": "~2.6.9",
"express": "~4.16.1",
"express-validator": "^6.2.0",
"http-errors": "~1.6.3",
"morgan": "~1.9.1",
"mysql": "^2.17.1",
"pug": "^2.0.4"
},
"devDependencies": {
"nodemon": "^1.19.4"
}
}
nodemon app
gives the error:
'nodemon' is not recognized as an internal or external command,
operable program or batch file.
You can just point the nodemon executable...
node node_modules/nodemon/bin/nodemon.js bin/www
I have an application using React and Node.js. I'm struggling to deploy this to Heroku as I need to use Babel to transpile the code. Can someone help with setting the scripts I need to run?
I'm currently struggling to understand how to run client and server correctly as I'm getting an error regarding module location:
Error: Cannot find module '/app/build/index.js'
This is my package.json
{
"main": "server.js",
"scripts": {
"server": "nodemon server/server.js --ignore './client/' --exec babel-node -e js",
"client": "npm start --prefix client",
"dev": "concurrently \"npm run server\" \"npm run client\"",
"clean": "rm -rf build && mkdir build",
"build-babel": "babel -d ./build ./server/server.js -s",
"build": "npm run clean && npm run build-babel",
"start": "npm run build && node ./build/index.js",
"heroku-postbuild": "npm install --prefix client && npm run build --prefix client"
},
"dependencies": {
"bcryptjs": "^2.4.3",
"body-parser": "^1.18.3",
"concurrently": "^4.1.0",
"express": "^4.16.4",
"google-play-scraper": "^6.2.8",
"googleapis": "^39.2.0",
"is-empty": "^1.2.0",
"jsonwebtoken": "^8.5.1",
"mongoose": "^5.4.19",
"mongoose-paginate-v2": "^1.0.24",
"multer": "^1.4.1",
"node-fetch": "^2.3.0",
"passport": "^0.4.0",
"passport-jwt": "^4.0.0",
"validator": "^10.11.0",
"winston": "^3.2.1",
"xml2js": "^0.4.19",
"#babel/core": "^7.3.4",
"#babel/node": "^7.2.2",
"babel-cli": "^6.26.0",
"babel-loader": "^8.0.5",
"babel-preset-es2015": "^6.24.1",
"babel-preset-stage-0": "^6.24.1"
},
"devDependencies": {
"eslint": "^5.12.0",
"eslint-config-airbnb-base": "^13.1.0",
"eslint-plugin-import": "^2.17.2",
"nodemon": "^1.18.10"
}
}
And the file structure of the important files:
node_modules
.babelrc
babel.config.js
package-lock.json
package.json
client
--package-lock.json
--package.json
node_modules
--src
----index.js
server
--server.js
I assume I'm setting up the scripts incorrectly.
My problem was with some of the scripts. This is the final package.json that got it all working.
{
"main": "server.js",
"scripts": {
"server": "nodemon server/server.js --ignore './client/' --exec babel-node -e js",
"client": "npm start --prefix client",
"dev": "concurrently \"npm run server\" \"npm run client\"",
"start": "npm run build && node ./build/server.js",
"build": "npm run clean && npm run build-babel",
"clean": "rm -rf ./build && mkdir ./build",
"build-babel": "babel -d ./build ./server -s",
"heroku-postbuild": "npm install --prefix client && npm run build --prefix client"
},
"dependencies": {
"#babel/core": "^7.3.4",
"#babel/node": "^7.2.2",
"babel-cli": "^6.26.0",
"babel-loader": "^8.0.5",
"babel-preset-es2015": "^6.24.1",
"babel-preset-stage-0": "^6.24.1",
"bcryptjs": "^2.4.3",
"body-parser": "^1.18.3",
"concurrently": "^4.1.0",
"express": "^4.16.4",
"google-play-scraper": "^7.0.2",
"googleapis": "^39.2.0",
"is-empty": "^1.2.0",
"jsonwebtoken": "^8.5.1",
"mongoose": "^5.4.19",
"mongoose-paginate-v2": "^1.0.24",
"multer": "^1.4.1",
"node-fetch": "^2.3.0",
"passport": "^0.4.0",
"passport-jwt": "^4.0.0",
"validator": "^10.11.0",
"winston": "^3.2.1",
"xml2js": "^0.4.19"
},
"devDependencies": {
"eslint": "^5.12.0",
"eslint-config-airbnb-base": "^13.1.0",
"eslint-plugin-import": "^2.17.2",
"nodemon": "^1.18.10"
}
}
When I try to start my web application with Nodejs only, I've no any problem and my web application works fine.
But when I try to simulate in my laptop the start in production, namely, the static pages will be served by webpack server and the data from the API will be served by Nodejs. I've got this error:
/home/josecarlos/Workspace/nodejs/basketmetrics3/node_modules/convert-source-map/index.js:61
return SafeBuffer.Buffer.from(json, 'utf8').toString('base64');
^
TypeError: Cannot read property 'from' of undefined
at Converter.toBase64 (/home/josecarlos/Workspace/nodejs/basketmetrics3/node_modules/convert-source-map/index.js:61:28)
at Converter.toComment (/home/josecarlos/Workspace/nodejs/basketmetrics3/node_modules/convert-source-map/index.js:65:21)
at generateCode (/home/josecarlos/Workspace/nodejs/basketmetrics3/node_modules/#babel/core/lib/transformation/file/generate.js:78:76)
at runSync (/home/josecarlos/Workspace/nodejs/basketmetrics3/node_modules/#babel/core/lib/transformation/index.js:50:51)
at transformSync (/home/josecarlos/Workspace/nodejs/basketmetrics3/node_modules/#babel/core/lib/transform.js:43:38)
at Object.transform (/home/josecarlos/Workspace/nodejs/basketmetrics3/node_modules/#babel/core/lib/transform.js:22:38)
at compile (/home/josecarlos/Workspace/nodejs/basketmetrics3/node_modules/#babel/register/lib/node.js:73:20)
at compileHook (/home/josecarlos/Workspace/nodejs/basketmetrics3/node_modules/#babel/register/lib/node.js:102:12)
at Module._compile (/home/josecarlos/Workspace/nodejs/basketmetrics3/node_modules/pirates/lib/index.js:93:29)
at Module._extensions..js (internal/modules/cjs/loader.js:827:10)
To start my project I use this command npm run start
These are my config files:
package.json
{
"name": "basketmetrics3",
"version": "1.0.0",
"description": "Basketball advanced stats",
"main": "server.js",
"scripts": {
"dev-webpack": "webpack-dev-server --hot --mode development",
"clean": "rm -rf ./dist",
"dev": "npm run clean && npm run compile-dev && cross-env NODE_ENV=development nodemon --exec babel-node src/server/server.js --ignore ./src/client",
"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",
"start": "npm run clean && npm run compile && cross-env NODE_ENV=production babel-node src/server/server.js --ignore ./src/client"
},
"license": "ISC",
"dependencies": {
"#babel/polyfill": "^7.4.4",
"#material-ui/core": "^3.9.3",
"#material-ui/icons": "^3.0.2",
"bootstrap": "^4.3.1",
"cors": "^2.8.5",
"create-react-app": "^3.0.1",
"cross-env": "^5.2.0",
"d3": "^5.9.7",
"dotenv": "^7.0.0",
"express": "^4.17.1",
"express-graphql": "^0.7.1",
"graphql": "^14.4.2",
"jquery": "^3.4.1",
"morgan": "^1.9.1",
"mysql2": "^1.6.5",
"popper.js": "^1.15.0",
"react": "^16.8.6",
"react-bootstrap": "^1.0.0-beta.10",
"react-bootstrap-table-next": "^3.1.7",
"react-bootstrap-table2-paginator": "^2.0.7",
"react-dom": "^16.8.6",
"react-multi-language": "^0.4.2",
"react-router-dom": "^5.0.1",
"react-simple-tooltip": "^2.6.1",
"sequelize": "^4.44.2",
"validator": "^10.11.0"
},
"devDependencies": {
"#babel/cli": "^7.5.5",
"#babel/core": "^7.5.5",
"#babel/node": "^7.5.5",
"#babel/preset-env": "^7.5.5",
"#babel/preset-react": "^7.0.0",
"babel-loader": "^8.0.6",
"babel-plugin-transform-class-properties": "^6.24.1",
"babel-register": "^6.26.0",
"css-loader": "^2.1.1",
"file-loader": "^3.0.1",
"html-webpack-plugin": "^3.2.0",
"mini-css-extract-plugin": "^0.5.0",
"nodemon": "^1.19.1",
"path": "^0.12.7",
"style-loader": "^0.23.1",
"url-loader": "^1.1.2",
"webpack": "^4.38.0",
"webpack-cli": "^3.3.6",
"webpack-dev-server": "^3.7.2"
}
}
.babelrc
{
"presets": [
["#babel/preset-env" , {"modules": "cjs"}],
"#babel/preset-react"
],
"plugins": [
"transform-class-properties"
],
"ignore": [
"./node_modules"
]
}
I have been looking for information about my problem and I have found that I have to add "ignore: [node_modules]" to my .babelrc file. But, after I have added this entry in my .babelrc file, my application still doesn't work :(
Is there any other way to ignore "node_modules" with node.js? How can I ignore "node_modules"? If this is the real reason of my error, of course.
What can I do to fix the error that I've got?
I have found a solution to my problem. Changing the start script adding the ignore of "node_modules" is enough.
My "start" script in package.json will be:
"scripts": {
"dev-webpack": "webpack-dev-server --hot --mode development",
"clean": "rm -rf ./dist",
"dev": "npm run clean && npm run compile-dev && cross-env NODE_ENV=development nodemon --exec babel-node src/server/server.js --ignore ./src/client",
"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",
"start": "npm run clean && npm run compile && cross-env NODE_ENV=production babel-node src/server/server.js --ignore ./node_modules"
},