Next.js Discord

Discord Forum

npm run dev doesn't invoke useEffect

Unanswered
Rhinoceros Auklet posted this in #help-forum
Open in Discord
Rhinoceros AukletOP
...but if I run npm run build, npm run start then everything is fine.

Steps to reproduce:
npx create-next-app@latest

Accept defaults for project name and options.

Open app/page.tsx

Modify default example as below:

"use client"
import Image from "next/image";
import { useEffect } from "react";

export default function Home() {
useEffect(() => { console.log('test'); }, []);

return (
<div className="flex min-h-screen items-center justify-center bg-zinc-50 font-sans dark:bg-black">.... rest of example code....

npm run dev

Open page and check browser console. Observe no console log of 'test'.

Close page, shutdown server.

npm run build
npm run start

Open page again, check browser console. Observe console log of 'test' appears.

Is this a bug with dev mode? Is it supposed to be behave this way? 😕

I'm a complete novice to NextJS picking it up. This behaviour problem in dev derailed me for quite a while until I discovered nothing was wrong with my code, it works fine in production mode (after much time and frustration had occurred and I actually thought to actually try production mode out of desperation...) it's just my useEffect was never invoked when running in dev mode...

Thanks to anyone who can offer help.

1 Reply

Rhinoceros AukletOP
Nm figured it out...

This was the issue:

We detected multiple lockfiles and selected the directory of C:\Users<username>\yarn.lock as the root directory.
To silence this warning, set turbopack.root in your Next.js config, or consider removing one of the lockfiles if it's not needed.
See https://nextjs.org/docs/app/api-reference/config/next-config-js/turbopack#root-directory for more information.
Detected additional lockfiles:
* C:\Users\<username>\Documents<username> Files\Projects\Proj1\proj-web\package-lock.json

I deleted the below files from C:\Users\<username>\
package.json
package-lock.json
yarn.lock

When I run
npm run dev

All works as expected 😩