question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

server imports lack the file extensions required by native esm

See original GitHub issue

Environment

  • Operating System: Darwin
  • Node Version: v16.15.1
  • Nuxt Version: 3.0.0-rc.8
  • Package Manager: yarn@1.22.19
  • Builder: vite
  • User Config: -
  • Runtime Modules: -
  • Build Modules: -

Reproduction

https://stackblitz.com/edit/nuxt-starter-a2yhjt?file=app.vue

Describe the bug

Importing anything from the node_modules without explicit extension will return an error that module is not found. Here is the example of importing a function from lodash. However if I import it with the explicit extension like ‘lodash/cloneDeep.js’ everything works well.

Additional context

No response

Logs

Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/home/projects/nuxt-starter-a2yhjt/node_modules/lodash/cloneDeep' imported from /home/projects/nuxt-starter-a2yhjt/.nuxt/dist/server/server.mjs
Did you mean to import lodash/cloneDeep.js?
    at InternalError.get (https://nuxt-starter-a2yhjt.w.staticblitz.com/blitz.1f021b18268b32e6c6b2095e039ac8c9f88b0d52.js:6:292488)
    at defaultResolve (https://nuxt-starter-a2yhjt.w.staticblitz.com/blitz.1f021b18268b32e6c6b2095e039ac8c9f88b0d52.js:6:622935)
    at ESMLoader.resolve (https://nuxt-starter-a2yhjt.w.staticblitz.com/blitz.1f021b18268b32e6c6b2095e039ac8c9f88b0d52.js:6:1210848)
    at ESMLoader.getModuleJob (https://nuxt-starter-a2yhjt.w.staticblitz.com/blitz.1f021b18268b32e6c6b2095e039ac8c9f88b0d52.js:6:1208564)
    at ESMLoader.import (https://nuxt-starter-a2yhjt.w.staticblitz.com/blitz.1f021b18268b32e6c6b2095e039ac8c9f88b0d52.js:6:1208951)
    at importModuleDynamically (https://nuxt-starter-a2yhjt.w.staticblitz.com/blitz.1f021b18268b32e6c6b2095e039ac8c9f88b0d52.js:6:981760)
    at i.importModuleDynamicallyCallback (https://nuxt-starter-a2yhjt.w.staticblitz.com/blitz.1f021b18268b32e6c6b2095e039ac8c9f88b0d52.js:6:246238)
    at _0x69e4ca (https://nuxt-starter-a2yhjt.w.staticblitz.com/blitz.1f021b18268b32e6c6b2095e039ac8c9f88b0d52.js:15:291665)
    at $id_3IjiM7kU2H (file:///home/projects/nuxt-starter-a2yhjt/.nuxt/dist/server/server.mjs:3544:1)
    at __instantiateModule__ (file:///home/projects/nuxt-starter-a2yhjt/.nuxt/dist/server/server.mjs:3690:9)
    at __ssrLoadModule__ (file:///home/projects/nuxt-starter-a2yhjt/.nuxt/dist/server/server.mjs:3628:25)
    at ssrImport (file:///home/projects/nuxt-starter-a2yhjt/.nuxt/dist/server/server.mjs:3653:13)
    at $id_jJopoG4kMT (file:///home/projects/nuxt-starter-a2yhjt/.nuxt/dist/server/server.mjs:3383:37)
    at async __instantiateModule__ (file:///home/projects/nuxt-starter-a2yhjt/.nuxt/dist/server/server.mjs:3690:3) {
  code: 'ERR_MODULE_NOT_FOUND'
}
[nuxt] [request error] [unhandled] [500] [vite dev] Error loading external "lodash/cloneDeep".
  at async eval (./.nuxt/dev/index.mjs:117:22)  
  at async eval (./node_modules/h3/dist/index.mjs:571:19)  
  at async Server.nodeHandler (./node_modules/h3/dist/index.mjs:517:7)

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:21
  • Comments:21 (7 by maintainers)

github_iconTop GitHub Comments

3reactions
516310460commented, Aug 15, 2022
1reaction
brtinneycommented, Aug 27, 2022

I’m getting this with pinia on rc.8. In my server output when I yarn build, it ends up being import 'pinia/dist/pinia'; which fails to be able to load node_modules/pinia/dist/pina.mjs.

Cannot find module '/var/task/.output/server/node_modules/pinia/dist/pinia' imported from /var/task/.output/server/chunks/_..._.mjs

Adding pinia to transpile in the Nuxt configuration doesn’t seem to do anything. I’m guessing there’s something I can do with vite settings in the Nuxt configuration to get it to explicitly use .mjs, but I haven’t figured it out yet.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How does JavaScript import find the module without an ...
In a browser with support for ES Modules (ESM), no extensions will be added to the URL that you import - if your...
Read more >
ECMAScript modules | Node.js v19.3.0 Documentation
Mandatory file extensions #. A file extension must be provided when using the import keyword to resolve relative or absolute specifiers. Directory indexes...
Read more >
Publishing NPM Packages as Native ES Modules - Medium
This is telling babel to replace the strings written to imports, exports, and requires the correct file extension for the module system we...
Read more >
import - JavaScript - MDN Web Docs - Mozilla
This is often a relative or absolute URL to the .js file containing the module. In Node, extension-less imports often refer to packages...
Read more >
TypeScript and native ESM on Node.js - 2ality
import {start} from 'my-package/dist/src/main.js';. The key point is that, by default, we need to provide filename extensions, especially for ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found