I am currently in the process of learning how to publish a typescript package on NPM. The package I am working on is quite simple at this point, as it mainly just exports some random number functions. However, I have plans for it to do much more in the future:
src
/random
/index.ts
/index.ts
package.json
tsconfig.json
.gitignore
When attempting to import the npm package into another typescript project, an error arises:
import {Random} from "@whiterook6/my-package";
console.log(Random.randomInt(1, 10));
Cannot find module '@whiterook6/my-package' or its corresponding type declarations.ts(2307)
However, adjusting the import statement like so resolves the issue:
import {Random} from "@whiterook6/my-package/src";
console.log(Random.randomInt(1, 10));
I am puzzled as to what I might have done incorrectly within my library package to lead to this undesirable outcome, and I am seeking guidance on how to modify it so that users can utilize these types of imports:
import {Random, Logger} from "@whiterook6/my-package";
Below is the content of my tsconfig.json file:
{
"compilerOptions": {
"target": "ESNext", // Specify the ECMAScript target version
"module": "ESNext", // Specify the module code generation
"moduleResolution": "node", // Specify module resolution strategy
"declaration": true, // Generate declaration files
"emitDeclarationOnly": true, // Only emit declaration files
"outDir": "./src", // Redirect output structure to the directory
"rootDir": "./src", // Specify the root directory of input files
"strict": true, // Enable strict type checking options
"esModuleInterop": true, // Enable compatibility with CommonJS and ES modules
"skipLibCheck": true, // Skip type checking of declaration files
"forceConsistentCasingInFileNames": true // Ensure consistent casing in imports
},
"include": [
"src/**/*" // Include all files in the src directory
],
"exclude": [
"node_modules", // Exclude node_modules directory
"dist" // Exclude dist directory
]
}