camelCase
camelCase

Reputation: 485

Why is bcrypt showing errors on TravisCI?

I am running tests on TravisCI but at the point, npm test is triggered, this error is thrown:

     > [email protected] test /home/travis/build/danoseun/Store-Manager
        > npm run createTables && nyc --reporter=html --reporter=text mocha ./server/tests/*.js --exit --compilers js:babel-core/register
        > [email protected] createTables /home/travis/build/danoseun/Store-Manager
        > babel-node -- ./server/db/dbTables
        /home/travis/build/danoseun/Store-Manager/node_modules/bcrypt/bcrypt.js:92
                throw new Error('data and salt arguments required');
                ^
        **Error: data and salt arguments required**
            at Object.hashSync (/home/travis/build/danoseun/Store-Manager/node_modules/bcrypt/bcrypt.js:92:15)
            at Object.<anonymous> (/home/travis/build/danoseun/Store-Manager/server/db/dbTables/seedAdmin/insertAdmin.js:6:28)
            at Module._compile (internal/modules/cjs/loader.js:721:30)
            at loader (/home/travis/build/danoseun/Store-Manager/node_modules/babel-register/lib/node.js:144:5)
            at Object.require.extensions.(anonymous function) [as .js] (/home/travis/build/danoseun/Store-Manager/node_modules/babel-register/lib/node.js:154:7)
            at Module.load (internal/modules/cjs/loader.js:620:32)
            at tryModuleLoad (internal/modules/cjs/loader.js:560:12)
            at Function.Module._load (internal/modules/cjs/loader.js:552:3)
            at Module.require (internal/modules/cjs/loader.js:657:17)
            at require (internal/modules/cjs/helpers.js:22:18)
        npm ERR! code ELIFECYCLE
        npm ERR! errno 1
        npm ERR! [email protected] createTables: `babel-node -- ./server/db/dbTables`
        npm ERR! Exit status 1
        npm ERR! 
        npm ERR! Failed at the [email protected] createTables script.
        npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
        npm ERR! A complete log of this run can be found in:
        npm ERR!     /home/travis/.npm/_logs/2019-01-15T14_24_23_739Z-debug.log
        npm ERR! Test failed.  See above for more details.
        The command "npm test" exited with 1.
        cache.2
        store build cache
        0.00s2.35snothing changed, not updating cache
        Done. Your build exited with 1.

This is the way, the insertAdmin.js file looks like:

    import bcrypt from 'bcrypt';
    import pool from '../../connection';



const sql = 'insert into users (email, password, role) values ($1, $2, $3)';
    const password = process.env.PASSWORD;
    const newPassword = bcrypt.hashSync(password, 10);
    const email = process.env.EMAIL;
    const variables = [email, newPassword, 'admin'];

    I also tried to restructure the file to use asynchronous hashing but it still didn't work.

    const sql = 'insert into users (email, password, role) values ($1, $2, $3)';
    const password = process.env.PASSWORD;

    async function value() {
         console.log('HERE', bcrypt.hash(password, 10))
         const hashPassword = await bcrypt.hash(password, 10);
        console.log('OYA', hashPassword);
         return hashPassword;
         }
        const email = process.env.EMAIL;
        const variables = [email, value(), 'admin'];
        console.log('NOW', variables[1]);

console.log(bcrypt.hash(password, 10)) and console.log(hashPassword) return the correct values but console.log(variables[1]) returns an empty object.

I don't understand what could be wrong.

PS: I just did something. I unintentionally pushed the console.log(process.env.PASSWORD) in that file to github and integrated it with travis and behold, the line where I did console.log shows that process.env.PASSWORD is undefined. I have made further steps to import dotenv or dtenv/config but it's still undefined. What do I do?

Upvotes: 0

Views: 199

Answers (2)

camelCase
camelCase

Reputation: 485

To add to the upvoted answer, data and salt arguments required error in bcrypt is thrown when one of the arguments to bcrypt.hashSync method is null according to https://github.com/kelektiv/node.bcrypt.js/blob/master/bcrypt.js line 92. To solve this on Travis, click on more options-settings and environmental variables on TravisCI of the repository and add(with the add button) the respective key as was specified in your .env file. Build should automatically restart and see it pass if other things are equal.

Upvotes: 0

Itai Steinherz
Itai Steinherz

Reputation: 817

process.env.PASSWORD is probably undefined since you forgot to configure environment variables on Travis CI. Since passwords are usually confidential, you should set the variable in the settings page of your repo, which will encrypt the password and keep it secret. See more info on how to do that here.

Upvotes: 1

Related Questions