简体   繁体   English

找不到模块的声明文件

[英]Could not find a declaration file for module

I am trying to use a nodejs package which I created, can someone point out what am I doing wrong.我正在尝试使用我创建的 nodejs package,有人可以指出我做错了什么。

here is how the package structure looks like:这是 package 结构的样子:

node_modules
 |__my_commons
 |   |__dist
 |       |__src
 |       |    |__helpers.d.ts
 |       |    |__helpers.js
 |       |    |__index.d.ts
 |       |    |__index.js
 |       |__node_modules   <=This got recursively installed
 |       |__.npmignore
 |       |__package.json
src
 |_app/app.component.ts <= Here I am using it

my_commons/package.json my_commons/package.json

{
  "_args": [
    [
      {
        "raw": "my_commons@bitbucket:ishan_dutta/my_commons",
        "scope": null,
        "escapedName": "my_commons",
        "name": "my_commons",
        "rawSpec": "bitbucket:ishan_dutta/my_commons",
        "spec": "bitbucket:ishan_dutta/my_commons",
        "type": "hosted",
        "hosted": {
          "type": "bitbucket",
          "ssh": "git@bitbucket.org:ishan_dutta/my_commons.git",
          "sshUrl": "git+ssh://git@bitbucket.org/ishan_dutta/my_commons.git",
          "httpsUrl": "git+https://bitbucket.org/ishan_dutta/my_commons.git",
          "shortcut": "bitbucket:ishan_dutta/my_commons",
          "directUrl": "https://bitbucket.org/ishan_dutta/my_commons/raw/master/package.json"
        }
      },
      "/Users/ishandutta2007/Documents/Projects/my_proj"
    ]
  ],
  "_from": "bitbucket:ishan_dutta/my_commons",
  "_id": "my_commons@1.0.0",
  "_inCache": true,
  "_location": "/my_commons",
  "_phantomChildren": {},
  "_requested": {
    "raw": "my_commons@bitbucket:ishan_dutta/my_commons",
    "scope": null,
    "escapedName": "my_commons",
    "name": "my_commons",
    "rawSpec": "bitbucket:ishan_dutta/my_commons",
    "spec": "bitbucket:ishan_dutta/my_commons",
    "type": "hosted",
    "hosted": {
      "type": "bitbucket",
      "ssh": "git@bitbucket.org:ishan_dutta/my_commons.git",
      "sshUrl": "git+ssh://git@bitbucket.org/ishan_dutta/my_commons.git",
      "httpsUrl": "git+https://bitbucket.org/ishan_dutta/my_commons.git",
      "shortcut": "bitbucket:ishan_dutta/my_commons",
      "directUrl": "https://bitbucket.org/ishan_dutta/my_commons/raw/master/package.json"
    }
  },
  "_requiredBy": [
    "#DEV:/"
  ],
  "_resolved": "git+https://bitbucket.org/ishan_dutta/my_commons.git#6fef24d35f8b2b1f6f3d362cccaf92ca4fa62a93",
  "_shasum": "ad2546c6aadd832baf97d5334b60cc355b4e6647",
  "_shrinkwrap": null,
  "_spec": "my_commons@bitbucket.org/ishan_dutta/my_commons",
  "_where": "/Users/ishandutta2007/Documents/Projects/my_proj",
  "author": "",
  "dependencies": {
    "@types/node": "^7.0.8",
    "aws-sdk": "2.28.0",
    "firebase": "^3.6.7",
    "gulp": "^3.9.1",
    "gulp-typescript": "^3.1.4",
    "requirejs": "^2.3.3",
    "typescript": "^2.0.10"
  },
  "description": "all common code to be shared across multiple projects",
  "devDependencies": {},
  "engines": {
    "node": "7.6.0"
  },
  "gitHead": "6fef24d35f8b2b1f6f3d362cccaf92ca4fa62a93",
  "license": "ISC",
  "main": "dist/src/index.js",
  "name": "mystartup_commons",
  "optionalDependencies": {},
  "readme": "ERROR: No README data found!",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "version": "1.0.0"
}

helpers.d.ts: helpers.d.ts:

export declare namespace Helpers {
    const tags: string[];
    function roundMinutes(date: any): any;
    function convertDateToUTC(date: any): Date;
}

helpers.js: helpers.js:

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var http = require('http');
var https = require('https');
var fs = require('fs');
var AWS = require('aws-sdk');
var myBucket = 'videoreactions';
var Helpers;
(function (Helpers) {
    Helpers.tags = [
        'national',
        'business',
        'sports',
        'world',
        'politics',
        'technology',
        'startup',
        'entertainment',
        'miscellaneous',
        'science',
        'automobile'
    ];
    function roundMinutes(date) {
        date.setHours(date.getHours() + Math.round(date.getMinutes() / 60));
        date.setMinutes(0);
        return date;
    }
    Helpers.roundMinutes = roundMinutes;
    function convertDateToUTC(date) {
        return new Date(date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate(), date.getUTCHours(), date.getUTCMinutes(), date.getUTCSeconds());
    }
    Helpers.convertDateToUTC = convertDateToUTC;
})(Helpers = exports.Helpers || (exports.Helpers = {}));

index.d.ts:索引.d.ts:

export * from "./helpers";

index.js:索引.js:

"use strict";
function __export(m) {
    for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
}
Object.defineProperty(exports, "__esModule", { value: true });
__export(require("./helpers"));

app.component.ts:应用程序组件.ts:

import {Helpers} from 'my_commons';

The error:错误:

[ts] Could not find a declaration file for module 'my_commons'. [ts] 找不到模块“my_commons”的声明文件。 '/Users/ishandutta2007/Documents/Projects/my_proj/node_modules/my_commons/dist/src/index.js' implicitly has an 'any' type. '/Users/ishandutta2007/Documents/Projects/my_proj/node_modules/my_commons/dist/src/index.js' 隐式具有 'any' 类型。

You did not indicate in your package.json that your my_commons package has typings in it: 你没有在你的package.json中指出你的my_commons包里面有my_commons

// my_commons package.json
{
  "typings": "dist/src/index.d.ts"
}

如果您不能只导入简单的 .js 文件 - 尝试检查并在tsconfig.json中将“ allowJs ”选项设置为true

You Should try this你应该试试这个

npm i --save-dev @types/<module_name>

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

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