Skip to content

Instantly share code, notes, and snippets.

@ksmithut
Last active May 13, 2024 19:41
Show Gist options
  • Star 76 You must be signed in to star a gist
  • Fork 21 You must be signed in to fork a gist
  • Save ksmithut/e126f7ddb40b760487a17e8b569a77b5 to your computer and use it in GitHub Desktop.
Save ksmithut/e126f7ddb40b760487a17e8b569a77b5 to your computer and use it in GitHub Desktop.
Node Docker Compose nodemon
node_modules

Reload node processes on file save in Docker

Run docker-compose up --build.

Make changes to index.js and see the server restart.

version: '3'
services:
app:
build:
context: .
dockerfile: Dockerfile.dev
volumes:
- ./:/usr/src/app
- /usr/src/app/node_modules # Remove this if you have pure JS dependencies
ports:
- "3000:3000"
FROM node:16-alpine
# Create app directory
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
# Install dependencies
COPY package.json .
RUN npm install
# Bundle app source
COPY index.js ./
# Exports
EXPOSE 3000
CMD [ "npm", "run", "start.dev" ]
'use strict'
const express = require('express')
const { PORT = '3000' } = process.env
const app = express()
app.use((req, res, next) => {
res.send('Hello Jack')
})
app.listen(PORT)
{
"main": "index.js",
"scripts": {
"start": "node index",
"start.dev": "nodemon"
},
"dependencies": {
"express": "^4.17.2"
},
"devDependencies": {
"nodemon": "^2.0.15"
}
}
@ptsurko
Copy link

ptsurko commented Jul 26, 2019

I am always getting error npm ERR! enoent ENOENT: no such file or directory, open '/usr/src/app/package.json' and seems like my /usr/src/app folder is empty. anybody had similar issue?

@ksmithut
Copy link
Author

@ptsurko That does seem odd... what OS and docker versions are you running on?

@ErnandesAJr
Copy link

For those who are having the issue of it not updating you must start nodemon with the -L flag to catch file changes

That solved for me.

@marcosadsj
Copy link

For those who are having the issue of it not updating you must start nodemon with the -L flag to catch file changes

That solved for me! Thanks!

@Griha3212
Copy link

Thx

For those who are having the issue of it not updating you must start nodemon with the -L flag to catch file changes

Thx, it helped me!

@mouradev
Copy link

@fennellgb23

For those who are having the issue of it not updating you must start nodemon with the -L flag to catch file changes.

Thanks!

@pawarvijay
Copy link

awesome

@jailsonsf
Copy link

For those who are having the issue of it not updating you must start nodemon with the -L flag to catch file changes

Thanks

@ahmad-punch
Copy link

For those who are having the issue of it not updating you must start nodemon with the -L flag to catch file changes

Thanks.

@Michaelpalacce
Copy link

For those who are having the issue of it not updating you must start nodemon with the -L flag to catch file changes

Thanks!

@hawaijar
Copy link

hawaijar commented Sep 8, 2020

For those who are having the issue of it not updating you must start nodemon with the -L flag to catch file changes

it works for me too 👍

@alessandroAmedei
Copy link

For those who are having the issue of it not updating you must start nodemon with the -L flag to catch file changes

This worked! Thank you!

@vinhxp03
Copy link

vinhxp03 commented Nov 16, 2020

For those who are having the issue of it not updating you must start nodemon with the -L flag to catch file changes

Thanks

@dmitryshostak
Copy link

For those who are having the issue of it not updating you must start nodemon with the -L flag to catch file changes

Thank you!

@thiere18
Copy link

if all these suggestion don't work you may need to reinstall or update docker

@jjjjackson
Copy link

jjjjackson commented Jan 18, 2021

For those who are having the issue of it not updating you must start nodemon with the -L flag to catch file changes

Thank you so much, mate!

@aderbas
Copy link

aderbas commented Mar 16, 2021

For those who are having the issue of it not updating you must start nodemon with the -L flag to catch file changes

Is that! Thanks

@cyleecode
Copy link

For those who are having the issue of it not updating you must start nodemon with the -L flag to catch file changes

The comment that im looking for, Thank you!

@jhonnycgarcia
Copy link

For those who are having the issue of it not updating you must start nodemon with the -L flag to catch file changes

Work for me! Thanks!

@steadyGuy
Copy link

For those who are having the issue of it not updating you must start nodemon with the -L flag to catch file changes

Thx a lot.
For those who are interested, in the documentation -L option means --legacy-watch which enables Chokidar's polling.

@rsmnarts
Copy link

rsmnarts commented Jul 6, 2021

For those who are having the issue of it not updating you must start nodemon with the -L flag to catch file changes

It's work for me. Thank you!

@caco0516
Copy link

caco0516 commented Nov 3, 2021

For those who are having the issue of it not updating you must start nodemon with the -L flag to catch file changes

This worked for me !!!

@DriLLFreAK100
Copy link

For those who're wondering where the -L flag comes from https://github.com/remy/nodemon/blob/main/lib/cli/parse.js#L132

@guilhermealbino33
Copy link

Starting docker with docker-compose start, it's doens't starts nodemon.
My package.json:
"start.dev": "nodemon -L"
I've tried in that way too:
"start": "nodemon -L ts-node-dev -r tsconfig-paths/register src/infra/api.ts",
Then I've got:
Error: Cannot find module '/usr/app/ts-node-dev' at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15) at Function.Module._load (node:internal/modules/cjs/loader:778:27) at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12) at main (/usr/app/node_modules/ts-node/src/bin.ts:198:14) at Object.<anonymous> (/usr/app/node_modules/ts-node/src/bin.ts:288:3) at Module._compile (node:internal/modules/cjs/loader:1097:14) at Object.Module._extensions..js (node:internal/modules/cjs/loader:1149:10) at Module.load (node:internal/modules/cjs/loader:975:32) at Function.Module._load (node:internal/modules/cjs/loader:822:12) at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)

What am I doing wrong?

@ksmithut
Copy link
Author

@guilhermealbino33 If you're using ts-node-dev you don't need nodemon. So your script might look something like this:

"start.dev": "ts-node-dev -r tsconfig-paths/register src/infra/api.ts"

If that doesn't auto restart on file changes, it looks like it takes a --poll flag:

"start.dev": "ts-node-dev --poll -r tsconfig-paths/register src/infra/api.ts"

But that does make it much more CPU heavy since it's reading the container file system every so often to detect changes.

@guilhermealbino33
Copy link

Even using --poll it doesn't auto restart.
Using ts-node with docker it have never auto restarted in my api.

@ksmithut
Copy link
Author

ksmithut commented Jan 14, 2022

I was able to make this gist work on my machine with the following changes:

package.json

{
  "main": "index.js",
  "scripts": {
    "start": "node index",
    "start.dev": "ts-node-dev index"
  },
  "dependencies": {
    "express": "^4.17.2"
  },
  "devDependencies": {
    "nodemon": "^2.0.15",
    "ts-node-dev": "^1.1.8"
  }
}

More complex environments may require more tweaking. It may also matter what version of docker you're using and your operating system. I'm currently using:

macOS 12.1 21C52 arm64
Docker version 20.10.11, build dea9396

@code-cody-418
Copy link

The -L worked for me thanks!

@akramsameer
Copy link

For those who are having the issue of it not updating you must start nodemon with the -L flag to catch file changes

That worked for thank you ❤️

@ahashem
Copy link

ahashem commented Aug 21, 2023

-L alone didn't help me. adding specific config file nodemon.json or config within package.json can help

{
    "ext": "js,json,ts",
    "watch": ["src"],
    "ignore": ["node_modules/**/node_modules"],
    "exec": "ts-node ./src/index.ts",
    "verbose": true,
    "legacyWatch": true,
    "env": {
        "NODE_ENV": "development"
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment