当尝试使用Jimp执行下面的代码时,我一直收到此错误。我仔细阅读了这些问题,发现.28版中的缓冲区存在问题。我降级到0.27,这无济于事。我想知道这是否仍然是问题,还是我错误地导入了文件。
ERROR in node_modules/jimp/jimp.d.ts(18,24): error TS2304: Cannot find name 'Buffer'.
我认为.d.ts文件可能是错误的,因为我没有在代码中使用缓冲区。
我已经将Jimp的类型导入到我的开发依赖项中(npm i --save-dev @ types / node),但是新版的Jimp表示它不需要这些类型,它有自己的类型。我还将“类型”:[“节点”]放入了tsconfig.json文件中。
代码
import { Component, OnInit } from '@angular/core';
import * as jimp from 'jimp';
@Component({
selector: 'app-jimp-test',
templateUrl: './jimp-test.component.html',
styleUrls: ['./jimp-test.component.css']
})
export class JimpTestComponent implements OnInit {
constructor() { }
ngOnInit() {
this.testJimp();
}
testJimp() {
jimp.read('assets/testInput.jpg').then( (img) => {
img.clone().blur(1).write('assets/testOutput.jpg');
});
}
}
依赖项
{
"name": "my-playground",
"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": "^6.0.3",
"@angular/common": "^6.0.3",
"@angular/compiler": "^6.0.3",
"@angular/core": "^6.0.3",
"@angular/forms": "^6.0.3",
"@angular/http": "^6.0.3",
"@angular/platform-browser": "^6.0.3",
"@angular/platform-browser-dynamic": "^6.0.3",
"@angular/router": "^6.0.3",
"core-js": "^2.5.4",
"jimp": "^0.2.28",
"rxjs": "^6.0.0",
"zone.js": "^0.8.26"
},
"devDependencies": {
"@angular-devkit/build-angular": "~0.6.8",
"@angular/cli": "~6.0.8",
"@angular/compiler-cli": "^6.0.3",
"@angular/language-service": "^6.0.3",
"@types/jasmine": "~2.8.6",
"@types/jasminewd2": "~2.0.3",
"@types/jimp": "^0.2.28",
"@types/node": "^8.9.5",
"codelyzer": "~4.2.1",
"jasmine-core": "~2.99.1",
"jasmine-spec-reporter": "~4.2.1",
"karma": "~1.7.1",
"karma-chrome-launcher": "~2.2.0",
"karma-coverage-istanbul-reporter": "~2.0.0",
"karma-jasmine": "~1.1.1",
"karma-jasmine-html-reporter": "^0.2.2",
"protractor": "~5.3.0",
"ts-node": "~5.0.1",
"tslint": "~5.9.1",
"typescript": "~2.7.2"
}
}
角度版本
@angular-devkit/architect 0.6.8
@angular-devkit/build-angular 0.6.8
@angular-devkit/build-optimizer 0.6.8
@angular-devkit/core 0.6.8
@angular-devkit/schematics 0.6.8
@angular/cli 6.0.8
@ngtools/webpack 6.0.8
@schematics/angular 0.6.8
@schematics/update 0.6.8
rxjs 6.2.1
typescript 2.7.2
webpack 4.8.3
Jimp版本
0.2.28
tsconfig.json
{
"compileOnSave": false,
"compilerOptions": {
"baseUrl": "./",
"outDir": "./dist/out-tsc",
"sourceMap": true,
"declaration": false,
"moduleResolution": "node",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"target": "es5",
"typeRoots": [
"node_modules/@types"
],
"lib": [
"es2017",
"dom"
]
}
}
tsconfig.app.json
{
"extends": "../tsconfig.json",
"compilerOptions": {
"outDir": "../out-tsc/app",
"module": "es2015",
"types": []
},
"exclude": [
"src/test.ts",
"**/*.spec.ts"
]
}
答案 0 :(得分:1)
您可以尝试以下方法:
此行应位于顶部:
declare const Buffer
它应该编译没有错误。
使用新版本的Typescript,您还可以使用官方声明文件:
npm i -g typescript@next
npm i --save-dev @types/node
并在tsconfig.app.json中添加"types" : ["node"]
答案 1 :(得分:1)
不知道是否可以帮助任何人,但我通过添加:
import {Utils} from 'tslint'
进入我的组件。疯狂的工作