Skip to content

The "type": "module" breaks npm run dev #22033

@MaximLitvinovZenni

Description

@MaximLitvinovZenni

Environment

  • Operating System: Darwin
  • Node Version: v18.16.1
  • Nuxt Version: 3.6.2
  • Nitro Version: 2.5.2
  • Package Manager: npm@9.5.1
  • Builder: vite
  • User Config: alias, app, css, plugins, components, modules, nitro
  • Runtime Modules: -
  • Build Modules: -

Reproduction

Stackblitz: https://stackblitz.com/edit/nuxt-starter-s9qxje?file=libs%2Fconfig%2Fload.ts

The project has next structure different from the standard:

  • /libs - folder contain "common code."
  • /libs/config/cli.js - an "internal" tool importing load-js.js
  • /libs/config/load-js.js - an es6 module contains code shared with an internal tool
  • /libs/config/load.ts - the TS wrapper that imports load-js.js
  • /server/api/hello.ts - the API endpoint that import /libs/config

The nuxt.config.ts:

// https://v3.nuxtjs.org/api/configuration/nuxt.config
export default defineNuxtConfig({
  alias: {
    '~/*': './*',
  }
})

The package.json has the type "module":

{
  "private": true,
  "type": "module",
  "scripts": {
    "build": "nuxt build",
    "dev": "HOST=0.0.0.0 nuxt dev",
    "generate": "nuxt generate",
    "preview": "nuxt preview",
    "postinstall": "nuxt prepare"
  },
  "devDependencies": {
    "@types/node": "^18",
    "nuxt": "^3.6.2"
  }
}

Describe the bug

When running npm run dev, I got the next error:

[1:52:21 PM]  ERROR  [worker reload] [worker init] Cannot find module '/home/projects/nuxt-starter-s9qxje/.nuxt/dev/load-js.js' imported from /home/projects/nuxt-starter-s9qxje/.nuxt/dev/index.mjs

The bundler does not include the ".js" file from the/libs directory even if it has an alias in nuxt.config.ts. It will have same issue if renamed to ".mjs".

The issue goes away if I remove "type": "module", from package.json - but it will break out internal tools due Node.js ESM loading error:

❯ node libs/config/cli.mjs
file:///home/projects/nuxt-starter-s9qxje/libs/config/cli.mjs:1
import {loadData} from './load-js.js'
        ^^^^^^^^
SyntaxError: Named export 'loadData' not found. The requested module './load-js.js' is a CommonJS module, which may not support all module.exports as named exports.

Additional context

No response

Logs

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    nitroNitro server engine integrationupstreamIssue caused by a dependency or upstream project🔨 p3-minorMinor issue, should be fixed

    Type

    No fields configured for Bug.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions