ardatan / meteor-webpack

https://medium.com/@ardatan/meteor-with-webpack-in-2018-faster-compilation-better-source-handling-benefit-from-bc5ccc5735ef
MIT License
123 stars 29 forks source link

Cannot use bcrypt in server #31

Closed dobesv closed 6 years ago

dobesv commented 6 years ago

Modifying the example to import bcrypt results in all kinds of errors. What is the way to use module that webpack / babel don't accept, cannot handle? It seems like marking them as externals doesn't work because meteor doesn't allow them to be imported.

$ pwd
/home/ubuntu/meteor-webpack/examples/react
$ git diff
diff --git a/examples/react/imports/api/tasks.js b/examples/react/imports/api/tasks.js
index d7d72dd..aed05d0 100644
--- a/examples/react/imports/api/tasks.js
+++ b/examples/react/imports/api/tasks.js
@@ -1,6 +1,7 @@
 import { Meteor } from 'meteor/meteor';
 import { Mongo } from 'meteor/mongo';
 import { check } from 'meteor/check';
+import bcrypt from 'bcrypt';

 export const Tasks = new Mongo.Collection('tasks');

@@ -60,6 +61,8 @@ Meteor.methods({
     check(taskId, String);
     check(setToPrivate, Boolean);

+    bcrypt.hashSync('bacon', 8);
+
     const task = Tasks.findOne(taskId);

     // Make sure only the task owner can make a task private
diff --git a/examples/react/package.json b/examples/react/package.json
index 743fa5e..cba227d 100644
--- a/examples/react/package.json
+++ b/examples/react/package.json
@@ -6,7 +6,7 @@
   },
   "dependencies": {
     "@babel/runtime": "7.0.0-beta.46",
-    "bcrypt": "^1.0.3",
+    "bcrypt": "^3.0.0",
     "classnames": "^2.2.5",
     "meteor-node-stubs": "^0.3.3",
     "react": "^16.3.2",

$ yarn start

...
W20180712-16:05:01.799(-7)? (STDERR)     WARNING in /home/ubuntu/meteor-webpack/examples/react/node_modules/bcrypt/bcrypt.js 6:15-36
W20180712-16:05:01.799(-7)? (STDERR)     Critical dependency: the request of a dependency is an expression
W20180712-16:05:01.799(-7)? (STDERR)      @ /home/ubuntu/meteor-webpack/examples/react/imports/api/tasks.js
W20180712-16:05:01.799(-7)? (STDERR)      @ /home/ubuntu/meteor-webpack/examples/react/client/ui/App.jsx
W20180712-16:05:01.799(-7)? (STDERR)      @ /home/ubuntu/meteor-webpack/examples/react/client/main.jsx
W20180712-16:05:01.799(-7)? (STDERR)      @ multi ./client/main.jsx webpack-hot-middleware/client?reload=true
W20180712-16:05:01.799(-7)? (STDERR)     
W20180712-16:05:01.799(-7)? (STDERR)     WARNING in /home/ubuntu/meteor-webpack/examples/react/node_modules/bcrypt/node_modules/node-pre-gyp/lib/pre-binding.js 20:22-48
W20180712-16:05:01.800(-7)? (STDERR)     Critical dependency: the request of a dependency is an expression
W20180712-16:05:01.800(-7)? (STDERR)      @ /home/ubuntu/meteor-webpack/examples/react/node_modules/bcrypt/node_modules/node-pre-gyp/lib/node-pre-gyp.js
W20180712-16:05:01.800(-7)? (STDERR)      @ /home/ubuntu/meteor-webpack/examples/react/node_modules/bcrypt/bcrypt.js
W20180712-16:05:01.800(-7)? (STDERR)      @ /home/ubuntu/meteor-webpack/examples/react/imports/api/tasks.js
W20180712-16:05:01.800(-7)? (STDERR)      @ /home/ubuntu/meteor-webpack/examples/react/client/ui/App.jsx
W20180712-16:05:01.800(-7)? (STDERR)      @ /home/ubuntu/meteor-webpack/examples/react/client/main.jsx
W20180712-16:05:01.800(-7)? (STDERR)      @ multi ./client/main.jsx webpack-hot-middleware/client?reload=true
W20180712-16:05:01.800(-7)? (STDERR)     
W20180712-16:05:01.800(-7)? (STDERR)     WARNING in /home/ubuntu/meteor-webpack/examples/react/node_modules/bcrypt/node_modules/node-pre-gyp/lib/util/versioning.js 17:20-67
W20180712-16:05:01.800(-7)? (STDERR)     Critical dependency: the request of a dependency is an expression
W20180712-16:05:01.800(-7)? (STDERR)      @ /home/ubuntu/meteor-webpack/examples/react/node_modules/bcrypt/node_modules/node-pre-gyp/lib/pre-binding.js
W20180712-16:05:01.801(-7)? (STDERR)      @ /home/ubuntu/meteor-webpack/examples/react/node_modules/bcrypt/node_modules/node-pre-gyp/lib/node-pre-gyp.js
W20180712-16:05:01.801(-7)? (STDERR)      @ /home/ubuntu/meteor-webpack/examples/react/node_modules/bcrypt/bcrypt.js
W20180712-16:05:01.801(-7)? (STDERR)      @ /home/ubuntu/meteor-webpack/examples/react/imports/api/tasks.js
W20180712-16:05:01.801(-7)? (STDERR)      @ /home/ubuntu/meteor-webpack/examples/react/client/ui/App.jsx
W20180712-16:05:01.801(-7)? (STDERR)      @ /home/ubuntu/meteor-webpack/examples/react/client/main.jsx
W20180712-16:05:01.801(-7)? (STDERR)      @ multi ./client/main.jsx webpack-hot-middleware/client?reload=true
W20180712-16:05:01.801(-7)? (STDERR)     
W20180712-16:05:01.801(-7)? (STDERR)     WARNING in /home/ubuntu/meteor-webpack/examples/react/node_modules/bcrypt/node_modules/node-pre-gyp/lib/util/compile.js
W20180712-16:05:01.801(-7)? (STDERR)     Module not found: Error: Can't resolve 'node-gyp' in '/home/ubuntu/meteor-webpack/examples/react/node_modules/bcrypt/node_modules/node-pre-gyp/lib/util'
W20180712-16:05:01.802(-7)? (STDERR)      @ /home/ubuntu/meteor-webpack/examples/react/node_modules/bcrypt/node_modules/node-pre-gyp/lib/util/compile.js
W20180712-16:05:01.802(-7)? (STDERR)      @ /home/ubuntu/meteor-webpack/examples/react/node_modules/bcrypt/node_modules/node-pre-gyp/lib sync ^\.\/.*$
W20180712-16:05:01.802(-7)? (STDERR)      @ /home/ubuntu/meteor-webpack/examples/react/node_modules/bcrypt/node_modules/node-pre-gyp/lib/node-pre-gyp.js
W20180712-16:05:01.802(-7)? (STDERR)      @ /home/ubuntu/meteor-webpack/examples/react/node_modules/bcrypt/bcrypt.js
W20180712-16:05:01.802(-7)? (STDERR)      @ /home/ubuntu/meteor-webpack/examples/react/imports/api/tasks.js
W20180712-16:05:01.802(-7)? (STDERR)      @ /home/ubuntu/meteor-webpack/examples/react/client/ui/App.jsx
W20180712-16:05:01.802(-7)? (STDERR)      @ /home/ubuntu/meteor-webpack/examples/react/client/main.jsx
W20180712-16:05:01.802(-7)? (STDERR)      @ multi ./client/main.jsx webpack-hot-middleware/client?reload=true
W20180712-16:05:01.803(-7)? (STDERR)     
W20180712-16:05:01.803(-7)? (STDERR)     WARNING in /home/ubuntu/meteor-webpack/examples/react/node_modules/bcrypt/node_modules/node-pre-gyp/lib/util/compile.js
W20180712-16:05:01.803(-7)? (STDERR)     Module not found: Error: Can't resolve 'npm' in '/home/ubuntu/meteor-webpack/examples/react/node_modules/bcrypt/node_modules/node-pre-gyp/lib/util'
W20180712-16:05:01.803(-7)? (STDERR)      @ /home/ubuntu/meteor-webpack/examples/react/node_modules/bcrypt/node_modules/node-pre-gyp/lib/util/compile.js
W20180712-16:05:01.803(-7)? (STDERR)      @ /home/ubuntu/meteor-webpack/examples/react/node_modules/bcrypt/node_modules/node-pre-gyp/lib sync ^\.\/.*$
W20180712-16:05:01.803(-7)? (STDERR)      @ /home/ubuntu/meteor-webpack/examples/react/node_modules/bcrypt/node_modules/node-pre-gyp/lib/node-pre-gyp.js
W20180712-16:05:01.803(-7)? (STDERR)      @ /home/ubuntu/meteor-webpack/examples/react/node_modules/bcrypt/bcrypt.js
W20180712-16:05:01.803(-7)? (STDERR)      @ /home/ubuntu/meteor-webpack/examples/react/imports/api/tasks.js
W20180712-16:05:01.804(-7)? (STDERR)      @ /home/ubuntu/meteor-webpack/examples/react/client/ui/App.jsx
W20180712-16:05:01.804(-7)? (STDERR)      @ /home/ubuntu/meteor-webpack/examples/react/client/main.jsx
W20180712-16:05:01.804(-7)? (STDERR)      @ multi ./client/main.jsx webpack-hot-middleware/client?reload=true
W20180712-16:05:01.804(-7)? (STDERR)     
W20180712-16:05:01.804(-7)? (STDERR)     WARNING in /home/ubuntu/meteor-webpack/examples/react/node_modules/bcrypt/node_modules/node-pre-gyp/lib/util/nw-pre-gyp/index.html 1:0
W20180712-16:05:01.804(-7)? (STDERR)     Module parse failed: Unexpected token (1:0)
W20180712-16:05:01.804(-7)? (STDERR)     You may need an appropriate loader to handle this file type.
W20180712-16:05:01.804(-7)? (STDERR)     > <!doctype html>
W20180712-16:05:01.805(-7)? (STDERR)     | <html>
W20180712-16:05:01.805(-7)? (STDERR)     | <head>
W20180712-16:05:01.805(-7)? (STDERR)      @ /home/ubuntu/meteor-webpack/examples/react/node_modules/bcrypt/node_modules/node-pre-gyp/lib sync ^\.\/.*$ ./util/nw-pre-gyp/index.html
W20180712-16:05:01.805(-7)? (STDERR)      @ /home/ubuntu/meteor-webpack/examples/react/node_modules/bcrypt/node_modules/node-pre-gyp/lib/node-pre-gyp.js
W20180712-16:05:01.805(-7)? (STDERR)      @ /home/ubuntu/meteor-webpack/examples/react/node_modules/bcrypt/bcrypt.js
W20180712-16:05:01.805(-7)? (STDERR)      @ /home/ubuntu/meteor-webpack/examples/react/imports/api/tasks.js
W20180712-16:05:01.805(-7)? (STDERR)      @ /home/ubuntu/meteor-webpack/examples/react/client/ui/App.jsx
W20180712-16:05:01.805(-7)? (STDERR)      @ /home/ubuntu/meteor-webpack/examples/react/client/main.jsx
W20180712-16:05:01.805(-7)? (STDERR)      @ multi ./client/main.jsx webpack-hot-middleware/client?reload=true
W20180712-16:05:01.805(-7)? (STDERR)     
W20180712-16:05:01.806(-7)? (STDERR)     ERROR in /home/ubuntu/meteor-webpack/examples/react/node_modules/bcrypt/node_modules/node-pre-gyp/lib/unpublish.js
W20180712-16:05:01.806(-7)? (STDERR)     Module not found: Error: Can't resolve 'aws-sdk' in '/home/ubuntu/meteor-webpack/examples/react/node_modules/bcrypt/node_modules/node-pre-gyp/lib'
W20180712-16:05:01.806(-7)? (STDERR)      @ /home/ubuntu/meteor-webpack/examples/react/node_modules/bcrypt/node_modules/node-pre-gyp/lib/unpublish.js 16:14-32
W20180712-16:05:01.806(-7)? (STDERR)      @ /home/ubuntu/meteor-webpack/examples/react/node_modules/bcrypt/node_modules/node-pre-gyp/lib sync ^\.\/.*$
W20180712-16:05:01.807(-7)? (STDERR)      @ /home/ubuntu/meteor-webpack/examples/react/node_modules/bcrypt/node_modules/node-pre-gyp/lib/node-pre-gyp.js
W20180712-16:05:01.807(-7)? (STDERR)      @ /home/ubuntu/meteor-webpack/examples/react/node_modules/bcrypt/bcrypt.js
W20180712-16:05:01.807(-7)? (STDERR)      @ /home/ubuntu/meteor-webpack/examples/react/imports/api/tasks.js
W20180712-16:05:01.807(-7)? (STDERR)      @ /home/ubuntu/meteor-webpack/examples/react/client/ui/App.jsx
W20180712-16:05:01.808(-7)? (STDERR)      @ /home/ubuntu/meteor-webpack/examples/react/client/main.jsx
W20180712-16:05:01.808(-7)? (STDERR)      @ multi ./client/main.jsx webpack-hot-middleware/client?reload=true
W20180712-16:05:01.808(-7)? (STDERR)     

... and so on ...

I've tried messing around with stuff like the webpack-node-externals package but then it seems like nothing in node_modules can be imported at runtime - meteor doesn't include any of that in the folder it builds into.

ardatan commented 6 years ago

I think ‘bcrypt’ is not compatible for browser, so Webpack tries to resolve it as a browser/es2015 module and cannot compile it for browser then. Try to seperate server code to a different file, and make sure this code is not added to client bundle. If you still have the issue, please reproduce a repo so I can help you better. Thanks for contribution!