execution failure on wsl2
Environment
windows version - 11, 23H2 wsl2 distro - ubuntu 24.04.02 node version - 22.15.0 node-cron version- 4.2.1
Problem
I have this code for a job in my js file that should run every minute
import cron from 'node-cron';
cron.schedule('* * * * *', async () => {
console.log('first scheduler');
});
but instead i have been getting the below errors
I have tried to make it a background task as indicated in docs to have isolated process by extracting the console.log function into its own file but the result is same. It fails every time. I looked at my task manager while it was running and cpu/memory usuage is low. Looked at the mem/cpu usage in wsl2 with htop and nothing passed over 2-3%. I am not sure why this is happening.
if i schedule it to run every second with * * * * * * expression it works but still throws the error around every 40 second. setting 1 * * * * * doesn't work for some reason, I think it should also run every second.
sorry for not providing more info I am very new to linux and cron
Hello @GaneshS288, thanks for sharing this and about the analysis on cpu usage.
This happens when using exactly the code you shared or it's just an example?
When isolating into a background task, the issue is likely related to you task implementation. Is that doing any synchronous or blocking operation?
About the expression 1 * * * * * it means at second 1 so it's going to execute every minute on second 1.
@merencia thanks for the response. This happens with the exact code I shared. The original code had some db queries. I thought they were the cause so I removed them and put the code I shared above.
I tried the above code in my existing node project and also tried it in a blank new project with fresh node-cron install. The same results happened, it always failed to run.
Also thanks for clarifying what the expression means.
I created a new node project and inside it created a file named cron.js.
import nodeCron from "node-cron";
nodeCron.schedule("* * * * *", async () => {
console.log("cron task");
});
I then ran this with node cron.js command. Below is a screenshot of htop while previous command is running
and here is the error in terminal with cron job
why does htop show multiple process ID for node cron.js while the error from node-cron only shows one consistently. Is this something unusual or are multiple process ID common
A really weird update-
Node cron seems to work if I have my wifi turned off. But if I am connected to internet it stops working.
I tried shutting down the wifi while command was running and failing then it started working.
After connecting to the internet it started failing again.
I'm really unsure what's causing this now. I used ps aux command to see if any new processes popped up after connecting to internet by looking at the timestamp but there doesn't seem to be any new ones
Another update.
I learned how to use crontab that comes installed by default in ubuntu and that works without any problems and regardless of wifi connected or not. So maybe the fault is on node-cron side?
Thanks for the detailed updates.
This is a strange issue, especially with the behavior changing when the internet is connected. Could it be something in the environment affecting how the process runs in WSL2? Maybe some firewall, antivirus, VPN, or network policy interfering?
Have you tried running the same code outside of WSL2 to see if the issue still happens? That might help isolate the cause.
I don't have a vpn and after doing some research it seems wsl2 uses windows firewall which allows any traffic from wsl. I confirmed by checking ufw status which shows as inactive. I can also use curl, fetch with node, db queries using orm etc with no difficulty in wsl2. I am not sure where to check in network policy if that is interfering.
following your advice, I installed node on windows and node cron works there without any problems regardless of internet connection status.
I also asked a friend who runs wsl2 with ubuntu 20 to check and it worked for her. I am unable to reset and reinstall wsl2 for now to test with a fresh install unfortunately.
Had the same issue on WSL2, after upgrading package.json dependencies of a project.
windows version - 11, 24H2 wsl2 - ubuntu 24.04.2 LTS node version - 20.18.1 node-cron version- 4.2.1
@GaneshS288 Downgraiding to node-cron v.3.0.3 solved the issue.
following @danzelbg's advice I changed the node-cron package version to 3.0.3 and that seems to work fine. I also tried out 4.0.0 and that doesn't work just like 4.2.1 with same error message. So i would assume 4.0.0 is where some changes were introduced that are causing this problem
I think I have a clue. Starting from node-cron v4, we use Intl.DateTimeFormat to handle timezones and localized formatting.
This issue might be related to how Intl behaves inside WSL2 — especially if Node.js was built with small-icu and lacks full locale support.
Can you please run the following code inside your WSL2 environment and share the output?
const dftOptions = {
year: 'numeric',
month: '2-digit',
day: '2-digit',
hour: '2-digit',
minute: '2-digit',
second: '2-digit',
weekday: 'short',
hour12: false
}
const dateFormat = new Intl.DateTimeFormat('en-US', dftOptions);
const parts = dateFormat.formatToParts(new Date()).filter(part => {
return part.type !== 'literal';
}).reduce((acc, part) => {
acc[part.type] = part.value;
return acc;
}, {});
console.log(parts);
@merencia sure.
WSL2 windows version - 11, 24H2 wsl2 - ubuntu 24.04.2 node version - 24.2.0
WSL2 output
{
weekday: 'Wed',
month: '07',
day: '30',
year: '2025',
hour: '20',
minute: '38',
second: '29'
}
Docker Container extended from node:20-alpine3.19
Dockerfile
FROM node:20-alpine3.19
ARG TZ=Europe/Sofia
RUN apk add --no-cache tzdata
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
WORKDIR /usr/src/app
COPY ./express/package*.json /usr/src/app/.
COPY ./.env /usr/src/app/.env
RUN npm install
...
Docker Container output
{
weekday: 'Wed',
month: '07',
day: '30',
year: '2025',
hour: '20',
minute: '59',
second: '15'
}
this is what I get on my wsl2
{
weekday: 'Thu',
month: '07',
day: '31',
year: '2025',
hour: '07',
minute: '57',
second: '41'
}
A similar issue (all 'run every minute' tasks were missing, and 'run every second' tasks were occasionally missing) was happening in my WSL2 environment.
Strangely, after I ran sudo hwclock -s to resynchronize the time, everything started working as if nothing had happened.