GithubHelp home page GithubHelp logo

exports-import-not-es-module's Introduction

Unable to import library that uses package.json exports with "." and "import" & "require" entries

To reproduce:

  1. Install pnpm: npm i -g pnpm
  2. Install deps: pnpm i -r
  3. Package library: pnpm run build:pkg --filter @app/library
  4. Build client: pnpm run build --filter @app/client

Observe results.

> Named export 'WebsocketProvider' not found. The requested module 'y-websocket' 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 'y-websocket';
const { WebsocketProvider } = pkg;
file:///.../exports-import-not-es-module/packages/client/.svelte-kit/output/server/app.js:24
import { WebsocketProvider } from "y-websocket";

The package.json in question https://github.com/yjs/y-websocket/blob/master/package.json#L28

  "exports": {
    "./package.json": "./package.json",
    ".": {
      "import": "./src/y-websocket.js",
      "require": "./dist/y-websocket.cjs"
    }
  },

You can see that Rollup and Vite perform this bundling correctly with using:

pnpm run vite --filter @app/rollup
pnpm run roll --filter @app/rollup

If you remove the exports block and let the y-websocket be resolved via "main" and "module" entries the build succeeds so I think there's something wrong with how SvelteKit resolves the dependencies.

There is also additional problem with importing dependencies of the library from the client. I had to add the library's dependencies to the client, otherwise the bundler (rollup?) won't find them. I think pnpm's symlinks are not resolved properly.

exports-import-not-es-module's People

Contributors

teemukoivisto avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.