auth70 / paseto-ts

PASETO v4 (encrypt, decrypt, sign & verify) in TypeScript
MIT License
19 stars 6 forks source link

Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: Package subpath './v4' is not defined by "exports" #5

Closed Elsoberanold closed 7 months ago

Elsoberanold commented 7 months ago

First of all, I want congratulate you for the work that you have done! In my current project, I have tried implement this library but unfortunately I have ran into this issue: Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: Package subpath './v4' is not defined by "exports"

Here is a minimal example to reproduce this error:

import * as p from "paseto-ts/v4"

const tokens_secret = "secret goes here";
const key = `v4.local.${Buffer.from(tokens_secret).toString("base64url")}`;
const token ="v4.local.qLuOv0OzM2-Ih3dMsNr4j3wehJEI5UORki7pxD9aF2RdKuyeVBzAhzc4QOILIvNPMxIgCAw-QqEshERWJZlSafg84ePxXc6kmtmPYpARzT7sTJahbhqtyDV0IQgabVdJaaxVaYhPWFHiib58zxRGJCEOZMej6b3DuUW-DkMsUfDj65TCc-g0teRuRlWfLr009AAZ7_prHUovfvAc3buf07Hi5tWpSPFN5HmqqrS5YIPemWjmMroO7m6VE--IQMWKLJ1sGXo4Vqf7UmZlq9vZiBC-LbRgOlugJsK45kZNBFBanFRCTU98FwBsQtoNebcXvfU0iqlPd1CZTDhej2AQTGVxOWogW77YoX4ef-vo3iN15ARQC8ubtoRSF9gf_csCB1N3C9Z4uVyjgENEFFkPIXpLwrmXm6cNa6juNLOhzdOFaJIFMaF8PQXKZyoLxXFwkPdLveFtDKxuY9qKSPNoaHLzHapazptM3cZ7w2gZJHw9Em43h-1HNFpAHH-Wb9aUju9iwuIc6orL3WiFxkHyXNPZr4Pnc8VIgU6VZSybyPyvSMd5BHNwDqzqE1R1NY0q8a4XJ8sa2z4kxZfn-IiwVBzaoXt8Ml0SWAOWMyr7LD3VaT--V40V8GGhcRX121Ytf9gIsQ89ktIlV5NMNANis1yqKe_Hd1qXVFn4bt0";

console.log(tokens_secret);
try {
  const { payload, footer } = p.decrypt(key, token);

  console.log(payload, footer);
} catch (error) {}

This example was tested with Node.JS version v18.12.1

I am not sure but perhaps it might be a problem related to exports in package.json... What do you think?

miunau commented 7 months ago

Hi! That's odd- I can make a new project and run your code just fine on 18.12.1. Is your package.json a type: "module"?

Elsoberanold commented 7 months ago

Hi! That's odd- I can make a new project and run your code just fine on 18.12.1. Is your package.json a type: "module"?

Thanks for replying 🙌🙌 Not at all... here is my tsconfig:

{
  "compilerOptions": {
    "target": "es2016",
    "module": "commonjs",
    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": true,
    "strict": true,
    "skipLibCheck": true
  },
  "exclude": [
    "node_modules"
  ],
  "include": [
    "./src/**/*.tsx",
    "./src/**/*.ts"
  ]

}

and my package.json


{
  "name": "test",
  "version": "1.0.0",
  "description": "",
  "main": "test2.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "dev": "NODE_ENV=development nodemon --exec ts-node src/test-paseto.ts"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "@types/node": "^20.9.0",
    "nodemon": "^3.0.1",
    "ts-node": "^10.9.1",
    "typescript": "^5.2.2"
  },
  "dependencies": {
    "paseto-ts": "^1.5.1"
  }
}
miunau commented 7 months ago

Try putting "type": "module" in your package.json. This is an ES module (not commonjs).

Elsoberanold commented 7 months ago

Try putting "type": "module" in your package.json. This is an ES module (not commonjs).

It works now! Thank you by the help!! I just had to change a few things: Tsconfig:

{
  "compilerOptions": {
    "target": "ESNext",
    "module": "NodeNext",
    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": true,
    "strict": true,
    "skipLibCheck": true
  },
  "exclude": [
    "node_modules"
  ],
  "include": [
    "./src/**/*.tsx",
    "./src/**/*.ts"
  ]

}

and package.json:

{
  "name": "test",
  "version": "1.0.0",
  "description": "",
  "main": "test2.js",
  "type": "module",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "dev": "NODE_ENV=development nodemon --exec ts-node --esm src/test-paseto.ts"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "@types/node": "^20.9.0",
    "nodemon": "^3.0.1",
    "ts-node": "^10.9.1",
    "typescript": "^5.2.2"
  },
  "dependencies": {
    "paseto-ts": "^1.5.1"
  }
}