Next.js Discord

Discord Forum

Module not found: Can't resolve

Unanswered
Southeastern blueberry bee posted this in #help-forum
Open in Discord
Southeastern blueberry beeOP
Hello, when i try to launch dev environment next cant resolve: path, os and crypto nodejs modules

During the build, I encountered this error: "Module build failed: UnhandledSchemeError: Reading from 'node:path' is not handled by plugins (Unhandled scheme)." However, I was able to resolve it by removing the "node:" prefix from the imports.

56 Replies

whats your node.js version?
Southeastern blueberry beeOP
v22.12.0
this is a monorepo the other workspace are able to resolve these nodejs modules but not nextjs
@gin where does the error happen?
Southeastern blueberry beeOP
here
ts import { config } from "dotenv";
import { expand } from "dotenv-expand";
import path from "node:path";
import { z } from "zod";

expand(
  config({
    path: path.resolve(process.cwd(), process.env.NODE_ENV === "test" ? ".env.test" : ".env"),
  }),
);

const envSchema = z.object({
  NODE_ENV: z.string(),
  AUTH_SECRET: z.string(),
  JOBS_BEARER_TOKEN: z.string(),
  JOBS_API_BASEURL: z.string().url(),
  NEXT_PUBLIC_BASEURL: z.string().url(),
});

const { data: env, error } = envSchema.safeParse(process.env);

if (error) {
  console.error("❌ Invalid env:");
  console.error(JSON.stringify(error.flatten().fieldErrors, null, 2));
  process.exit(1);
}

export default env!;
Southeastern blueberry beeOP
no error are thrown :/
Dwarf Hotot
you can not call any imports like

const crypto = require("crypto");

you need to import all of this type of imports like

import crypto from "crypto";
try importing crypto like this and check that crypto error is removed or not
Southeastern blueberry beeOP
the require you see in the screen is used inside dotenv lib
@Southeastern blueberry bee no error are thrown :/
no error thrown where?
can u show me where u import this file in your code?
@gin no error thrown where?
Southeastern blueberry beeOP
i mean ive put impoirt "server-only" on the file that use dotenv and nothing change
import 'server-only';

import { config } from "dotenv";
import { expand } from "dotenv-expand";
import path from "node:path";
import { z } from "zod";

expand(
  config({
    path: path.resolve(process.cwd(), process.env.NODE_ENV === "test" ? ".env.test" : ".env"),
  }),
);

const envSchema = z.object({
  NODE_ENV: z.string(),
  AUTH_SECRET: z.string(),
  JOBS_BEARER_TOKEN: z.string(),
  JOBS_API_BASEURL: z.string().url(),
  NEXT_PUBLIC_BASEURL: z.string().url(),
});

const { data: env, error } = envSchema.safeParse(process.env);

if (error) {
  console.error("❌ Invalid env:");
  console.error(JSON.stringify(error.flatten().fieldErrors, null, 2));
  process.exit(1);
}

export default env!;
@Southeastern blueberry bee i mean ive put impoirt "server-only" on the file that use dotenv and nothing change
u need to explain it better, just sayin "no error thrown" i cant help u.
U need to tell me if its on dev, what route or if it is a route handler, or if its production build, or if it happens on build
if u import this file in another file which exports a function that u use on the client u will get this error
u need to carefully seperate client and server code
u cant mix up both
even if it sometimes works
Southeastern blueberry beeOP
this file is imported inside instrumentation.ts file https://nextjs.org/docs/app/building-your-application/optimizing/open-telemetry
export async function register() {
  await import("./env");
}
Southeastern blueberry beeOP
Module not found: Can't resolve 'path'
  3 | import { config } from "dotenv";
  4 | import { expand } from "dotenv-expand";
> 5 | import path from "node:path";
    | ^
  6 | import { z } from "zod";
  7 |
  8 | expand(

https://nextjs.org/docs/messages/module-not-found

Import trace for requested module:
./src/instrumentation.ts
look the last line
yeah
oh yeah i oversaw that
for what do u need instrumentation?
Southeastern blueberry beeOP
just for doing the validation of env variable since the register() function is executed on each nextjs instantiation
i think its related to monorepo but i cant figure out whats exactly
thats the problem i think
that just idk
just for validating your envs?
if u really want to validate it, cant u handle that outside of the runtime?
Southeastern blueberry beeOP
yeah but i think i will remove it
Southeastern blueberry beeOP
that was working well just today idk what happend xD
yeah i know that problem
xD
sometimes code just breaks for no reason
if u use turborepo u can easily integrate your env checking
@gin if u use turborepo u can easily integrate your env checking
Southeastern blueberry beeOP
yeah im using it, how can i do that?
like that ?
    "@delivery/web#build": {
      "dependsOn": ["@delivery/jobs#build"],
      "env": ["AUTH_SECRET", "JOBS_BEARER_TOKEN", "JOBS_API_BASEURL"]
    },
yeah but i think that just makes sure the envs are recognized right?
but it can still be a empty string
u can have a seperate script do that for u and just chain it in your build script
root package.json
and just have build depend on that script and when it fails, the whole build fails
Southeastern blueberry beeOP
I just realized that the register function might not execute sometimes with nodejs. I simply checked the runtime before import:

export async function register() {
  if (process.env.NEXT_RUNTIME === "nodejs") {
    await import("./env");
  }
}
thats why node modules was not recognized