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

Named export not found for an ESM module when running svelte-kit build #1498

Closed
benwoodward opened this issue May 20, 2021 · 3 comments · Fixed by MacFJA/svelte-persistent-store#4

Comments

@benwoodward
Copy link

benwoodward commented May 20, 2021

This issue occurs when an adapter is added to the config (tested with netlify), and I run npm run build.

So far I've seen the error from multiple dependencies, but my testing of the issue has focussed on https://github.com/devlato/async-wait-until/

A above dependency ships an ESM formatted JS module.

The problem is that even though the module has a named export and is ESM, the named export is not detected, and the file is recognised as being CommonJS.

If I use the default export, there are no issues:

  import waitUntil from 'async-wait-until'

However, if I use a named export:

  import { waitUntil } from 'async-wait-until/dist/index.esm.js';
  import { waitUntil } from 'async-wait-until'

Then I get the following error on npm run build:

file:///Users/ben/dev/esm-issue-repro/.svelte-kit/output/server/app.js:4
import {waitUntil} from "async-wait-until/dist/index.esm.js";
        ^^^^^^^^^
SyntaxError: Named export 'waitUntil' not found. The requested module 'async-wait-until/dist/index.esm.js' is a CommonJS module, which may not support all module.exports as named exports.
CommonJS modules can always be imported via the default export, for example using:

import pkg from 'async-wait-until/dist/index.esm.js';
const {waitUntil} = pkg;

    at ModuleJob._instantiate (internal/modules/esm/module_job.js:97:21)
    at async ModuleJob.run (internal/modules/esm/module_job.js:142:20)
    at async Loader.import (internal/modules/esm/loader.js:182:24)
    at async prerender (file:///Users/ben/dev/esm-issue-repro/node_modules/@sveltejs/kit/dist/chunks/index5.js:79:14)
    at async Object.prerender (file:///Users/ben/dev/esm-issue-repro/node_modules/@sveltejs/kit/dist/chunks/index5.js:296:5)
    at async adapt (file:///Users/ben/dev/esm-issue-repro/node_modules/@sveltejs/adapter-netlify/index.js:33:4)
    at async adapt (file:///Users/ben/dev/esm-issue-repro/node_modules/@sveltejs/kit/dist/chunks/index5.js:322:2)
    at async file:///Users/ben/dev/esm-issue-repro/node_modules/@sveltejs/kit/dist/cli.js:616:5
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! esm-issue-repro@0.0.1 build: `svelte-kit build`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the esm-issue-repro@0.0.1 build script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/ben/.npm/_logs/2021-05-19T22_37_22_686Z-debug.log

To reproduce:

Clone this repo: https://github.com/benwoodward/sveltekit-named-export-issue-repro, and then swap the default export import for one of the named export imports, and then run npm run build

I have tested this with node versions 12.22.1, 14.16.1, and 16.1.0.

@benwoodward benwoodward changed the title Named export not found for an ESM module Named export not found for an ESM module when running svelte-kit build May 20, 2021
@benmccann
Copy link
Member

benmccann commented May 22, 2021

I can't quite figure out what's going on or how this is supposed to work. Maybe someone who knows more about the Node module resolution would know. I don't think it's related to Vite. The Vite build seems to have already completed successfully. And the stack trace seems to be coming from Node itself

You can see that it happens during prerendering if you change package.json to:

    "build": "svelte-kit build --verbose",

Node really wants to load the CommonJS version for some reason. The code for the Node.js module resolution is here: https://github.com/nodejs/node/blob/master/lib/internal/modules/esm/resolve.js

By looking at it a bit I figured out that you can make it work with:

  "type": "module",
  "exports": {
    "import": "./dist/index.esm.js",
    "require": "./dist/index.js"
  }

That's not an ideal exports map because it should also export package.json. I was just demonstrating a fix. I can't say whether there's a bug here or not, but if there is, I think it would be in either Node or async-wait-until. I think the most actionable thing to do would be to ask async-wait-until to add an exports map

@benwoodward
Copy link
Author

Thanks @benmccann

@khromov
Copy link

khromov commented Aug 12, 2022

Had the same issue with the monolite package:

Named export 'set' not found. The requested module 'monolite' is a CommonJS module, which may not support all module.exports as named exports.

The solution is as suggested to use the default export, so instead of:

import { set } from 'monolite';
set(...);

Use the default export:

import monolite from 'monolite';
monolite.set(...);

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

Successfully merging a pull request may close this issue.

3 participants