简体   繁体   English

将aws-sdk集成到angular2给出“无法找到模块'流'”

[英]integrating aws-sdk to angular2 gives “Cannot find module 'stream'”

I tried these 我试过这些

My typings and @types/node seems to have been installed properly. 我的打字和@ types / node似乎已正确安装。 Here are error details: 以下是错误详细信息:

ERROR in /Users/ishandutta2007/Documents/Projects/myproj/node_modules/aws-sdk/lib/http_response.d.ts (1 ,25): Cannot find module 'stream'.) /Users/ishandutta2007/Documents/Projects/myproj/node_modules/aws-sdk/lib/http_response.d.ts (14,18): Cannot find name 'Buffer'.) /Users/ishandutta2007/Documents/Projects/myproj/node_modules/aws-sdk/lib/http_response.d.ts(1,25)中的错误:找不到模块'stream'。)/ Users / ishandutta2007 / Documents / Projects / myproj /node_modules/aws-sdk/lib/http_response.d.ts(1418):找不到名称'Buffer'。)

tsconfig.json tsconfig.json

{
  "compileOnSave": false,
  "compilerOptions": {
    "outDir": "./dist/out-tsc",
    "baseUrl": "src",
    "sourceMap": true,
    "declaration": false,
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "target": "es5",
    "typeRoots": [
      "node_modules/@types"
    ],
    "lib": [
      "es2016",
      "dom"
    ]
  }
}

package.json 的package.json

{
  "name": "reactvidsangular",
  "version": "0.0.0",
  "license": "MIT",
  "scripts": {
    "ng": "ng",
    "start": "webpack-dev-server --port=4200",
    "build": "webpack",
    "test": "karma start ./karma.conf.js",
    "lint": "ng lint",
    "e2e": "protractor ./protractor.conf.js",
    "prepree2e": "npm start",
    "pree2e": "webdriver-manager update --standalone false --gecko false --quiet"
  },
  "private": true,
  "dependencies": {
    "@angular/common": "^2.4.0",
    "@angular/compiler": "^2.4.0",
    "@angular/core": "^2.4.0",
    "@angular/forms": "^2.4.0",
    "@angular/http": "^2.4.0",
    "@angular/platform-browser": "^2.4.0",
    "@angular/platform-browser-dynamic": "^2.4.0",
    "@angular/router": "^3.4.0",
    "@types/jasmine": "2.5.38",
    "@types/node": "^6.0.66",
    "angular2-social-login": "^2.1.0",
    "aws-sdk": "^2.32.0",
    "core-js": "^2.4.1",
    "jquery": "^3.2.1",
    "mystartup_commons": "bitbucket:ishan_dutta/mystartup_commons",
    "ng2-bootstrap": "^1.4.2",
    "rxjs": "^5.1.0",
    "zone.js": "^0.7.6"
  },
  "devDependencies": {
    "@angular/cli": "1.0.0-rc.4",
    "@angular/compiler-cli": "^2.4.0",
    "autoprefixer": "^6.5.3",
    "codelyzer": "~2.0.0",
    "css-loader": "^0.26.1",
    "cssnano": "^3.10.0",
    "exports-loader": "^0.6.3",
    "file-loader": "^0.10.0",
    "istanbul-instrumenter-loader": "^2.0.0",
    "jasmine-core": "~2.5.2",
    "jasmine-spec-reporter": "~3.2.0",
    "json-loader": "^0.5.4",
    "karma": "~1.4.1",
    "karma-chrome-launcher": "~2.0.0",
    "karma-cli": "~1.0.1",
    "karma-coverage-istanbul-reporter": "^0.2.0",
    "karma-jasmine": "~1.1.0",
    "karma-jasmine-html-reporter": "^0.2.2",
    "karma-sourcemap-loader": "^0.3.7",
    "less-loader": "^2.2.3",
    "postcss-loader": "^0.13.0",
    "postcss-url": "^5.1.2",
    "protractor": "~5.1.0",
    "raw-loader": "^0.5.1",
    "sass-loader": "^4.1.1",
    "script-loader": "^0.7.0",
    "source-map-loader": "^0.1.5",
    "style-loader": "^0.13.1",
    "stylus-loader": "^2.4.0",
    "ts-node": "~2.0.0",
    "tslint": "~4.5.0",
    "typescript": "~2.0.0",
    "url-loader": "^0.5.7",
    "webpack-dev-server": "~2.3.0"
  }
}

app.component.ts app.component.ts

declare module 'aws-sdk' {
   var foo:any;
   export = foo;
}

import * as AWS from 'aws-sdk';
.......
.......

@Component({selector: 'app-basic', templateUrl: './app.component.html', styleUrls: ['./app.component.css']})
export class AppComponent {
  countReactions(myprefix) {
    var s3 = new AWS.S3();
    var params = {
      Bucket: 'mybucket',
      Prefix: myprefix,
    };
    s3.listObjectsV2(params, function (err, data) {
      if (err) 
        console.log(err, err.stack); // an error occurred
      else 
        console.log(data); // successful response
      }
    );
  }
}

Adding "node" to types in tsconfig.app.json file that the angular-cli creates in the src directory as below: 将“node”添加到tsconfig.app.json文件中的类型,angular-cli在src目录中创建如下:

{
  "extends": "../tsconfig.json",
  "compilerOptions": {
    "outDir": "../out-tsc/app",
    "module": "es2015",
    "baseUrl": "",
    "types": ["node"]
  },
  "exclude": [
    "test.ts",
    "**/*.spec.ts"
  ]
}

Based on these TypeScript docs : 基于这些TypeScript文档

Specify "types": [] to disable automatic inclusion of @types packages 指定“类型”:[]禁用自动包含@types包

Since types is empty, it is excluding the node typings that you installed via npm install @types/node. 由于types为空,因此不包括通过npm install @ types / node安装的节点类型。

In src/tsconfig.app.json src/tsconfig.app.json

Replacing 更换

"types": []

with "types": ["node"] "types": ["node"]

will work in Angular 5. 将在Angular 5中工作。

I had similar issue while using aws-sdk-js. 使用aws-sdk-js时我遇到了类似的问题。

Reason for this issue is because of node types. 此问题的原因是节点类型。 This is what I did 这就是我做的

npm install --save @types/node
npm install --save @types/aws-sdk

Add this line under compilerOptions in tsconfig.json tsconfig.json中的 compilerOptions下添加此行

"typeRoots": ["node_modules/@types"],

I found a solution for me. 我找到了一个解决方案。

You need to install the typing definitions for aws-sdk : 您需要安装aws-sdk的输入定义:

npm install --save-dev @types/aws-sdk

I got some errors about invalid peer dependencies from npm during that, but it worked anyway. 在此过程中,我从npm得到了一些关于无效对等依赖关系的错误,但无论如何它都有效。

You might get another error when compiling your application though, stating that the import syntax used in aws-sdk is not compatible with ECMAScript2015. 编译应用程序时可能会遇到另一个错误,说明aws-sdk使用的导入语法与ECMAScript2015不兼容。 I had to change compilerOptions.module to "es5" instead of "es2015" in my tsconfig.app.json to get it to work finally. 我不得不在我的tsconfig.app.json compilerOptions.module更改为"es5"而不是"es2015"以使其最终工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM