paseto-ts icon indicating copy to clipboard operation
paseto-ts copied to clipboard

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

Open dawadam opened this issue 1 year ago • 12 comments

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

dawadam avatar Sep 13 '24 20:09 dawadam

Is your package an ES module?

miunau avatar Sep 14 '24 07:09 miunau

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

dawadam avatar Sep 16 '24 18:09 dawadam

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

miunau avatar Sep 16 '24 18:09 miunau

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

dawadam avatar Sep 17 '24 11:09 dawadam

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

miunau avatar Sep 24 '24 18:09 miunau

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"
  );

opsb avatar Nov 11 '24 10:11 opsb

I've removed the typesVersions - maybe this helps.

miunau avatar Mar 26 '25 06:03 miunau

I've removed the typesVersions - maybe this helps.

Still having the same issue with TS (ESM import/export) :/

Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: Package subpath './v4' is not defined by "exports" in /***/packages/api/node_modules/paseto-ts/package.json

I've tried all possible combinations of import path and tsconfig options, still not working.

FYI: I'm using tsx.

avetisk avatar Mar 27 '25 22:03 avetisk

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

After battling a lot I finally ended up with this:

  1. "type": "module" in your package.json
  2. "module": "ESNext" and "moduleResolution": "bundler" in your tsconfig.json.

This allowed me to use paseto-ts while having TS with custom paths.

Hope this helps.

avetisk avatar Mar 28 '25 22:03 avetisk

I apologise, I realised I had left over old defs in the tsconfig. The configuration should work now as I've set esnext as the target and bundler as the module resolution method. I cut a version above 2.x.x as it might break someone's flow.

miunau avatar Apr 12 '25 16:04 miunau

How to use it in nestjs? error: Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: Package subpath './v4' is not defined by "exports" Node.js v24.6.0

sstifler avatar Aug 21 '25 11:08 sstifler

NestJS does currently not support to compile to ESM

In node v22 there was With Node v22 you can use --experimental-require-module. But in node v24 it was removed.

For me the best solution:

async generateLocalKey(): Promise<any> {
    const paseto = await import('paseto-ts/v4');
    return paseto.generateKeys('local');
  }

more info: https://stackoverflow.com/questions/74830166/unable-to-import-esm-module-in-nestjs

sstifler avatar Aug 21 '25 15:08 sstifler