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

Q: How to import spellCheckDocument from cspell-lib using tsx from a package with type module or commonjs? #6711

Open
1 task done
acenturyandabit opened this issue Dec 26, 2024 · 1 comment

Comments

@acenturyandabit
Copy link

Kind of Issue

Change in behavior / Regression

Tool or Library

cspell-lib

Version

8.17.1

Supporting Library

Not sure / None

OS

Linux

OS Version

wsl

Description

When I try:

// a.ts
import { spellCheckDocument } from 'cspell-lib';
(async () => {
  console.log(await spellCheckDocument(
    { uri: 'text.txt', text: "hello world", languageId: 'plaintext', locale: 'en' },
    { generateSuggestions: true, noConfigSearch: true },
    { suggestionsTimeout: 2000 }
  ))
})();

and run

tsx a.ts

I get

Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: No "exports" main defined in /.../node_modules/cspell-lib/package.json

When I add "type": "module" to my package.json, this fixes the issue, but breaks another local dependency which I've imported which is commonjs.

Steps to Reproduce

No response

Expected Behavior

No response

Additional Information

No response

cspell.json

No response

cspell.config.yaml

No response

Example Repository

No response

Code of Conduct

  • I agree to follow this project's Code of Conduct
@Jason3S
Copy link
Collaborator

Jason3S commented Dec 26, 2024

@acenturyandabit,

Please try using the await import():

// a.ts
(async () => {
    const { spellCheckDocument } = await import('cspell-lib');
    console.log(await spellCheckDocument(
    { uri: 'text.txt', text: "hello worldz", languageId: 'plaintext', locale: 'en' },
    { generateSuggestions: true, noConfigSearch: true },
    { suggestionsTimeout: 2000 }
  ))
})();

Since cspell-lib is an ESM Module, it cannot be used with import .. from in a commonjs package. Instead, it is necessary to use import().

I hope this helps.

@Jason3S Jason3S changed the title [Bug]: Importing cspell-lib gives Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: No "exports" main defined in node_modules/cspell-lib/package.json Q: How to import spellCheckDocument from cspell-lib using tsx from a package with type module or commonjs? Dec 26, 2024
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

2 participants