Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I have nodejs version 10.19.0, ubuntu 20.04.2, and use webstorm ide for javascript. I tried installing both bcrypt and sha256 and neither libraries worked.
For example, after installing bcrypt the first 2 lines in my javascript code is:

    const bcrypt = require('bcrypt');
    alert('hello');

The alert function only pops up when I comment out the first line. I have the same problem with sha256.

I have tried installing, uninstalling, and reinstalling bcrypt and bcryptjs (even the bcrypt version that was supposed to match my node version). Why can't I seem to get these libraries to install properly? Thanks.

PS. I tried 'npm install bcrypt'

    $ npm list -g
    (node:40710) ExperimentalWarning: The fs.promises API is 
    experimental
    /home/philip/.nvm/versions/node/v10.16.3/lib
    ├── [email protected]
    ├── [email protected]
    ├── [email protected]
    ├── [email protected]
    └── [email protected]
                ` const bcrypt = require('bcrypt');  alert('hello'); ` is this code present in your HTML file? because alert is used as window alert in HTML files and the way you have used bcrypt is syntax for backend like app.js
– Jatin Mehrotra
                Jun 7, 2021 at 7:40
                Not directly.   <script type="application/javascript" src="js/todo.js"></script> at the end of the body.  Should I try console.log instead?
– Philip Stephens
                Jun 7, 2021 at 7:43
                if you have used npm install bcrypt then you need to use bcrypt in backend, check this example loginradius.com/blog/async/… or refer their npm website npmjs.com/package/bcrypt
– Jatin Mehrotra
                Jun 7, 2021 at 7:47

First of all you should properly look into how to install bcrypt in your project, You can look into this link https://www.npmjs.com/package/bcrypt for detailed installation and you would also be able to get further help from it. But right now the problem is that you are not importing bcrypt properly in your js file. You should import it like this and it will work fine.

const bcrypt = require('bcryptjs')
                OP uses npm iinstall bcrypt and if you refer bcrypt npm package this is not the correct way.
– Jatin Mehrotra
                Jun 7, 2021 at 7:51
                I tried  'npm install -g browserify' but that didn't work either.  I found this error message after looking at chrome's console log:  Uncaught ReferenceError: require is not defined     at todo.js:1
– Philip Stephens
                Jun 7, 2021 at 8:10
                @PhilipStephens require is not meant for html files it sis meant for backend node js files. refer the example i posted in my comment.
– Jatin Mehrotra
                Jun 7, 2021 at 9:54
                @Jatin I tried  " import bcrypt from 'bcryptj; "   and  "  import bcrypt from 'bcryptjs'; "  but neither of them worked.  Is there a short snippet that illustrates how to use bcrypt in javascript because I want to use it on a web application and not the backend?
– Philip Stephens
                Jun 7, 2021 at 19:06
                I tested 'bcryptjs' here: runkit.com/embed/lsr3gv9wpxj8 .  It works except I can't get the compare and verify function to return values.  Global variables don't work, neither do the return values but if I console.log the middle of the hash functions it works.  The original comments told me to store the results in a database but I am not using a database.
– Philip Stephens
                Jun 8, 2021 at 3:17

https://browserify.org/ is the only solution I tried that worked. Thank you everyone for your help.

Edit: I no longer need to use browserify to use bcrypt in my javascript application!

Here are the changes I did to make it work:

In todoApp.html...

<link rel="stylesheet" type="text/css" href="css/todo.css"> <meta charset="UTF-8"> <title>Todo App</title> <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/bcrypt.js"></script> </head> <script src="scripts/todo.js"></script> </body>

In todo.js

let bcrypt = dcodeIO.bcrypt;
const saltRounds = 12;
                also try this library based on bcrypt but really easy to use: npmjs.com/package/bcrypt-inzi
– Inzamam Malik
                Jun 26, 2021 at 10:53
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.