I installed thrift library and his typings file :
npm install thrift --save
npm install @types/thrift --save
When I import something from thrift like this:
import { TFramedTransport, TBinaryProtocol } from 'thrift';
And I create a random function to use these imports :
deserialize(message: Buffer): void {
const tTransport = new TFramedTransport(message);
const tProtocol = new TBinaryProtocol(tTransport);
}
Then, I run into 'Can't resolve' errors about thrift and ws libraries. (child_process, constants, crypto, fs, 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'
These errors are strange to me. Does anyone has an idea ?
Here is the package.json if it helps:
{
"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"
}
}
These modules are only available in the context of Node.js and can not be imported in the browser. A solution is to tell npm to not import these modules in the browser by adding this in package.json :
"browser": {
"name of the module": false
}
So in my case, I added that:
"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
}
I don't know if it's good to do that but it worked.