Could not find a declaration file for module
Asked Answered
H

3

10

I am trying to use a nodejs package which I created, can someone point out what am I doing wrong.

here is how the package structure looks like:

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

{
  "_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": "[email protected]:ishan_dutta/my_commons.git",
          "sshUrl": "git+ssh://[email protected]/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": "[email protected]",
  "_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": "[email protected]:ishan_dutta/my_commons.git",
      "sshUrl": "git+ssh://[email protected]/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": "[email protected]/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:

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

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:

export * from "./helpers";

index.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:

import {Helpers} from 'my_commons';

The error:

[ts] Could not find a declaration file for module 'my_commons'. '/Users/ishandutta2007/Documents/Projects/my_proj/node_modules/my_commons/dist/src/index.js' implicitly has an 'any' type.

Hymettus answered 22/3, 2017 at 1:2 Comment(3)
can be merged, dublicate of #41293059Hymettus
Does this answer your question? Could not find a declaration file for module 'module-name'. '/path/to/module-name.js' implicitly has an 'any' typeSorrows
well not clearly as the one accepted here but this one does though https://mcmap.net/q/53158/-could-not-find-a-declaration-file-for-module-39-module-name-39-39-path-to-module-name-js-39-implicitly-has-an-39-any-39-typeHymettus
P
7

You did not indicate in your package.json that your my_commons package has typings in it:

// my_commons package.json
{
  "typings": "dist/src/index.d.ts"
}
Print answered 22/3, 2017 at 5:15 Comment(4)
.d.ts is not required,I also removed .js from main, {"main": "dist/src/index", "typings": "dist/src/index"}Hymettus
Yes, you can skip that. I usually add it for completeness.Print
One more thing, I would actually recommend keeping the extension. I have burnt on this when I try to consume a package with "main": "index" in systemjs.Print
Correction. It was a package without main. This is the issue I'm referring to: github.com/systemjs/systemjs/issues/1603Print
O
6

If you can't import regular .js files - try to check and set "allowJs" option to true in tsconfig.json.

Ommiad answered 28/10, 2021 at 16:0 Comment(0)
K
0

You Should try this

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

Krell answered 27/2, 2022 at 16:23 Comment(1)
This does not work for instances where the package does not have a @types/* npm package published.Stier

© 2022 - 2024 — McMap. All rights reserved.