最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - typescript cant find modules to bcrypt if .ts file deeper on folder - Stack Overflow

programmeradmin1浏览0评论

The simple working code on project root folder (named here as systemname):

import * as bcrypt from 'bcrypt'; 

let myvalue = "JFDSNJDSNFJSDNFSJASASNDCAUDHANDKLAMDXIALWDMQAW";

bcrypt.genSalt(10, function(err, salt) {
    bcrypt.hash(myvalue, salt, function(err, hash) {
        console.log(myvalue);
        console.log(hash);
        console.log(bcryptpareSync(myvalue,hash));
    });
});

let salt = bcrypt.genSaltSync(10);
let hash = bcrypt.hashSync(myvalue,salt);
console.log(myvalue);
console.log(hash);
console.log(bcryptpareSync(myvalue,hash));

The following code is almost the same as above but breaks only for the class stuff and by being deep inside folder structure like systemname/T/cryptUtil.ts . The code is the following:

import * as bcrypt from "bcrypt";

export class cryptUtil {
  //-------------------------------------------------
  private _rounds: number = 10;  //0xf49f6cd693d04c7ebe6928429a24f2ce;
  public get rounds(): number {
    return this._rounds;
  }
  public set rounds(rounds: number) {
    this._rounds = rounds;
  }
  //-------------------------------------------------
  public getSalt(): string {
    return bcrypt.genSaltSync(this.rounds);
  }
  //-------------------------------------------------
  public getSaltAsynchronous(
    callback: ((err: Error, salt: string) => void)
  ): void {
    bcrypt.genSalt(this.rounds, callback);
  }
  //-------------------------------------------------
  public BCryptHashGet(passw: string): string {
    return bcrypt.hashSync(passw, this.getSalt());
  }
  //-------------------------------------------------
  public BCryptHashGetAsynchronous(
    passw: string,
    callback: ((err: Error, hash: string) => void)
  ): void {
    this.getSaltAsynchronous((err: Error, salt: string) => {
      bcrypt.hash(passw, salt, callback);
    });
  }
  //-------------------------------------------------
  public BCryptHashCompare(passw: string, hash: string): boolean {
    return bcryptpareSync(passw, hash);
  }
  //-------------------------------------------------
  public BCryptHashCompareAsynchronous(
    passw: string,
    hash: string,
    callback: ((err: Error, isMatch: boolean) => void)
  ): void {
    bcryptpare(passw, hash, callback);
  }
  //-------------------------------------------------
}

Another way to not receive warnings or errors is move the class to project root folder (named here as systemname) and create a file also on the root folder to test as follow:

The file with class moved to root folder:

import * as bcrypt from "bcrypt";

export class lxCryptUtil {
  //-------------------------------------------------
  lxGetSalt(rounds: number): string {
    return bcrypt.genSaltSync(rounds);
  }
  //-------------------------------------------------
  lxGetSaltAsynchronous(
    rounds: number,
    callback: ((err: Error, salt: string) => void)
  ): void {
    bcrypt.genSalt(rounds, callback);
  }
  //-------------------------------------------------
  lxBCryptHashGet(rounds: number, passw: string): string {
    return bcrypt.hashSync(passw, this.lxGetSalt(rounds));
  }
  //-------------------------------------------------
  lxBCryptHashGetAsynchronous(
    rounds: number,
    passw: string,
    callback: ((err: Error, hash: string) => void)
  ): void {
      this.lxGetSaltAsynchronous(rounds, (err: Error, salt: string) => {
      bcrypt.hash(passw, salt, callback);
    });
  }
  //-------------------------------------------------
  lxBCryptHashCompare(passw: string, hash: string): boolean {
    return bcryptpareSync(passw, hash);
  }
  //-------------------------------------------------
  lxBCryptHashCompareAsynchronous(
    passw: string,
    hash: string,
    callback: ((err: Error, isMatch: boolean) => void)
  ): void {
    bcryptpare(passw, hash, callback);
  }
  //-------------------------------------------------
}

The file creatad to test the class moved to root folder:

import { lxCryptUtil } from './lxCryptUtil';

export class dummy{
    processPwd(): void {
        let hash: string = "";
        let passwd: string = "FJIEJWIOEFNWONMWLEW";
        let lx = new lxCryptUtil();
        hash = lx.lxBCryptHashGet(10,passwd);
        console.log(passwd);
        console.log(hash);
        console.log(lx.lxBCryptHashCompare(passwd, hash));
    }
}

let d = new dummy();
d.processPwd();

If coding the most basic possible inside a .ts file in project root folder there is no error with @types/bcrypt but if put .ts a bit deeper and organize inside a class then i receive errors.

When doing:

npm run build | grep "Can't resolve"

remained only the following errors:

Module not found: Error: Can't resolve 'npm' in '/systemname/node_modules/node-pre-gyp/lib/util'
Module not found: Error: Can't resolve '../package' in '/home/myuser/node_modules/node-gyp/lib'
Module not found: Error: Can't resolve 'aws-sdk' in '/systemname/node_modules/node-pre-gyp/lib'
Module not found: Error: Can't resolve 'aws-sdk' in '/systemname/node_modules/node-pre-gyp/lib'
Module not found: Error: Can't resolve 'aws-sdk' in '/systemname/node_modules/node-pre-gyp/lib'
npm ERR! code ELIFECYCLE
npm ERR! errno 2
npm ERR! [email protected] build: `webpack`
npm ERR! Exit status 2
npm ERR!
npm ERR! Failed at the [email protected] build script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A plete log of this run can be found in:
npm ERR!     /home/myuser/.npm/_logs/2018-01-18T01_01_01_012Z-debug.log

Before finding this solution there was many other errors.

But cant keep each class in right folder to include where i want.
There is detail and current status here.

The simple working code on project root folder (named here as systemname):

import * as bcrypt from 'bcrypt'; 

let myvalue = "JFDSNJDSNFJSDNFSJASASNDCAUDHANDKLAMDXIALWDMQAW";

bcrypt.genSalt(10, function(err, salt) {
    bcrypt.hash(myvalue, salt, function(err, hash) {
        console.log(myvalue);
        console.log(hash);
        console.log(bcrypt.pareSync(myvalue,hash));
    });
});

let salt = bcrypt.genSaltSync(10);
let hash = bcrypt.hashSync(myvalue,salt);
console.log(myvalue);
console.log(hash);
console.log(bcrypt.pareSync(myvalue,hash));

The following code is almost the same as above but breaks only for the class stuff and by being deep inside folder structure like systemname/T/cryptUtil.ts . The code is the following:

import * as bcrypt from "bcrypt";

export class cryptUtil {
  //-------------------------------------------------
  private _rounds: number = 10;  //0xf49f6cd693d04c7ebe6928429a24f2ce;
  public get rounds(): number {
    return this._rounds;
  }
  public set rounds(rounds: number) {
    this._rounds = rounds;
  }
  //-------------------------------------------------
  public getSalt(): string {
    return bcrypt.genSaltSync(this.rounds);
  }
  //-------------------------------------------------
  public getSaltAsynchronous(
    callback: ((err: Error, salt: string) => void)
  ): void {
    bcrypt.genSalt(this.rounds, callback);
  }
  //-------------------------------------------------
  public BCryptHashGet(passw: string): string {
    return bcrypt.hashSync(passw, this.getSalt());
  }
  //-------------------------------------------------
  public BCryptHashGetAsynchronous(
    passw: string,
    callback: ((err: Error, hash: string) => void)
  ): void {
    this.getSaltAsynchronous((err: Error, salt: string) => {
      bcrypt.hash(passw, salt, callback);
    });
  }
  //-------------------------------------------------
  public BCryptHashCompare(passw: string, hash: string): boolean {
    return bcrypt.pareSync(passw, hash);
  }
  //-------------------------------------------------
  public BCryptHashCompareAsynchronous(
    passw: string,
    hash: string,
    callback: ((err: Error, isMatch: boolean) => void)
  ): void {
    bcrypt.pare(passw, hash, callback);
  }
  //-------------------------------------------------
}

Another way to not receive warnings or errors is move the class to project root folder (named here as systemname) and create a file also on the root folder to test as follow:

The file with class moved to root folder:

import * as bcrypt from "bcrypt";

export class lxCryptUtil {
  //-------------------------------------------------
  lxGetSalt(rounds: number): string {
    return bcrypt.genSaltSync(rounds);
  }
  //-------------------------------------------------
  lxGetSaltAsynchronous(
    rounds: number,
    callback: ((err: Error, salt: string) => void)
  ): void {
    bcrypt.genSalt(rounds, callback);
  }
  //-------------------------------------------------
  lxBCryptHashGet(rounds: number, passw: string): string {
    return bcrypt.hashSync(passw, this.lxGetSalt(rounds));
  }
  //-------------------------------------------------
  lxBCryptHashGetAsynchronous(
    rounds: number,
    passw: string,
    callback: ((err: Error, hash: string) => void)
  ): void {
      this.lxGetSaltAsynchronous(rounds, (err: Error, salt: string) => {
      bcrypt.hash(passw, salt, callback);
    });
  }
  //-------------------------------------------------
  lxBCryptHashCompare(passw: string, hash: string): boolean {
    return bcrypt.pareSync(passw, hash);
  }
  //-------------------------------------------------
  lxBCryptHashCompareAsynchronous(
    passw: string,
    hash: string,
    callback: ((err: Error, isMatch: boolean) => void)
  ): void {
    bcrypt.pare(passw, hash, callback);
  }
  //-------------------------------------------------
}

The file creatad to test the class moved to root folder:

import { lxCryptUtil } from './lxCryptUtil';

export class dummy{
    processPwd(): void {
        let hash: string = "";
        let passwd: string = "FJIEJWIOEFNWONMWLEW";
        let lx = new lxCryptUtil();
        hash = lx.lxBCryptHashGet(10,passwd);
        console.log(passwd);
        console.log(hash);
        console.log(lx.lxBCryptHashCompare(passwd, hash));
    }
}

let d = new dummy();
d.processPwd();

If coding the most basic possible inside a .ts file in project root folder there is no error with @types/bcrypt but if put .ts a bit deeper and organize inside a class then i receive errors.

When doing:

npm run build | grep "Can't resolve"

remained only the following errors:

Module not found: Error: Can't resolve 'npm' in '/systemname/node_modules/node-pre-gyp/lib/util'
Module not found: Error: Can't resolve '../package' in '/home/myuser/node_modules/node-gyp/lib'
Module not found: Error: Can't resolve 'aws-sdk' in '/systemname/node_modules/node-pre-gyp/lib'
Module not found: Error: Can't resolve 'aws-sdk' in '/systemname/node_modules/node-pre-gyp/lib'
Module not found: Error: Can't resolve 'aws-sdk' in '/systemname/node_modules/node-pre-gyp/lib'
npm ERR! code ELIFECYCLE
npm ERR! errno 2
npm ERR! [email protected] build: `webpack`
npm ERR! Exit status 2
npm ERR!
npm ERR! Failed at the [email protected] build script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A plete log of this run can be found in:
npm ERR!     /home/myuser/.npm/_logs/2018-01-18T01_01_01_012Z-debug.log

Before finding this solution there was many other errors.

But cant keep each class in right folder to include where i want.
There is detail and current status here.

Share Improve this question edited Jan 25, 2018 at 1:10 Mark asked Jan 15, 2018 at 5:40 MarkMark 2571 gold badge4 silver badges11 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 2

As answered by @agathver here : https://github./kelektiv/node.bcrypt.js

NodeJS native modules contains DLLs and glue code and the module initialization code loads them into the NodeJS process. For this we need the full path to the native module. We use a library called bindings to find the correct path to the native DLL.

Webpack while bundling changes those expectations (namely, the initialization code and package.json are located in the directory)

bcrypt cant be bundled

发布评论

评论列表(0)

  1. 暂无评论