Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to bundle bcrypt #3977

Closed
northern opened this issue Jan 10, 2020 · 1 comment
Closed

Unable to bundle bcrypt #3977

northern opened this issue Jan 10, 2020 · 1 comment

Comments

@northern
Copy link

🐛 bug report

When I try to bundle a Node project there is a problem with bundling the bcrypt module. It seems that this module has a decency on externals/native modules for which it gets it locations from package.json. This causes a problem for the bundled output file since package.json is not available.

Webpack seems to have had a similar issue: angular/universal#1207

🎛 Configuration (.babelrc, package.json, cli command)

{
  "name": "test",
  "version": "1.0.0",
  "description": "",
  "main": "src/index.js",
  "scripts": {
    "build": "NODE_ENV=production parcel build ./src/index.js -d ./bin --target node --no-source-maps --bundle-node-modules"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "bcrypt": "^3.0.7"
  },
  "devDependencies": {
    "parcel-bundler": "^1.12.4"
  }
}

🤔 Expected Behavior

Bundled output should work just like running the non-bundled source.

😯 Current Behavior

The following error is presented when running the bundled output:

parcelRequire=function(e,r,t,n){var i,o="function"==typeof parcelRequire&&parcelRequire,u="function"==typeof require&&require;function f(t,n){if(!r[t]){if(!e[t]){var i="function"==typeof parcelRequire&&parcelRequire;if(!n&&i)return i(t,!0);if(o)return o(t,!0);if(u&&"string"==typeof t)return u(t);var c=new Error("Cannot find module '"+t+"'");throw c.code="MODULE_NOT_FOUND",c}p.resolve=function(r){return e[t][1][r]||r},p.cache={};var l=r[t]=new f.Module(t);e[t][0].call(l.exports,p,l,l.exports,this)}return r[t].exports;function p(e){return f(p.resolve(e))}}f.isParcelRequire=!0,f.Module=function(e){this.id=e,this.bundle=f,this.exports={}},f.modules=e,f.cache=r,f.parent=o,f.register=function(r,t){e[r]=[function(e,r){r.exports=t},{}]};for(var c=0;c<t.length;c++)try{f(t[c])}catch(e){i||(i=e)}if(t.length){var l=f(t[t.length-1]);"object"==typeof exports&&"undefined"!=typeof module?module.exports=l:"function"==typeof define&&define.amd?define(function(){return l}):n&&(this[n]

Error: package.json does not exist at /bin/package.json
    at Object.exports.find (/bin/index.js:164:360)
    at Object.parcelRequire.OGsn.node-pre-gyp (/bin/index.js:172:66)
    at f (/bin/index.js:1:468)
    at p (/bin/index.js:1:544)
    at Object.parcelRequire.Focm.bcrypt (/bin/index.js:174:7)
    at f (/bin/index.js:1:468)
    at parcelRequire.UIdH (/bin/index.js:1:771)
    at Object.<anonymous> (/bin/index.js:1:1023)
    at Module._compile (internal/modules/cjs/loader.js:936:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:947:10)

💁 Possible Solution

Unfortunately I have not enough knowledge of the externals of Parcel to make any suggestions.

🔦 Context

💻 Code Sample

Super simple script that demonstrates the problem.

var bcrypt = require('bcrypt');

const makeHash = async (password, saltRounds = 10) => bcrypt.hash(password, saltRounds);

makeHash('s3cr3t', 10);

🌍 Your Environment

Software Version(s)
Parcel ^1.12.4
Node 12
npm/Yarn npm 6.13.1
Operating System Linux
@mischnic
Copy link
Member

mischnic commented Jan 10, 2020

The issue is that node-pre-gyp (which is called by bcrypt) doesn't like being bundled. It searches for bcrypt's package.json where it would find

  "binary": {
    "module_name": "bcrypt_lib",
    "module_path": "./lib/binding/",
    "package_name": "{module_name}-v{version}-{node_abi}-{platform}-{arch}-{libc}.tar.gz",
    "host": "https://github.com/kelektiv/node.bcrypt.js/releases/download/",
    "remote_path": "v{version}"
  }

But neither the package.json (for obvious reasons) nor the native module(s) (because that isn't determined statically) are copied to the dist folder.

From node-pre-gyp issue (mapbox/node-pre-gyp#308):

Is node-pre-gyp supposed to work with Webpack... or not ?

No.


Related issue for Parcel 2: #2493

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants