N
Nuxt2mo ago
_frdrk_

ERR_MODULE_NOT_FOUND jiti/lib/jiti.mjs

Does anyone know what can cause the following error?
[3:19:15 pm] ERROR Error while importing module MODULE_HERE: Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/home/runner/work/MODULE_HERE/node_modules/jiti/lib/jiti.mjs' imported from /Users/frederikprijck/Development/playground/my-nuxt-app/node_modules/jiti/lib/jiti.mjs

at loadNuxtModuleInstance (node_modules/@nuxt/kit/dist/index.mjs:720:15)
at async installModules (node_modules/@nuxt/kit/dist/index.mjs:561:17)
at async initNuxt (node_modules/nuxt/dist/index.mjs:5856:3)
at async loadNuxt (node_modules/nuxt/dist/index.mjs:6073:5)
at async loadNuxt (node_modules/@nuxt/kit/dist/index.mjs:947:16)
at async Object.run (node_modules/@nuxt/cli/dist/chunks/prepare.mjs:32:18)
at async runCommand (node_modules/citty/dist/index.mjs:316:16)
at async runCommand (node_modules/citty/dist/index.mjs:307:11)
at async runMain (node_modules/citty/dist/index.mjs:445:7)
[3:19:15 pm] ERROR Error while importing module MODULE_HERE: Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/home/runner/work/MODULE_HERE/node_modules/jiti/lib/jiti.mjs' imported from /Users/frederikprijck/Development/playground/my-nuxt-app/node_modules/jiti/lib/jiti.mjs

at loadNuxtModuleInstance (node_modules/@nuxt/kit/dist/index.mjs:720:15)
at async installModules (node_modules/@nuxt/kit/dist/index.mjs:561:17)
at async initNuxt (node_modules/nuxt/dist/index.mjs:5856:3)
at async loadNuxt (node_modules/nuxt/dist/index.mjs:6073:5)
at async loadNuxt (node_modules/@nuxt/kit/dist/index.mjs:947:16)
at async Object.run (node_modules/@nuxt/cli/dist/chunks/prepare.mjs:32:18)
at async runCommand (node_modules/citty/dist/index.mjs:316:16)
at async runCommand (node_modules/citty/dist/index.mjs:307:11)
at async runMain (node_modules/citty/dist/index.mjs:445:7)
I am getting this after doing:
npx nuxi@latest init my-nuxt-app
cd my-nuxt-app
npm i MODULE_HERE
npx nuxi@latest init my-nuxt-app
cd my-nuxt-app
npm i MODULE_HERE
Then I include the module in the nuxt config and run npm run dev. For what it's worth, the file does exist in my node_modules:
ls /Users/frederikprijck/Development/playground/my-nuxt-app/node_modules/jiti/lib

jiti-cli.mjs jiti-hooks.mjs jiti-native.mjs jiti-register.d.mts jiti-register.mjs jiti.cjs jiti.d.cts jiti.d.mts jiti.mjs types.d.ts
ls /Users/frederikprijck/Development/playground/my-nuxt-app/node_modules/jiti/lib

jiti-cli.mjs jiti-hooks.mjs jiti-native.mjs jiti-register.d.mts jiti-register.mjs jiti.cjs jiti.d.cts jiti.d.mts jiti.mjs types.d.ts
Disclaimer: the module is my own, and new, available on npm. So it may just be an issue in the module.
12 Replies
kapa.ai
kapa.ai2mo ago
Hi @frdrk. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- The error ERR_MODULE_NOT_FOUND for jiti/lib/jiti.mjs when importing a module in a Nuxt project is likely related to issues with module resolution in the ESM (ECMAScript Module) context that Nuxt uses, especially in Nuxt 3 and 4. Based on the knowledge sources, here are some possible causes and troubleshooting steps: 1. ESM/CJS Resolution Issues:
Nuxt uses native ESM for its server engine (Nitro), and module resolution can be sensitive to how dependencies are structured and exported. If a module or one of its dependencies is not properly ESM-compatible, or if the exports/main fields in package.json are not set up correctly, you can get module not found errors—even if the file exists on disk. Node.js does not use the module field for resolution; it relies on exports and main fields, and the file extension (.mjs for ESM, .cjs for CJS, .js depending on "type": "module" in package.json) Docs: ES Modules in Nuxt 4. 2. Upstream Library Compatibility:
If the module you are importing is your own and new, it may not be fully compatible with ESM or the way Nuxt expects modules to be structured. The Nuxt documentation suggests that if you encounter such errors, the issue is almost certainly with the upstream library, which needs to be fixed to support being imported by Node in an ESM context Docs: Troubleshooting ESM Issues. 3. Transpiling or Aliasing as a Workaround:
As a temporary workaround, you can add the problematic module to build.transpile in your nuxt.config to force Nuxt to transpile it, or alias it to a CJS version if available:
export default defineNuxtConfig({
build: {
transpile: ['MODULE_HERE'],
},
alias: {
'MODULE_HERE': 'MODULE_HERE/dist/module.cjs.js', // if you have a CJS build
},
})

export default defineNuxtConfig({
build: {
transpile: ['MODULE_HERE'],
},
alias: {
'MODULE_HERE': 'MODULE_HERE/dist/module.cjs.js', // if you have a CJS build
},
})

Docs: Troubleshooting ESM Issues 4. Module Path Resolution:
There have been recent changes in Nuxt to improve module path resolution and avoid falling back to CJS paths, using the mlly package for more robust handling. If your module or its dependencies are not exporting paths correctly, this could cause issues Nuxt PR: use mlly to resolve module paths. 5. Check for Dependency Duplicates or Corruption:
Sometimes, issues can arise from duplicated or corrupted dependencies. Try deleting node_modules and your lock file (package-lock.json or pnpm-lock.yaml), then reinstalling dependencies. 6. Module Exports:
Make sure your module's package.json uses the exports field correctly for ESM, and that your ESM files use the .mjs extension if you are not setting "type": "module" in your package Docs: Library Author Guide. Summary:
The error is most likely due to ESM/CJS compatibility or incorrect module exports in your custom module. Review your module's package.json and build output, and consider the workarounds above. If the problem persists, you may need to adjust your module to be fully ESM-compatible as described in the Nuxt documentation. If you need more specific help, please provide your module's package.json and export structure. --- If you need more details on ESM compatibility and troubleshooting, see the official Nuxt documentation: ES Modules: Troubleshooting ESM Issues. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai2mo ago
<@227872143281487872> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
_frdrk_
_frdrk_OP2mo ago
Ok so that made me look into the files, and I noticed this at the top:
import { createJiti } from "file:///home/runner/work/MODULE_HERE/node_modules/jiti/lib/jiti.mjs";
import { createJiti } from "file:///home/runner/work/MODULE_HERE/node_modules/jiti/lib/jiti.mjs";
@kapa.ai does that additional context help ?
kapa.ai
kapa.ai2mo ago
Let me check the knowledge base for you again. One moment! :books: ---------------------- Thank you for the additional context. Based on the knowledge sources, the error you are encountering—ERR_MODULE_NOT_FOUND for jiti/lib/jiti.mjs—is most likely related to ESM (ECMAScript Module) and module resolution issues in the context of Nuxt 3/4. The Nuxt documentation explains that such errors are often due to upstream library compatibility with ESM. Even if the file exists on disk, Node.js may not resolve it correctly if the module's package.json does not properly define the exports or if the file extensions and "type": "module" settings are not aligned. This is especially important for libraries that are consumed in an ESM context, as Nuxt 3+ does natively. The recommended approach is to ensure your library is ESM-compatible, ideally using .mjs for ESM files and updating the exports field in your package.json accordingly. If your library is not ESM-compatible, you may need to add it to build.transpile or alias it to a CJS build as a workaround in your Nuxt config. These steps are outlined in the Nuxt documentation on troubleshooting ESM issues and authoring ESM-compatible libraries Docs: Troubleshooting ESM Issues Docs: Library Author Guide. If you have already confirmed that the file exists, the issue is almost certainly with how the module is being resolved or exported, not with the file's presence. The knowledge sources do not provide a more specific root cause for your exact scenario, but they do confirm that this class of error is common when ESM compatibility is not fully addressed in custom or third-party modules. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai2mo ago
<@227872143281487872> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
_frdrk_
_frdrk_OP2mo ago
@kapa.ai Ok so that made me look into the files, and I noticed this at the main file:
import { createJiti } from "file:///Users/frederikprijck/Development/MODULE_HERE/node_modules/jiti/lib/jiti.mjs";

const jiti = createJiti(import.meta.url, {
"interopDefault": true,
"alias": {
"MODULE_HERE": "/Users/frederikprijck/Development/MODULE_HERE/packages/MODULE_HERE"
},
"transformOptions": {
"babel": {
"plugins": []
}
}
})

/** @type {import("/Users/frederikprijck/Development/MODULE_HERE/packages/MODULE_HERE/src/module.js")} */
const _module = await jiti.import("/Users/frederikprijck/Development/MODULE_HERE/packages/MODULE_HERE/src/module.ts");

export default _module?.default ?? _module;
import { createJiti } from "file:///Users/frederikprijck/Development/MODULE_HERE/node_modules/jiti/lib/jiti.mjs";

const jiti = createJiti(import.meta.url, {
"interopDefault": true,
"alias": {
"MODULE_HERE": "/Users/frederikprijck/Development/MODULE_HERE/packages/MODULE_HERE"
},
"transformOptions": {
"babel": {
"plugins": []
}
}
})

/** @type {import("/Users/frederikprijck/Development/MODULE_HERE/packages/MODULE_HERE/src/module.js")} */
const _module = await jiti.import("/Users/frederikprijck/Development/MODULE_HERE/packages/MODULE_HERE/src/module.ts");

export default _module?.default ?? _module;
This does not seem right
kapa.ai
kapa.ai2mo ago
Let me check the knowledge base for you again. One moment! :books: ---------------------- You're correct—what you’re seeing in your main file is not ideal for a Nuxt module, especially in an ESM context. The use of absolute file URLs and direct imports from local node_modules paths (e.g., import { createJiti } from "file:///Users/frederikprijck/Development/MODULE_HERE/node_modules/jiti/lib/jiti.mjs";) is problematic for several reasons: 1. Hardcoded Absolute Paths:
Hardcoding absolute paths makes your module non-portable. When published and installed elsewhere, these paths will not exist, leading to module resolution errors. 2. Direct Use of Jiti:
Nuxt 3+ is fully ESM-native and does not require (or expect) the use of Jiti for module loading. Instead, you should use standard ESM imports and, if you need dynamic resolution, leverage utilities like resolvePath from @nuxt/kit or mlly for advanced scenarios Docs: Library Author Guide. 3. ESM Compatibility:
In ESM modules, you should avoid using Node.js-specific globals and patterns from CommonJS. Instead, use ESM imports and ensure your module’s package.json is set up for ESM (e.g., "type": "module" or using .mjs extensions) Docs: ES Modules. What to do instead:
- Refactor your module to use relative or package-based imports, not absolute file URLs. - Export your module using ESM syntax. - If you need to resolve paths, use Nuxt’s or mlly’s utilities, not Jiti. - Ensure your published package does not contain hardcoded local paths. Example (ESM-compliant):
// module.ts
export default function myModule(options) {
// Module logic here
}
// module.ts
export default function myModule(options) {
// Module logic here
}
// index.mjs or main entry
import myModule from './src/module.js'
export default myModule
// index.mjs or main entry
import myModule from './src/module.js'
export default myModule
Summary:
The code you posted is not portable or ESM-compliant for Nuxt modules. Refactor to use standard ESM imports and avoid hardcoded paths and Jiti. This will resolve the kind of errors you’re seeing and make your module compatible with Nuxt’s expectations Docs: Library Author Guide. If you need more details on ESM migration, see the Nuxt ESM Library Author Guide. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai2mo ago
<@227872143281487872> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
_frdrk_
_frdrk_OP2mo ago
@kapa.ai This code is in the generated dist folder, I am not sure where it comes from.
kapa.ai
kapa.ai2mo ago
Let me check the knowledge base for you again. One moment! :books: ---------------------- Based on the knowledge sources, there is no explicit documentation about Nuxt or its official module builder generating code in the dist folder that uses absolute file URLs (like file:///Users/.../node_modules/jiti/lib/jiti.mjs) or direct Jiti imports in the way you described. The recommended and documented approaches for building Nuxt modules involve using ESM imports, relative paths, and utilities like import.meta.url and createResolver from @nuxt/kit for path resolution—not hardcoded absolute paths or Jiti-based dynamic imports Docs: Library Author Guide Docs: Import Meta. If this code appears in your generated dist folder, it is likely being introduced by your own build process, a custom script, or a misconfiguration in your module setup, rather than by Nuxt or its official tooling. The knowledge sources do not provide enough information to identify the exact origin of this code generation. If you are using a custom build tool or script, review its configuration and output. If you are using @nuxt/module-builder, it should not generate such code according to the official documentation Nuxt Module Builder. In summary:
I'm sorry, but the knowledge sources do not contain enough information to explain why this code is being generated in your dist folder. It does not match the standard output or practices documented for Nuxt module development. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai2mo ago
<@227872143281487872> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
_frdrk_
_frdrk_OP2mo ago
Do not publish with --stub 😄

Did you find this page helpful?