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

Directory import is not supported resolving ES modules #1910

Open
thobson opened this issue Oct 20, 2023 · 6 comments
Open

Directory import is not supported resolving ES modules #1910

thobson opened this issue Oct 20, 2023 · 6 comments

Comments

@thobson
Copy link

thobson commented Oct 20, 2023

🐛 Bug report

Firstly, apologies but I'm not sure if this is an fp-ts issue or a vite/webpack/sveltekit issue but I'd be grateful for any pointers ...

Current Behavior

I'm importing using this syntax:

import { pipe } from "fp-ts/function";

When running vite dev everything works fine, however after building and running the svelte kit app I get this error:

Directory import 'node_modules/fp-ts/function' is not supported resolving ES modules imported from .svelte-kit/output/server/entries/pages/_page.svelte.js

Additional context

I can work around the issue by importing like this:

import { pipe } from "fp-ts/es6/function";

However this then raises an error during vite dev

.pnpm/[email protected]/node_modules/fp-ts/es6/function.js:17
export var getBooleanAlgebra = function (B) {
^^^^^^

SyntaxError: Unexpected token 'export'

Your environment

Software Version(s)
fp-ts 2.16.1
TypeScript 5.2.2
vite 4.5.0
@thobson
Copy link
Author

thobson commented Oct 20, 2023

Quick update:

importing from lib seems to resolve the issue, but if I'm honest I don't understand why 😄

import { pipe } from "fp-ts/lib/function";

I'll leave the ticket here in case someone else runs into the same problem

@ninsy
Copy link

ninsy commented Mar 14, 2024

Same happened on my local machine, directly using fp-ts. Used npm version is 10.2.4 and I've set "type": "module" within package.json.

"Fix" from @thobson comment appears to work

@samhh
Copy link
Contributor

samhh commented Mar 14, 2024

For anyone wondering, the fp-ts ecosystem of libraries don't currently support ESM, instead only ES6 and CJS. When you explicitly import from fp-ts/lib/* you're effectively specifying that you want to import CJS, which works because ESM can generally import CJS but not ES6. If you're using a bundler you'll want to rewrite those imports back to fp-ts/es6/* to avoid bundle bloat.

@jderochervlk
Copy link

I recently hit this issue and I think I might have a fix.

Please hold...

@jderochervlk
Copy link

Ok, in order to fix this issue the /es6/ output would need to be updated and changes would need to be made to the package.json file.

In the package.json the existing fields for main, module, and typings, would need to be removed and replaced with the exports field for each module, like this:

  "exports": {
    "./Option": {
      "import": "./es6/Option.js",
      "require": "./lib/Option.js",
      "types": "./lib/Option.d.ts"
    }
  },

And files inside the es6 folder would need to include the file extensions for all imports, like this:

import { getApplicativeMonoid } from './Applicative.js';
import { apFirst as apFirst_, apS as apS_, apSecond as apSecond_, getApplySemigroup as getApplySemigroup_ } from './Apply.js';
import * as chainable from './Chain.js';

@jderochervlk
Copy link

Since this is just using tsc as the compiler you would need to write the source files with the file extension. TypeScript can enforce this and make sure modules can be resolved if you set "moduleResolution": "NodeNext" in the main tsconfig.json file.

// this
import { foo } from './lib' // Relative import paths need explicit file extensions in ECMAScript imports when '--moduleResolution' is 'node16' or 'nodenext'. Did you mean './lib.js'?

// would have to become this
import { add } from './lib.js'

thewilkybarkid added a commit to PREreview/prereview.org that referenced this issue Jun 13, 2024
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

No branches or pull requests

4 participants