Skip to content

Argon2 does not work with webpack #261

Closed

Description

@jayd-mh

Steps to reproduce

  1. Install and include argon in your project
  2. run your application e.g. via nodemon - everything works fine
  3. run your application via serverless (after creating a webpack build) -> an error is thrown.

Expected behaviour

I'm having issues using argon2 in my project when building it with webpack and running it with serverless.
The following lines in the argon2.js file seem to be a problem for webpack (webpack cannot resolve dynamic imports/requires):

const binary = require('node-pre-gyp')
const path = require('path')
const bindingPath = binary.find(path.resolve(path.join(__dirname, './package.json')))

const { hash: _hash, limits, types, names, version } = require(bindingPath) /* eslint-disable-line */

it is working if you change above lines to:

const { hash: _hash, limits, types, names, version } = require("argon2/lib/binding/napi-v3/argon2.node")

Any suggestions how to fix this? (is there anything I can do differently?)

Actual behaviour

Error: Cannot find module '/[....]/node_modules/argon2/package.json'
    at webpackEmptyContext (/[....]/packages/server/.webpack/service/dist/src/webpack:/[....]/node_modules/node-pre-gyp/lib sync:2:1)
    at Object.module.exports.../../node_modules/node-pre-gyp/lib/pre-binding.js.exports.find (/[....]/packages/server/.webpack/service/dist/src/webpack:/[....]/node_modules/node-pre-gyp/lib/pre-binding.js:20:23)
    at Object.<anonymous> (/[....]/packages/server/.webpack/service/dist/src/webpack:/[....]/node_modules/argon2/argon2.js:8:1)
    at Object.../../node_modules/argon2/argon2.js (/[....]/packages/server/.webpack/service/dist/src/serverless.js:31908:30)
    at __webpack_require__ (/[....]/packages/server/.webpack/service/dist/src/webpack:/webpack/bootstrap:19:1)

Environment

Operating system: MacOS - Catalina 10.15.5

Node version: v12.17.0
Webpack version:* 4.42.0

Activity

  1. jayd-mh commented on Jun 5, 2020

    @jayd-mh
    Author

    I'm sorry, but a change in the webpack.config.js fixed the problem.
    Instead of:
    externals: [nodeExternals()],
    I needed to include argon2 explicitly in the externals:

      externals: {
        "argon2": "argon2",
      },
    
  2. ranisalt commented on Jun 5, 2020

    @ranisalt
    Owner

    Can you update the README or create an article in the wiki for us? :)

  3. jayd-mh commented on Jun 5, 2020

    @jayd-mh
    Author

    I'm not quite sure if it is the right way to do it (or just a workaround).
    But as soon as I am sure, I will provide you the information!

  4. added a commit that references this issue on Mar 1, 2022
    0d9f0f8
  5. haywirez commented on Jun 19, 2023

    @haywirez

    After a few hours wasted, the below config finally works for me 😉 Specifying the commonjs ${libraryName} externals type for webpack was crucial.

    // in a v5.x webpack.config.js
    externals: {   
        argon2: 'commonjs argon2',
    },
  6. di-sukharev commented on Nov 7, 2024

    @di-sukharev

    i used Bun.password.hash(), it uses argon2 under the hood and it just works

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions