Trying to dockerize a node.js file but keep getting error - node.js

FROM node:7
WORKDIR ~/Desktop/CS612
COPY package.json ~/Desktop/CS612
RUN npm install
COPY . ~/Desktop/CS612
CMD node server.js
EXPOSE 3000
Okay I have switched it and was able to get this far:
Step 5/7 : COPY . ~/Desktop/CS612/
---> 885080c48872
Step 6/7 : CMD node server.js
---> Running in 7ffbaeec889f
---> 61654068c183
Removing intermediate container 7ffbaeec889f
Step 7/7 : EXPOSE 3000
---> Running in 6862095ac871
---> abb84902c53b
Removing intermediate container 6862095ac871
Successfully built abb84902c53b
Successfully tagged restaurants:latest
Danas-MacBook-Air:CS612 DanaCarlin$ docker run restaurants
module.js:538
throw err;
^
Error: Cannot find module '/~/Desktop/CS612/server.js'
at Function.Module._resolveFilename (module.js:536:15)
at Function.Module._load (module.js:466:25)
at Function.Module.runMain (module.js:676:10)
at startup (bootstrap_node.js:187:16)
at bootstrap_node.js:608:3
Why am I getting this error now? It makes no sense, that is definitely a file that holds the requests and responses

WORKDIR ~/Desktop/CS612
WORKDIR specifies the working directory inside the container, not the directory on Danas-MacBook-Air. The host working directory is closer to what Docker calls the build context on your MacBook air.
Also, Docker needs absolute paths within the container. You're creating a directory in the container /~/Desktop/CS612, which is where all the subsequent Dockerfile commands will run from as you finish the build. Probably not what you want.
tl;dr ditch the relative paths (~/) in the Dockerfile. Example: COPY . /Desktop/CS612.
edit: to reflect changes in the original question

Related

npm run start won't find node_modules folder on different OS aside Windows

I made a node JS application using Hapi on Windows 10. After testing it locally, the script start would run without any problem. here is the start script inside the package.json
"scripts": {
"start": "nodemon -e * ./src/server.js"
}
I am trying to deploy this app inside of Centos 7. After cloning from github, set up the postgreSQL DB & Table, set up the .env file, when I run the npm run start command, the console would pop up an error like so (error taken from Ubuntu instance on AWS, but the error are the same across every linux based OS)
[nodemon] starting `node node_modules nodemon.json package-lock.json package.json readme.MD src views /src/server.js`
internal/modules/cjs/loader.js:1032
throw err;
^
Error: Cannot find module '/home/ubuntu/application_nodejs/node_modules'
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:1029:15)
at Function.Module._load (internal/modules/cjs/loader.js:898:27)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
at internal/main/run_main_module.js:17:47 {
code: 'MODULE_NOT_FOUND',
requireStack: []
}
[nodemon] app crashed - waiting for file changes before starting...
I tried to replicate the issue on Heroku, AWS EC2 running on Ubuntu & AMI 2, and my other Windows 10 PC. on Windows 10, the app would run with ease after running said command. On every linux based OS, it would return the same error. When I tried to run it with node ./src/server.js, the app would launch.
Is there any solution to my problem? I suspect it has something to do with file/folder naming or the start script itself. But I haven't found any correct solution. Thank you in advance!
You need to quote the *: nodemon -e "*" src/server.js.
Unlike Windows' cmd, Linux shells expand wildcards (as you can see in the command actually run, above the error). In Windows it's up to the program you are calling to expand wildcards. Since that is what you want in case of nodemon, it worked "by chance" on Windows without escaping the asterisk because it doesn't have any special meaning to cmd, but in Linux it will get expanded and that's not what you want.

NodeJS App 'Module Not Found' when run from Docker

I'm currently in the process of containerizing my App. Part of the App is a NodeJS Express Backend.
It works without any issues when I run it on my console with node index.js or nodemon index.js.
Inside the middlewares/index.js I do import authJwt.js and verifySignup.js via require('./verifySignup) and require('./authJwt).
This works unsurprisingly well when I run my app from the console. However If i build my docker-image using the following Dockerfile-Code:
# build environment
FROM node:14
# Working Directoy
WORKDIR /app
ENV FILE_UPLOADS="./files/uploads"
ENV FILE_UPLOADS_PART="./files/uploads_part"
ENV PORT=3001
ENV DB_HOST="localhost"
ENV DB_USER="root"
ENV DB_PASSDWORD="root_password"
ENV DB_NAME="upload_db"
ENV SECRET_KEY="bezkoder-secret-key"
# Copy Needed Files for Dependencies
COPY package.json /app/package.json
COPY yarn.lock /app/yarn.lock
# Install with YARN
RUN yarn install
# Copy Source Files
COPY ./ /app
EXPOSE 3001
CMD [ "yarn", "start" ]
When I run the Image it immediately terminates. Looking at the logs I find the following error:
$ docker logs 031241ce227a
yarn run v1.22.5
$ node index.js
internal/modules/cjs/loader.js:895
throw err;
^
Error: Cannot find module './verifySignUp'
Require stack:
- /app/middleware/middleware.js
- /app/routes/auth.routes.js
- /app/index.js
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:892:15)
at Function.Module._load (internal/modules/cjs/loader.js:742:27)
at Module.require (internal/modules/cjs/loader.js:964:19)
at require (internal/modules/cjs/helpers.js:88:18)
at Object.<anonymous> (/app/middleware/middleware.js:2:22)
at Module._compile (internal/modules/cjs/loader.js:1075:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1096:10)
at Module.load (internal/modules/cjs/loader.js:940:32)
at Function.Module._load (internal/modules/cjs/loader.js:781:14)
at Module.require (internal/modules/cjs/loader.js:964:19) {
code: 'MODULE_NOT_FOUND',
requireStack: [
'/app/middleware/middleware.js',
'/app/routes/auth.routes.js',
'/app/index.js'
]
}
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
I then exported my docker-image using docker export adoring_kowalevski > contents.tar to look at the filesystem inside the image, but everything is where it should be.
In a desperate attempt I hoped, that the issue might occur because I have multiple index.js in different folders, so I changed the names of all except the one in the root directory, but issue persisted. This is why in the error-log there is a middleware/middleware.js, it was after renaming the middleware/index.js.
Edit:
I tried using rfr to change my references from require('../middleware') to rfr('middleware'). It still workds from console but still not inside the container. So it seems that the issue doesn't occur because of the relative file paths.
Turns out I'm stupid (somewhat).
The import inside middleware/index.js looked like this require('./verifySignUp').
If we look at the file verifySignup.js inside the middleware Folder we can see, that it is named without the capital 'U'.
Why did it still work from console?
Inside verifySignup.js is the following piece of code:
module.exports = {
authJwt,
verifySignUp
};
Here it is written with capital 'U'. This means that running node index.js from the console only cared about whatever was written in the module.exports and not the actual Filename, however when running inside Docker, the Filename matters.
Renaming the file from verifySignup.js to verifySignUp.js solved my problems.

NodeJS App path broken in Docker container

I got this strange issue when trying to run my NodeJS App inside a Docker Container.
All the path are broken
e.g. :
const myLocalLib = require('./lib/locallib.');
Results in Error:
Cannot find module './lib/locallib'
All the file are here (show by ls command inside the lib direcotry)
I'm new to Docker so I may have missed something in my setup
There's my Dockerfile
FROM node:latest
COPY out/* out/
COPY src/.env.example src/
COPY package.json .
RUN yarn
ENTRYPOINT yarn start
Per request : File structure
Thank you.
You are using the COPY command wrong. It should be:
COPY out out

Heroku Deploy Error: Cannot find module '/app/index.js'

I' m trying to deploy mt app on Heroku but I always get the same error:
2016-08-18T10:16:10.988982+00:00 heroku[web.1]: Starting process with command `node index.js`
2016-08-18T10:16:13.180369+00:00 app[web.1]: module.js:341
2016-08-18T10:16:13.180389+00:00 app[web.1]: throw err;
2016-08-18T10:16:13.180390+00:00 app[web.1]: ^
2016-08-18T10:16:13.180391+00:00 app[web.1]:
2016-08-18T10:16:13.180392+00:00 app[web.1]: Error: Cannot find module '/app/index.js'
2016-08-18T10:16:13.180393+00:00 app[web.1]: at Function.Module._resolveFilename (module.js:339:15)
2016-08-18T10:16:13.180394+00:00 app[web.1]: at Function.Module._load (module.js:290:25)
2016-08-18T10:16:13.180394+00:00 app[web.1]: at Function.Module.runMain (module.js:447:10)
2016-08-18T10:16:13.180399+00:00 app[web.1]: at node.js:405:3
2016-08-18T10:16:13.271966+00:00 heroku[web.1]: Process exited with status 1
2016-08-18T10:16:13.273383+00:00 heroku[web.1]: State changed from starting to crashed
As I read in similar requests I have already added a Procfile containing the following code: web: node index.js, but I still have same issue.
Anybody have any idea where the problem is? Any guidance would be greatly appreciated.
Thank you in advance!
Is your index.js file in your root directory?
web: node ./index.js
Your file might be nested like so app/src/index.js
web: node ./src/index.js
Does your index.js have an uppercase 'I'? It has to be index.js and not Index.js
If you do have your index.js file at the root of your project, but heroku's error says that the module cannot be found. Then, the problem you are having might be due to GIT.
How can we make sure this is the case?
Well, your git repo might not be adding your index.js file to commits nor pushing it to heroku. You can verify this by looking at the files that git is watching in your local repo with the following command:
git ls-files
Your index.js file should be listed. If not, then your file is being ignored.
Solution: Force add your file.
git add --force ./index.js
Now you can commit and push to heroku and you should be up and running.
This might also be the case when having your index file inside a dist directory or src (app/dist/index.js or app/src/index.js).
Add relative path for index.js file as bellow
web: node ./index.js
I am getting this error also. then I solve it by fixing importing file. My folder was was in lowercase like the product, and I import it like Product. It doesn't give me errors in localhost. Please check if the import files path is right in the index file.
My issue was a fault with my local development environment. Somehow...JavaScript code was being injected into my app project, which I didn't do.
Not knowing this was occurring, Heroku started seeing errors during my push, like "can't find jQuery, even though your using it in your app". I was like "what????, I'm not even using jQuery." I then re-opened a .js file in my project...and there it was, a const variable declaring jquery. So I say all that say, check your VSCode extensions, your third party npm packages, and everything for that matter to make sure things like this are not happening to you.
If error still happening, try using this:
web: node .
Go to your package.json file in root directory of any sub directory with another instance.
Change the script tag from "start": "node start" to "start": "node app.js".
Please note: in my case my main file is app.js yours can be index.js or server.js or anything you named it.
it can also happen if you give space in folders name or create your files in multiple folders then it will work in localhost but in server it will give you application error so don't make multiple folders and as well as don't give space in them and specify path correctly then it will work fine.

Swagger express gives error inside docker container

I have an app that uses the swagger-express-mw library and I start my app the following way:
SwaggerExpress.create({ appRoot: __dirname }, (err, swaggerExpress) => {
// initialize application, connect to db etc...
}
Everything works fine on my local OSX machine. But when I use boot2docker to build an image from my app and run it, I get the following error:
/usr/src/app/node_modules/swagger-node-runner/index.js:154
config.swagger = _.defaults(readEnvConfig(),
^
TypeError: Cannot assign to read only property 'swagger' of [object Object]
My dockerfile looks like this (nothing special):
FROM node:latest
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
COPY package.json /usr/src/app/
RUN npm set progress=false
RUN npm install
COPY . /usr/src/app
EXPOSE 5000
CMD ["npm", "run", "dev"]
Has anyone else encountered similar situations where the local environment worked but the app threw an error inside the docker container?
Thanks!
Your issue doesn't appear to be something wrong with your docker container or machine setup. The error is not a docker error, that is a JavaScript error.
The docker container appears to be running your JavaScript module in Strict Mode, in which you cannot assign to read-only object properties (https://msdn.microsoft.com/library/br230269%28v=vs.94%29.aspx). On your OSX host, from the limited information we have, it looks like it is not running in strict mode.
There are several ways to specify the "strictness" of your scripts. I've seen that you can start Node.js with a flag --use_strict, but I'm not sure how dependable that is. It could be that NPM installed a different version of your dependent modules, and in the different version they specify different rules for strict mode. There are several other ways to define the "strictness" of your function, module, or app as a whole.
You can test for strict mode by using suggestions here: Is there any way to check if strict mode is enforced?.
So, in summary: your issue is not inheritantly a docker issue, but it is an issue with the fact that your javascript environments are running in different strict modes. How you fix that will depend on where strict is being defined.
Hope that helps!

Resources