Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

server imports lack the file extensions required by native esm #14597

Closed
v-romanyuk opened this issue Aug 15, 2022 · 21 comments
Closed

server imports lack the file extensions required by native esm #14597

v-romanyuk opened this issue Aug 15, 2022 · 21 comments

Comments

@v-romanyuk
Copy link

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)
@AndrewSparrowww
Copy link

@pi0 I can also reproduce this issue.

@516310460
Copy link
Contributor

Resolution: https://stackblitz.com/edit/nuxt-starter-zwu1fk?file=nuxt.config.ts,app.vue
build and vite

@AndrewSparrowww
Copy link

Resolution: https://stackblitz.com/edit/nuxt-starter-zwu1fk?file=nuxt.config.ts,app.vue build and vite

Added this options returns in other errors:

[nuxt] [request error] [unhandled] [500] request to http://localhost:3000/__nuxt_vite_node__/manifest failed, reason: connect ECONNREFUSED 127.0.0.1:3000 ()

Other potential fix may be to add extension of the import module like:

import cloneDeep from 'lodash/cloneDeep'

import cloneDeep from 'lodash/cloneDeep.js'

@516310460
Copy link
Contributor

Resolution: https://stackblitz.com/edit/nuxt-starter-zwu1fk?file=nuxt.config.ts,app.vue build and vite

Added this options returns in other errors:

[nuxt] [request error] [unhandled] [500] request to http://localhost:3000/__nuxt_vite_node__/manifest failed, reason: connect ECONNREFUSED 127.0.0.1:3000 ()

Other potential fix may be to add extension of the import module like:

import cloneDeep from 'lodash/cloneDeep'

import cloneDeep from 'lodash/cloneDeep.js'

In stack seems normal

@vis97c
Copy link
Contributor

vis97c commented Aug 21, 2022

Resolution: https://stackblitz.com/edit/nuxt-starter-zwu1fk?file=nuxt.config.ts,app.vue build and vite

Added this options returns in other errors:

[nuxt] [request error] [unhandled] [500] request to http://localhost:3000/__nuxt_vite_node__/manifest failed, reason: connect ECONNREFUSED 127.0.0.1:3000 ()

Other potential fix may be to add extension of the import module like:

import cloneDeep from 'lodash/cloneDeep'

import cloneDeep from 'lodash/cloneDeep.js'

Stack one also didn't work for me. Switching from rc.6 to rc.8 created this issue.

I had this on my config

export default defineNuxtConfig({
	nitro: {
		externals: {
			inline: ["uuid", "lodash"],
		},
	},
})

now it doesn't seem to work.

Did you figure it out @v-romanyuk ?

@vis97c
Copy link
Contributor

vis97c commented Aug 21, 2022

For anyone wondering what the issue is? es6 modules like uiid needs to be transpiled.

export default defineNuxtConfig({
	build: {
		transpile: ["uuid"],
	},
})

Anything else must be commonjs and additionally folder import are no longer a thing

You will need to especify the file for imports within libraries like so:

import cloneDeep from "lodash/cloneDeep.js";

@gezichenshan
Copy link

Resolution: https://stackblitz.com/edit/nuxt-starter-zwu1fk?file=nuxt.config.ts,app.vue build and vite

Added this options returns in other errors:

[nuxt] [request error] [unhandled] [500] request to http://localhost:3000/__nuxt_vite_node__/manifest failed, reason: connect ECONNREFUSED 127.0.0.1:3000 ()

Other potential fix may be to add extension of the import module like:
import cloneDeep from 'lodash/cloneDeep'

import cloneDeep from 'lodash/cloneDeep.js'

Stack one also didn't work for me. Switching from rc.6 to rc.8 created this issue.

I had this on my config

export default defineNuxtConfig({
	nitro: {
		externals: {
			inline: ["uuid", "lodash"],
		},
	},
})

now it doesn't seem to work.

Did you figure it out @v-romanyuk ?

Same error here happened after switching from rc.6 to rc.8

Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/Users/user/ryanown/ownwork/abroad-go/web/node_modules/lodash-es/isPlainObject' imported from /Users/user/ryanown/ownwork/abroad-go/web/.nuxt/dist/server/server.mjs
Did you mean to import lodash-es/isPlainObject.js?

@v-romanyuk
Copy link
Author

@gezichenshan I just put a file extension in the import path for now. Not a big deal but still a bit inconvenient.
es6 modules have to be included in the build -> transpile object to make them properly work.

@gezichenshan
Copy link

@v-romanyuk I've tried to add lodash-es in the build -> transpile but nothing works.

The issue originated that I want to use @pinia/nuxt in my nuxt3 project (3.0.0-rc.6). Then I encountered an issue that pinia says its peerDependency "vue-demi " isn't a common js module and didn't export the functions I want. So I follow the doc adding "vue-demi" into the build -> transpile but no help.
Then I saw some comments that upgrade nuxt3 to rc.8 will solve the issue, but then I got stuck here 😂.

@v-romanyuk v-romanyuk reopened this Aug 23, 2022
@alisdg
Copy link

alisdg commented Aug 23, 2022

Resolution: https://stackblitz.com/edit/nuxt-starter-zwu1fk?file=nuxt.config.ts,app.vue build and vite

Added this options returns in other errors:

[nuxt] [request error] [unhandled] [500] request to http://localhost:3000/__nuxt_vite_node__/manifest failed, reason: connect ECONNREFUSED 127.0.0.1:3000 ()

Other potential fix may be to add extension of the import module like:
import cloneDeep from 'lodash/cloneDeep'

import cloneDeep from 'lodash/cloneDeep.js'

Stack one also didn't work for me. Switching from rc.6 to rc.8 created this issue.
I had this on my config

export default defineNuxtConfig({
	nitro: {
		externals: {
			inline: ["uuid", "lodash"],
		},
	},
})

now it doesn't seem to work.
Did you figure it out @v-romanyuk ?

Same error here happened after switching from rc.6 to rc.8

Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/Users/user/ryanown/ownwork/abroad-go/web/node_modules/lodash-es/isPlainObject' imported from /Users/user/ryanown/ownwork/abroad-go/web/.nuxt/dist/server/server.mjs
Did you mean to import lodash-es/isPlainObject.js?

exact same error here, Has anyone found any solution ?

@vis97c
Copy link
Contributor

vis97c commented Aug 23, 2022

@alisdg @gezichenshan Use the commonjs module "lodash" and for file import add the .js file extension. Like this. This is not a nuxt issue but a intended nodejs package resolution feature.

@rowanwins
Copy link

I'm getting the same lodash error when trying to run npm run dev with rc.8.
I'm not using lodash in any of my own code

Cannot find module '/mysite/node_modules/lodash-es/isPlainObject' imported from mysite/.nuxt/dist/server/server.mjs
Did you mean to import lodash-es/isPlainObject.js?

@brtinney
Copy link

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.

@516310460
Copy link
Contributor

516310460 commented 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.

Using @nuxt/pinia
Or
You can refer to my code https://github.com/516310460/pinia-nuxt-persist

@brtinney
Copy link

Using @nuxt/pinia Or You can refer to my code https://github.com/516310460/pinia-nuxt-persist

That’s not the issue. I have that module already set up. And everything works great when using the dev server. I just wrote a script to add the .mjs after I build, which solves my problem for now.

@danielroe danielroe changed the title Cannot find module server imports lack the file extensions required by native esm Aug 30, 2022
@danielroe danielroe self-assigned this Sep 12, 2022
@gbdubs
Copy link

gbdubs commented Sep 29, 2022

I'm not sure if this is relevant, but I was experiencing this component of this issue:

Added this options returns in other errors:

[nuxt] [request error] [unhandled] [500] request to http://localhost:3000/__nuxt_vite_node__/manifest failed, reason: connect ECONNREFUSED 127.0.0.1:3000 ()

And changing my npm version to that of a collaborator (who was not experiencing this issue on the same, clean, repo) fixed this symptom.

NVM: nvm use 16.13
NPM: 8.1.2
Node: v16.13.2

I don't know enough about nuxt to know if this is likely to be helpful for others, so I'm happy to delete this if it's not useful.

@szwenni
Copy link

szwenni commented Oct 4, 2022

Using @nuxt/pinia Or You can refer to my code https://github.com/516310460/pinia-nuxt-persist

That’s not the issue. I have that module already set up. And everything works great when using the dev server. I just wrote a script to add the .mjs after I build, which solves my problem for now.

Can you share that script ? I'm hanging now on the same issue.

@brtinney
Copy link

brtinney commented Oct 4, 2022

Using @nuxt/pinia Or You can refer to my code https://github.com/516310460/pinia-nuxt-persist

That’s not the issue. I have that module already set up. And everything works great when using the dev server. I just wrote a script to add the .mjs after I build, which solves my problem for now.

Can you share that script ? I'm hanging now on the same issue.

I just used sed. The following script does that replacement in the two files, and is run during CI/CD in a node-alpine container after the build is done.

#!/bin/sh
sed -i "s#pinia/dist/pinia'#pinia/dist/pinia.mjs'#g" .output/server/chunks/_..._.mjs
sed -i "s#pinia/dist/pinia'#pinia/dist/pinia.mjs'#g" .output/server/chunks/_orders.mjs

@justwiebe
Copy link

I'm getting this error in webpack mode even though I've got extensions set

[nuxt] [request error] [unhandled] [500] Cannot find module '{repoPath}\node_modules\@company\uikit\es\mixins\date-formats' imported from {repoPath}\.nuxt\dist\server\server.mjs
Did you mean to import @company/uikit/es/mixins/date-formats.js?
  at new NodeError (node:internal/errors:371:5)
  at finalizeResolution (node:internal/modules/esm/resolve:416:11)
  at moduleResolve (node:internal/modules/esm/resolve:932:10)
  at defaultResolve (node:internal/modules/esm/resolve:1044:11)
  at ESMLoader.resolve (node:internal/modules/esm/loader:422:30)
  at ESMLoader.getModuleJob (node:internal/modules/esm/loader:222:40)
  at ModuleWrap.<anonymous> (node:internal/modules/esm/module_job:76:40)
  at link (node:internal/modules/esm/module_job:75:36)
export default defineNuxtConfig({
  alias: {
    '~': path.resolve(__dirname, 'node_modules'),
    '@/': path.resolve(__dirname, 'src')
  },
  vite: false,
  webpack: {
    resolve: {
      extensions: [
        ".js",
        ".vue",
        ".mjs"
      ]
    }
  },
  extensions: [
    ".js",
    ".vue",
    ".mjs"
  ]

@justwiebe
Copy link

I tried adding file extensions, but then I get this...

[nuxt] [request error] [unhandled] [500] Unknown file extension ".vue" for {repoPath}\node_modules\@company\uikit\es\loading\Loading.vue
  at new NodeError (node:internal/errors:371:5)
  at Object.file: (node:internal/modules/esm/get_format:72:15)
  at defaultGetFormat (node:internal/modules/esm/get_format:85:38)
  at defaultLoad (node:internal/modules/esm/load:13:42)
  at ESMLoader.load (node:internal/modules/esm/loader:303:26)
  at ESMLoader.moduleProvider (node:internal/modules/esm/loader:230:58)
  at new ModuleJob (node:internal/modules/esm/module_job:63:26)
  at ESMLoader.getModuleJob (node:internal/modules/esm/loader:244:11)
  at async ModuleWrap.<anonymous> (node:internal/modules/esm/module_job:78:21)
  at async Promise.all (index 4)

@danielroe
Copy link
Member

The original reproduction is no longer reproducible for me with Nitro 0.6.1: https://stackblitz.com/edit/nuxt-starter-shejcp. If someone is still experiencing a similar problem, please open a new issue with a reproduction and I will address.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests