Skip to content

mock-aws-s3, aws-sdk, nock dependencies vs devDependencies issue #661

Open

Description

@johnmanko

Usage of mock-aws-s3, aws-sdk, and nock are used in the call stack originating from lib/node-pre-gyp.js (which is the package main), but they are only listed in "devDependencies".

    "aws-sdk": "^2.1012.0",
    "mock-aws-s3": "^4.0.2",
    "nock": "^13.1.4",

This causes a problem with webpack:

ERROR in ../project/node_modules/@mapbox/node-pre-gyp/lib/util/s3_setup.js 43:20-42
Module not found: Error: Can't resolve 'mock-aws-s3' in '/Path/to/project/node_modules/@mapbox/node-pre-gyp/lib/util'
 @ ../project/node_modules/@mapbox/node-pre-gyp/lib/node-pre-gyp.js 15:21-62
 @ ../project/node_modules/bcrypt/bcrypt.js 3:17-48
 @ ./src/lib/server/initialize.ts 3:0-69 9:12-39 21:23-56
 @ ./src/index.ts 12:0-53 51:4-19

ERROR in ../project/node_modules/@mapbox/node-pre-gyp/lib/util/s3_setup.js 76:14-32
Module not found: Error: Can't resolve 'aws-sdk' in '/Path/to/project/node_modules/@mapbox/node-pre-gyp/lib/util'
 @ ../project/node_modules/@mapbox/node-pre-gyp/lib/node-pre-gyp.js 15:21-62
 @ ../project/node_modules/bcrypt/bcrypt.js 3:17-48
 @ ./src/lib/server/initialize.ts 3:0-69 9:12-39 21:23-56
 @ ./src/index.ts 12:0-53 51:4-19

ERROR in ../project/node_modules/@mapbox/node-pre-gyp/lib/util/s3_setup.js 112:15-30
Module not found: Error: Can't resolve 'nock' in '/Path/to/project/node_modules/@mapbox/node-pre-gyp/lib/util'
 @ ../project/node_modules/@mapbox/node-pre-gyp/lib/node-pre-gyp.js 15:21-62
 @ ../project/node_modules/bcrypt/bcrypt.js 3:17-48
 @ ./src/lib/server/initialize.ts 3:0-69 9:12-39 21:23-56
 @ ./src/index.ts 12:0-53 51:4-19

Either those modules should be moved to "dependencies", or the main call chain should never hit them.

Activity

  1. bhallstein commented on Jul 11, 2022

    @bhallstein

    I've encountered the same issue while trying to compile a back-end app with webpack for limited distribution. @johnmanko's analysis definitely looks correct to me.

  2. gaberudy commented on Jul 12, 2022

    @gaberudy

    I encountered this issue when depending on bcrypt, which depends on @mapbox/node-pre-gyp and using rollup as a bundler. These seem to be unnecessary hard dependencies for this package.

  3. johnmanko commented on Jul 12, 2022

    @johnmanko
    Author

    @gaberudy

    I ended up having to remove bcrypt and replaced it with the native node crypto:

            const {
                scryptSync,
                randomBytes
            } = await import('crypto');
    
            if (salted) {
                const hash = scryptSync(password, salted, 64).toString('hex');
                return new HashAndSalt(hash, salted);
            } else {
                const salt = randomBytes(32).toString("hex");
                const hash = await scryptSync(password, salt, 64).toString('hex');
                return new HashAndSalt(hash, salt);
            }
    
  4. JustroX commented on Sep 14, 2022

    @JustroX

    I encountered this issue using nest js. I solved it by removing webpack from compilerOptions

       // I remove this block
      "compilerOptions": {
        "webpack": true
      }
  5. bthibault commented on Sep 22, 2022

    @bthibault

    +1

  6. Karhide commented on Sep 28, 2022

    @Karhide

    +1, encountered depending on node-sqlite3

  7. eau-de-la-seine commented on Oct 11, 2022

    @eau-de-la-seine

    I have encountered the same problem and opened an issue at bcrypt yesterday so they improve their README, but my bad I didn't know that was a recent node-pre-gyp issue. Anyway, dev dependencies should not be used in production files

  8. tintinthong commented on Nov 27, 2022

    @tintinthong

    Is there a workaround for this issue? I face this problem

  9. Glandos commented on Dec 9, 2022

    @Glandos

    Bad workaround, add this to your webpack.config.js:

    const config = {
    // Some other config
    	  externals: [
        {
          'nock': 'commonjs2 nock',
          'mock-aws-s3': 'commonjs2 mock-aws-s3'
        }
      ],
    }

    This will tell webpack to let the require call as-is. It can obviously fail at runtime, and the required statement are still here.

  10. tintinthong commented on Dec 10, 2022

    @tintinthong

    Bad workaround, add this to your webpack.config.js:

    const config = {
    // Some other config
    	  externals: [
        {
          'nock': 'commonjs2 nock',
          'mock-aws-s3': 'commonjs2 mock-aws-s3'
        }
      ],
    }

    This will tell webpack to let the require call as-is. It can obviously fail at runtime, and the required statement are still here.

    Downside of this is that you need to handle all the runtime errors (like nested runtime dependencies) can take a long long time

  11. tobilg commented on Dec 12, 2022

    @tobilg

    I stumbled upon this as well when trying to bundle the DuckDB Node.js package. This additionally attempts to require npm as well :-/ I manually added nock and mock-aws-s3 as dev dependencies, and set npm as external. Then, it still complains that the package's dependencies html and cs files as well.

    Using esbuild is seemingly also not an option, as this produces incorrect builds as well, due to node-pre-gyp...

  12. NowyQuei commented on Dec 13, 2022

    @NowyQuei

    some error here...

  13. therealokoro commented on Dec 31, 2022

    @therealokoro

    Hmmm. So i got same error while using bcrypt in a nuxt app. And i just kept on thinking what nuxt had to do with aws. It was frustrating. I just kept on searching online and stumbled at this

  14. martinszeltins commented on Jan 12, 2023

    @martinszeltins

    I got this error using Vite and sqlite in an Electron.js app

  15. 61 remaining items

  16. cclauss commented on Feb 26, 2025

    @cclauss
    Collaborator

    Can someone please add a GitHub Action to this repo that fails because of this problem? We all need to be able to see this failure.

  17. karlhorky commented on Feb 26, 2025

    @karlhorky

    @cclauss I created a simple bcrypt + @mapbox/node-pre-gyp + esbuild reproduction repo with failing CI over here:

    $ npm run build
      shell: /usr/bin/bash -e {0}
      
    > repro-node-pre-gyp-bcrypt-esbuild@1.0.0 build
    > esbuild index.ts --bundle --platform=node --format=esm --target=node22 --outfile=out.js
    Error: R] Could not resolve "mock-aws-s3"
        node_modules/@mapbox/node-pre-gyp/lib/util/s3_setup.js:43:28:
          43 │     const AWSMock = require('mock-aws-s3');
             ╵                             ~~~~~~~~~~~~~
      You can mark the path "mock-aws-s3" as external to exclude it from the bundle, which will remove this error and leave the unresolved path in the bundle. You can also surround this "require" call with a try/catch block to handle this failure at run-time instead of bundle-time.
    Error: R] No loader is configured for ".html" files: node_modules/@mapbox/node-pre-gyp/lib/util/nw-pre-gyp/index.html
        node_modules/@mapbox/node-pre-gyp/lib/node-pre-gyp.js:86:21:
          86 │       return require('./' + command)(self, argvx, callback);
             ╵                      ~~~~~~~~~~~~~~
    Error: R] Could not resolve "aws-sdk"
        node_modules/@mapbox/node-pre-gyp/lib/util/s3_setup.js:76:22:
          76 │   const AWS = require('aws-sdk');
             ╵                       ~~~~~~~~~
      You can mark the path "aws-sdk" as external to exclude it from the bundle, which will remove this error and leave the unresolved path in the bundle. You can also surround this "require" call with a try/catch block to handle this failure at run-time instead of bundle-time.
    Error: R] Could not resolve "nock"
        node_modules/@mapbox/node-pre-gyp/lib/util/s3_setup.js:112:23:
          112 │   const nock = require('nock');
              ╵                        ~~~~~~
      You can mark the path "nock" as external to exclude it from the bundle, which will remove this error and leave the unresolved path in the bundle. You can also surround this "require" call with a try/catch block to handle this failure at run-time instead of bundle-time.
    4 errors
    Error: Process completed with exit code 1.

    package.json

    {
      "name": "repro-node-pre-gyp-bcrypt-esbuild",
      "version": "1.0.0",
      "type": "module",
      "main": "index.js",
      "scripts": {
        "build": "esbuild index.ts --bundle --platform=node --format=esm --target=node22 --outfile=out.js"
      },
      "dependencies": {
        "bcrypt": "5.1.1"
      },
      "devDependencies": {
        "@types/bcrypt": "5.0.2",
        "esbuild": "0.25.0"
      }
    }

    index.ts

    import bcrypt from 'bcrypt';
    
    console.log(await bcrypt.hash('x', 10));
  18. cclauss commented on Feb 26, 2025

    @cclauss
    Collaborator

    Awesome!!

    Can you please also create a second job that tries bcryptjs instead of bcrypt so we can see the side-by-side results?
    #661 (comment)

  19. karlhorky commented on Feb 26, 2025

    @karlhorky

    @cclauss done:

    npm run build-bcryptjs
    
    > repro-node-pre-gyp-bcrypt-esbuild@1.0.0 build-bcryptjs
    > esbuild index-bcryptjs.ts --bundle --platform=node --format=esm --target=node22 --outfile=out.js
    
    
      out.js  30.9kb
    
    ⚡ Done in 4ms
    

    index-bcryptjs.ts

    import bcrypt from 'bcryptjs';
    
    console.log(await bcrypt.hash('x', 10));

    package.json script

    "build-bcryptjs": "esbuild index-bcryptjs.ts --bundle --platform=node --format=esm --target=node22 --outfile=out.js"
  20. youssefnassim commented on Jul 4, 2025

    @youssefnassim

    same here, had the issue with bcrypt which depends on all of these (as dev deps), switched to bcryptjs and i'm finally moving forward after hours of debugging 😅

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions