paseto-ts
paseto-ts copied to clipboard
Error: Package subpath './v4' is not defined by "exports"
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
Is your package an ES module?
I am using ES module syntax (import/export) with TypeScript. However, it's transpiled with ts-node for use in a Node.js environment.
Can you please make a reproduction repo? I haven't used ts-node
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
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
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"
);
I've removed the typesVersions - maybe this helps.
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.
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:
"type": "module"in yourpackage.json"module": "ESNext"and"moduleResolution": "bundler"in yourtsconfig.json.
This allowed me to use paseto-ts while having TS with custom paths.
Hope this helps.
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.
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
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