Run docker-compose up --build
.
Make changes to index.js and see the server restart.
node_modules |
node_modules |
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" | |
} | |
} |
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!
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!
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.
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!
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 !!!
For those who're wondering where the -L flag comes from https://github.com/remy/nodemon/blob/main/lib/cli/parse.js#L132
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?
@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.
Even using --poll it doesn't auto restart.
Using ts-node with docker it have never auto restarted in my api.
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
The -L worked for me thanks!
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 ❤️
-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"
}
}
Is that! Thanks