【问题标题】:Can't import external NPM package to make angular library无法导入外部 NPM 包来制作角度库
【发布时间】:2021-12-15 04:11:40
【问题描述】:
  • package.json首先我是这样做的,
{
  "name": "crypto-local-storage",
  "version": "0.0.1",
  "peerDependencies": {
    "@angular/common": "^12.2.0",
    "@angular/core": "^12.2.0",
    "crypto-js": "^3.1.8",
    "secure-web-storage": "^1.0.2"
  },
  "dependencies": {
    "tslib": "^2.3.0"
  },
  "devDependencies": {
    "@types/crypto-js": "^4.0.2"
  }
}

其次,我这样做了


{
  "name": "crypto-local-storage",
  "version": "0.0.1",
  "peerDependencies": {
    "@angular/common": "^12.2.0",
    "@angular/core": "^12.2.0"
    
  },
  "dependencies": {
    "tslib": "^2.3.0",
    "crypto-js": "^3.1.8",
    "secure-web-storage": "^1.0.2"
  },
  "devDependencies": {
    "@types/crypto-js": "^4.0.2"
  }
}

但是在我导入secure-web-storage时在我的服务文件中像这样

import * as SecureStorage from 'secure-web-storage'; 我收到了这样一条消息,

TS7016: Could not find a declaration file for module 'secure-web-storage'.
 'D:/AMS/crypto-local-storage/projects/crypto-local-storage/node_modules/secure-web-storage/secure-storage.js' 
implicitly has an 'any' type.  
Try `npm i --save-dev @types/secure-web-storage` if it exists or add a new declaration (.d.ts) file containing 
`declare module 'secure-web-storage';`

secure-web-storage 不包含任何@types/secure-web-storage 文件。那么我怎样才能摆脱这个错误呢?谷歌搜索了这么多,我没有得到任何明确的答案。

我的文件夹结构可能会对你有所帮助,

My Folder Structure Image

【问题讨论】:

  • 您是否尝试过按照 TypeScript 编译器的建议进行操作?
  • @tromgy 是的,但直到现在出现错误。

标签: javascript angular npm typeerror


【解决方案1】:

如果运行npm install -D @types/secure-web-storage 没有解决问题,请使用require 而不是import

const secureWebStorage= require('secure-web-storage');

【讨论】:

    猜你喜欢
    • 2021-02-08
    • 1970-01-01
    • 1970-01-01
    • 2021-09-18
    • 1970-01-01
    • 2019-09-16
    • 1970-01-01
    • 1970-01-01
    • 2016-11-27
    相关资源
    最近更新 更多