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

Error: Package subpath './v4' is not defined by "exports" #9

Open
dawadam opened this issue Sep 13, 2024 · 6 comments
Open

Error: Package subpath './v4' is not defined by "exports" #9

dawadam opened this issue Sep 13, 2024 · 6 comments

Comments

@dawadam
Copy link

dawadam commented Sep 13, 2024

Hi, error appear when i'm testing :

Error: Package subpath './v4' is not defined by "exports" in /node_modules/paseto-ts/package.json

My import is just : import { encrypt, decrypt, generateKeys } from 'paseto-ts/v4';

I'm testing with ts-node

@miunau
Copy link
Collaborator

miunau commented Sep 14, 2024

Is your package an ES module?

@dawadam
Copy link
Author

dawadam commented Sep 16, 2024

I am using ES module syntax (import/export) with TypeScript.
However, it's transpiled with ts-node for use in a Node.js environment.

@miunau
Copy link
Collaborator

miunau commented Sep 16, 2024

Can you please make a reproduction repo? I haven't used ts-node

@dawadam
Copy link
Author

dawadam commented Sep 17, 2024

Here is the step-by-step guide to initialize a TypeScript project, install the necessary libraries, and test with ts-node, all in one go.

1. Initialize the TypeScript Project and Install Dependencies

Create a directory for your project, initialize it with npm, and install TypeScript, ts-node, and paseto-ts in one step:

mkdir my-paseto-project
cd my-paseto-project
npm init -y
npm install typescript ts-node @types/node --save-dev
npm install paseto-ts

2. Set up tsconfig.json

Create a tsconfig.json file to configure TypeScript with ESM (ECMAScript Modules) support:

{
  "compilerOptions": {
    "module": "ES2022",
    "target": "ES2022",
    "moduleResolution": "Node",
    "strict": true,
    "esModuleInterop": true,
    "skipLibCheck": true
  },
  "ts-node": {
    "esm": true
  }
}

3. Minimal Example

Create a file named index.ts with the following code to test key generation and encryption using paseto-ts:

import { generateKeys, encrypt, decrypt } from 'paseto-ts/v4';

async function main() {
  // Generate a local key for encryption
  const localKey = await generateKeys('local');

  // Payload to encrypt
  const payload = { sub: '1234567890', name: 'John Doe' };

  // Encrypt the payload
  const token = await encrypt(localKey, payload, { addExp: false });

  console.log('Token:', token);

  // Decrypt the token
  const decryptedPayload = await decrypt(localKey, token);

  console.log('Decrypted Payload:', decryptedPayload);
}

main().catch(console.error);

4. Run the Example with ts-node

Run the TypeScript file using ts-node:

npx ts-node index.ts

@miunau
Copy link
Collaborator

miunau commented Sep 24, 2024

Thanks for the instructions, I'm a bit loaded with work but will take a look at this once I have a moment. You could try removing the typesVersions from the package.json and see if that helps, that was my idea

@opsb
Copy link

opsb commented Nov 11, 2024

As a workaround you can use a dynamic import with a relative path

const { sign } = await import(
    "../../node_modules/paseto-ts/dist/v4/index.js"
  );

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

3 participants