我安装了thrift库和他的类型文件:
npm install thrift --save
npm install @types/thrift --save当我像这样从thrift导入一些东西时:
import { TFramedTransport, TBinaryProtocol } from 'thrift';我创建了一个随机函数来使用这些导入:
deserialize(message: Buffer): void {
const tTransport = new TFramedTransport(message);
const tProtocol = new TBinaryProtocol(tTransport);
}然后,我遇到了关于thrift和ws库的“无法解决”错误。(child_process,常量,加密,文件系统,http,https,net,path,tls,zlib)
WARNING in ./node_modules/ws/lib/BufferUtil.js
Module not found: Error: Can't resolve 'bufferutil' in 'D:\workspace\dev\thrift_deserialize\my-app\node_modules\ws\lib'
WARNING in ./node_modules/ws/lib/Validation.js
Module not found: Error: Can't resolve 'utf-8-validate' in 'D:\workspace\dev\thrift_deserialize\my-app\node_modules\ws\lib'
ERROR in ./node_modules/thrift/lib/nodejs/lib/thrift/connection.js
Module not found: Error: Can't resolve 'child_process' in 'D:\workspace\dev\thrift_deserialize\my-app\node_modules\thrift\lib\nodejs\lib\thrift'
ERROR in ./node_modules/thrift/lib/nodejs/lib/thrift/connection.js
Module not found: Error: Can't resolve 'constants' in 'D:\workspace\dev\thrift_deserialize\my-app\node_modules\thrift\lib\nodejs\lib\thrift'
ERROR in ./node_modules/thrift/lib/nodejs/lib/thrift/server.js
Module not found: Error: Can't resolve 'constants' in 'D:\workspace\dev\thrift_deserialize\my-app\node_modules\thrift\lib\nodejs\lib\thrift'
ERROR in ./node_modules/thrift/lib/nodejs/lib/thrift/web_server.js
Module not found: Error: Can't resolve 'crypto' in 'D:\workspace\dev\thrift_deserialize\my-app\node_modules\thrift\lib\nodejs\lib\thrift'
ERROR in ./node_modules/ws/lib/WebSocketServer.js
Module not found: Error: Can't resolve 'crypto' in 'D:\workspace\dev\thrift_deserialize\my-app\node_modules\ws\lib'
ERROR in ./node_modules/ws/lib/WebSocket.js
Module not found: Error: Can't resolve 'crypto' in 'D:\workspace\dev\thrift_deserialize\my-app\node_modules\ws\lib'
ERROR in ./node_modules/ws/lib/Sender.js
Module not found: Error: Can't resolve 'crypto' in 'D:\workspace\dev\thrift_deserialize\my-app\node_modules\ws\lib'
ERROR in ./node_modules/thrift/lib/nodejs/lib/thrift/web_server.js
Module not found: Error: Can't resolve 'fs' in 'D:\workspace\dev\thrift_deserialize\my-app\node_modules\thrift\lib\nodejs\lib\thrift'
ERROR in ./node_modules/thrift/lib/nodejs/lib/thrift/http_connection.js
Module not found: Error: Can't resolve 'http' in 'D:\workspace\dev\thrift_deserialize\my-app\node_modules\thrift\lib\nodejs\lib\thrift'
ERROR in ./node_modules/thrift/lib/nodejs/lib/thrift/web_server.js
Module not found: Error: Can't resolve 'http' in 'D:\workspace\dev\thrift_deserialize\my-app\node_modules\thrift\lib\nodejs\lib\thrift'
ERROR in ./node_modules/ws/lib/WebSocket.js
Module not found: Error: Can't resolve 'http' in 'D:\workspace\dev\thrift_deserialize\my-app\node_modules\ws\lib'
ERROR in ./node_modules/ws/lib/WebSocketServer.js
Module not found: Error: Can't resolve 'http' in 'D:\workspace\dev\thrift_deserialize\my-app\node_modules\ws\lib'
ERROR in ./node_modules/thrift/lib/nodejs/lib/thrift/web_server.js
Module not found: Error: Can't resolve 'https' in 'D:\workspace\dev\thrift_deserialize\my-app\node_modules\thrift\lib\nodejs\lib\thrift'
ERROR in ./node_modules/thrift/lib/nodejs/lib/thrift/http_connection.js
Module not found: Error: Can't resolve 'https' in 'D:\workspace\dev\thrift_deserialize\my-app\node_modules\thrift\lib\nodejs\lib\thrift'
ERROR in ./node_modules/ws/lib/WebSocket.js
Module not found: Error: Can't resolve 'https' in 'D:\workspace\dev\thrift_deserialize\my-app\node_modules\ws\lib'
ERROR in ./node_modules/thrift/lib/nodejs/lib/thrift/server.js
Module not found: Error: Can't resolve 'net' in 'D:\workspace\dev\thrift_deserialize\my-app\node_modules\thrift\lib\nodejs\lib\thrift'
ERROR in ./node_modules/thrift/lib/nodejs/lib/thrift/connection.js
Module not found: Error: Can't resolve 'net' in 'D:\workspace\dev\thrift_deserialize\my-app\node_modules\thrift\lib\nodejs\lib\thrift'
ERROR in ./node_modules/thrift/lib/nodejs/lib/thrift/web_server.js
Module not found: Error: Can't resolve 'path' in 'D:\workspace\dev\thrift_deserialize\my-app\node_modules\thrift\lib\nodejs\lib\thrift'
ERROR in ./node_modules/thrift/lib/nodejs/lib/thrift/server.js
Module not found: Error: Can't resolve 'tls' in 'D:\workspace\dev\thrift_deserialize\my-app\node_modules\thrift\lib\nodejs\lib\thrift'
ERROR in ./node_modules/thrift/lib/nodejs/lib/thrift/connection.js
Module not found: Error: Can't resolve 'tls' in 'D:\workspace\dev\thrift_deserialize\my-app\node_modules\thrift\lib\nodejs\lib\thrift'
ERROR in ./node_modules/ws/lib/PerMessageDeflate.js
Module not found: Error: Can't resolve 'zlib' in 'D:\workspace\dev\thrift_deserialize\my-app\node_modules\ws\lib'这些错误对我来说很奇怪。有谁有主意吗?
下面是package.json,如果它有用的话:
{
"name": "my-app",
"version": "0.0.0",
"scripts": {
"ng": "ng",
"start": "ng serve",
"build": "ng build",
"test": "ng test",
"lint": "ng lint",
"e2e": "ng e2e"
},
"private": true,
"dependencies": {
"@angular/animations": "~7.1.0",
"@angular/common": "~7.1.0",
"@angular/compiler": "~7.1.0",
"@angular/core": "~7.1.0",
"@angular/forms": "~7.1.0",
"@angular/platform-browser": "~7.1.0",
"@angular/platform-browser-dynamic": "~7.1.0",
"@angular/router": "~7.1.0",
"@stomp/stompjs": "^5.1.0-beta.1",
"@types/thrift": "^0.10.7",
"buffer": "^5.2.1",
"core-js": "^2.5.4",
"rxjs": "~6.3.3",
"thrift": "^0.11.0",
"thriftrw": "^3.11.3",
"tslib": "^1.9.0",
"zone.js": "~0.8.26"
},
"devDependencies": {
"@angular-devkit/build-angular": "^0.12.2",
"@angular/cli": "~7.1.3",
"@angular/compiler-cli": "~7.1.0",
"@angular/language-service": "~7.1.0",
"@types/jasmine": "~2.8.8",
"@types/jasminewd2": "~2.0.3",
"@types/node": "^8.10.39",
"codelyzer": "~4.5.0",
"jasmine-core": "~2.99.1",
"jasmine-spec-reporter": "~4.2.1",
"karma": "~3.1.1",
"karma-chrome-launcher": "~2.2.0",
"karma-coverage-istanbul-reporter": "~2.0.1",
"karma-jasmine": "~1.1.2",
"karma-jasmine-html-reporter": "^0.2.2",
"protractor": "~5.4.0",
"ts-node": "~7.0.0",
"tslint": "~5.11.0",
"typescript": "^3.1.6"
}
}发布于 2019-01-21 19:07:27
这些模块仅在Node.js环境中可用,不能在浏览器中导入。一种解决方案是告诉npm不要在浏览器中导入这些模块,方法是在package.json中添加以下内容:
"browser": {
"name of the module": false
}因此,在我的例子中,我添加了:
"browser": {
"child_process": false,
"crypto": false,
"constants": false,
"fs": false,
"http": false,
"https": false,
"net": false,
"tls": false,
"zlib": false,
"path": false,
"bufferutil": false,
"utf-8-validate": false
}我不知道这样做好不好,但它起作用了。
https://stackoverflow.com/questions/54257084
复制相似问题