dajiaji / hpke-js

A Hybrid Public Key Encryption (HPKE) module built on top of Web Cryptography API.
https://dajiaji.github.io/hpke-js/docs
MIT License
58 stars 9 forks source link

Cannot find module './src/cipherSuite.js' #302

Open OR13 opened 4 months ago

OR13 commented 4 months ago
_sync:2 Uncaught Error: Cannot find module './src/cipherSuite.js'
    at webpackEmptyContext (_sync:2:10)
    at eval (mod.js:30:28)
    at eval (mod.js:17:17)
    at eval (mod.js:26:3)
    at ./node_modules/hpke-js/script/mod.js (main.js:372:1)
    at __webpack_require__ (main.js:726:42)
    at eval (app.js:40:17)
    at ./app.js (main.js:19:1)
    at __webpack_require__ (main.js:726:42)
    at eval (index.js:2:62)

This error occurs when bundling with web pack, a typescript application, that included hpke-js.

I created a PoC here: https://github.com/dajiaji/hpke-js/pull/301

dajiaji commented 4 months ago

Thanks for the report. I'll check tomorrow.

OR13 commented 4 months ago

If I figure it out before then, I will update the PR and mark it "not draft", with some comments.

OR13 commented 4 months ago

Please don't let my comments here keep you awake.

Interestingly switching from:

import { AeadId, CipherSuite, KdfId, KemId } from "hpke-js";
import { AeadId, CipherSuite, KdfId, KemId } from "@hpke/core";

causes a different error:

of the same form:

Uncaught Error: Cannot find module '../src/aeads/aesGcm.js'

Seems to be the first "none type" export here:

https://github.com/dajiaji/hpke-js/blob/main/core/mod.ts#L16

Screenshot 2024-02-24 at 8 58 17 AM

Seems like webpack needs to be instructed to use node modules resolve relative paths in dependencies properly

Possibly relevant: https://github.com/webpack/webpack/issues/472

This worked:

const path = require('path');
module.exports = [{
  mode: 'development',
  entry: './index.js',
  watch: true,
  plugins: [],
  resolve: {
    alias: {
      '@hpke/core': path.resolve('./node_modules/@hpke/core')
    },
    fallback: { 
      "crypto": false
    }
 }
}];
dajiaji commented 4 months ago

Please don't let my comments here keep you awake.

Don't worry. I fell asleep before I saw your comment above :-) I'll now check your PR.

OR13 commented 4 months ago

For those trying to use this library in next 14 or greater:

You can update webpack used by next, like this:

https://nextjs.org/docs/app/api-reference/next-config-js/webpack

const path = require("path");

module.exports = {
  webpack: (
    config,
    { buildId, dev, isServer, defaultLoaders, nextRuntime, webpack }
  ) => {
    // Important: return the modified config
    return {
      ...config, 
      resolve: {
        ...config.resolve,
        alias: {
          ...config.resolve.alias,
          "hpke-js": path.resolve("./node_modules/hpke-js"),
        },
        fallback: {
          ...config.resolve.fallback,
          "crypto": false,
        },
      },
    }
  },
}