导入旧版存储库时出现角度“无法解决”错误

时间:2019-01-18 15:35:56

标签: angular thrift

我安装了节俭库和他的打字文件:

npm install thrift --save

npm install @types/thrift --save

当我从节俭中导入某些东西时,

import { TFramedTransport, TBinaryProtocol } from 'thrift';

然后我创建一个随机函数来使用这些导入:

deserialize(message: Buffer): void {
  const tTransport = new TFramedTransport(message);
  const tProtocol = new TBinaryProtocol(tTransport);
}

然后,我遇到了有关节俭和ws库的“无法解决”错误。 (child_process,常量,加密,fs,http,https,net,路径,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"
  }
}

1 个答案:

答案 0 :(得分:0)

这些模块仅在Node.js上下文中可用,不能在浏览器中导入。 一种解决方案是通过在package.json中添加npm使其不将这些模块导入浏览器:

"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
  }

我不知道这样做是不是很好,但是确实有效。