12
I set up the package.json with the properties main and types:
{
    "name": "idb2promise",
    "version": "0.0.5",
    "description": "TypeScript library to manage IndexedDB Storage",
    "main": "./dist/index.js",
    "types": "./dist/index.d.ts",
    "files": [ "./dist" ],
    "scripts": { [...] },
    "keywords": [ [...] ],
    "author": "Guilherme Costamilam",
    "license": "MIT",
    "repository": {
        "type": "git",
        "url": "git+https://github.com/Costamilam/IDB2Promise.git"
    },
    "bugs": {
        "url": "https://github.com/Costamilam/IDB2Promise/issues"
    },
    "homepage": "https://costamilam.github.io/IDB2Promise/",  
    "dependencies": { },
    "devDependencies": { [...] }
}
I set up the tsconfig.json with the property declaration as true:
{
    "compileOnSave": false,
    "compilerOptions": {
        "module": "commonjs",
        "moduleResolution": "node",
        "removeComments": true,
        "sourceMap": true,
        "target": "es5",
        "declaration": true,
        "declarationDir": "./dist",
        "outDir": "./dist"
    },
    "lib": [ "es2015.promise"],
    "include": [ "src/**/*.ts" ],
    "exclude": [ "src/test.ts" ]
}
In the .npmignore I put the Typescript code, the test and configuration files
src
coverage
test
karma.conf.js
rollup.config.js
tsconfig.json
test.tsconfig.json
And in the /src/index.ts export public parts
export * from './object-store';
export * from './intefaces/request';
export * from './intefaces/request-event';
export * from './intefaces/async-cursor';
I build it with tsc -b and public with command npm publish
The structure of the folder dist:
|- dist
    |- <file>.js/.map.js/.d.ts
    |- util
        |- <file>.js/.map.js/.d.ts
    |- interfaces
        |- <file>.js/.map.js/.d.ts
The /dist/index.d.ts is equal to /src/index.ts above
The archive /dist/index.js generated:
"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("./object-store"));
//# sourceMappingURL=index.js.map
In another project, I install as dependency:
npm i idb2promise
When trying to import components
import { ObjectStore } from 'idb2promise';
I get the error
Cannot find module 'idb2promise'. ts(2307)
However, if I care using the relative path to the index, works as expected
import { ObjectStore } from './node_modules/idb2promise/dist/index';
How to import what is exported by index only by package name instead of relative path?
Take a look: https://cameronnokes.com/blog/the-30-second-guide-to-publishing-a-typescript-package-to-npm/
– Marconi
Is this your package? https://www.npmjs.com/package/idb2promise
– Marconi
@Marconi thanks for the link, I had already seen this, but I will relook. Yes that’s the same
– Costamilam
@Marconi reviewed how it was done on the link and the repository on Github, but apparently I do the same thing, I don’t know why it’s not working. I didn’t know the
npm link, will be of great help to test– Costamilam