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

[Bug] Missing .js File Extensions in Module Exports Causes Import Errors in Node.js #41

Open
Siykt opened this issue Nov 6, 2024 · 0 comments

Comments

@Siykt
Copy link

Siykt commented Nov 6, 2024

I am experiencing an issue with the package when trying to import it in a Node.js environment that strictly requires file extensions in ES Module imports. The package is specified as an ES module ("type": "module" in package.json), but the exported files in the distribution do not include the .js file extensions. This results in import failures when the package is used in environments that adhere to the Node.js module resolution algorithm for ES Modules.

Details

Node.js, from version 12 onwards, enforces the inclusion of file extensions for ES Modules. However, the current exports in @ston-fi/sdk are missing these required .js extensions, which leads to errors such as:

error TS2305: Module '"@ston-fi/sdk"' has no exported member 'DEX'.
error TS2305: Module '"@ston-fi/sdk"' has no exported member 'pTON'.

Given the use of vite.js for your development and potential deployment setups, a switch to tsup for library packaging might resolve this issue, as it supports better configuration options for both ESM and CommonJS formats.

Suggested Fix

I recommend updating the package build process to include .js file extensions in the module export paths. Here is a suggested tsup.config.ts configuration that ensures proper file extensions:

import { defineConfig } from 'tsup';

export default defineConfig(options => {
  return {
    entryPoints: ['src/'],
    outDir: 'dist',
    dts: true,
    sourcemap: !options.watch,
    clean: true,
    splitting: true,
    format: ['esm', 'cjs'], // This ensures both formats are built with appropriate file extensions
  };
});

and package.json:

{
  "exports": {
    "./package.json": "./package.json",
    ".": {
      "types": "./dist/index.d.ts",
      "import": "./dist/index.js",
      "default": "./dist/index.cjs"
    }
  }
}

This configuration specifies multiple formats (ESM and CJS) and includes explicit settings to ensure file extensions are correct, thus making your SDK compatible with standard Node.js module resolution rules and preventing import errors for users in strict ES Module environments.

@Siykt Siykt mentioned this issue Nov 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

1 participant