gpt4 book ai didi

javascript - 如果 .ts 文件在文件夹中更深, typescript 无法找到要 bcrypt 的模块

转载 作者:搜寻专家 更新时间:2023-10-30 21:12:13 24 4
gpt4 key购买 nike

项目根文件夹(此处命名为 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.compareSync(myvalue,hash));
});
});

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

以下代码与上面几乎相同,但仅针对类内容和深入文件夹结构(如 systemname/T/cryptUtil.ts)中断。代码如下:

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.compareSync(passw, hash);
}
//-------------------------------------------------
public BCryptHashCompareAsynchronous(
passw: string,
hash: string,
callback: ((err: Error, isMatch: boolean) => void)
): void {
bcrypt.compare(passw, hash, callback);
}
//-------------------------------------------------
}

另一种不接收警告或错误的方法是将类移动到项目根文件夹(此处命名为 systemname)并在根文件夹上创建一个文件以进行如下测试:

带类的文件已移至根文件夹:

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.compareSync(passw, hash);
}
//-------------------------------------------------
lxBCryptHashCompareAsynchronous(
passw: string,
hash: string,
callback: ((err: Error, isMatch: boolean) => void)
): void {
bcrypt.compare(passw, hash, callback);
}
//-------------------------------------------------
}

创建用于测试类的文件已移至根文件夹:

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();

如果在项目根文件夹中的 .ts 文件中编写最基本的代码,则 @types/bcrypt 没有错误,但如果将 .ts 更深入一点并在类中组织然后我收到错误。

做的时候:

npm run build | grep "Can't resolve"

仅保留以下错误:

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! systemname@1.0.0 build: `webpack`
npm ERR! Exit status 2
npm ERR!
npm ERR! Failed at the systemname@1.0.0 build 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/myuser/.npm/_logs/2018-01-18T01_01_01_012Z-debug.log

在找到 之前 this 解决方案还有很多其他错误。

但不能将每个类都放在正确的文件夹中以包含我想要的位置。
有详情和现状here .

最佳答案

@agathver 在这里回答:https://github.com/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不能捆绑

关于javascript - 如果 .ts 文件在文件夹中更深, typescript 无法找到要 bcrypt 的模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48257475/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com