2

I've published a TypeScript library onto NPM. On GitHub the dist (Link to Repository Folder) directory contains all the compiled JavaScript and d.ts files. However when running npm i <my_package> the result is a module with a dist folder only containing an index.js, the src folder is still present with the TypeScript files. The dist/src folder along with it's JavaScript and d.ts files are not found on the installed package consequently my module is not recognized or typed in Vanilla JavaScript projects.

Library tsconfig.json:

{
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "declaration": true,
    "outDir": "./dist",
    "strict": true,
  },
  "exclude": [
    "node_modules/",
    "dist/"
  ]
}

Library package.json

{
  "name": "easy-trivia",
  "version": "2.0.3",
  "description": "A wrapper for the Open Trivia Database API. Built with TypeScript, works with VanillaJS.",
  "keywords": [
    "trivia",
    "games",
    "fun",
    "api",
    "easy",
    "typescript",
    "small",
    "quiz",
    "opentriviadatabase",
    "opentdb",
    "opentriviadb"
  ],
  "main": "dist/index.js",
  "types": "dist/index.d.ts",
  "scripts": {
    "build": "tsc",
    "prettier": "npm run build && prettier -w src/",
    "prepublish": "npm run prep",
    "exec": "npm run build && node .",
    "test": "npm run build && npx jest",
    "prep": "npm run build && npm run test && npm run prettier"
  },
  "files": [
    "src",
    "typings"
  ],
  "repository": {
    "type": "git",
    "url": "git+https://github.com/Elitezen/easy-trivia.git"
  },
  "author": "Elitezen",
  "license": "ISC",
  "bugs": {
    "url": "https://github.com/Elitezen/easy-trivia/issues"
  },
  "homepage": "https://github.com/Elitezen/easy-trivia#readme",
  "devDependencies": {
    "@types/jest": "^27.0.3",
    "@types/node": "^16.11.7",
    "jest": "^27.5.1",
    "nodemon": "^2.0.12",
    "prettier": "2.4.1",
    "ts-jest": "^27.1.3"
  },
  "engines": {
    "node": ">=14.0.0",
    "npm": ">=7.0.0"
  }
}

1 Answer 1

1

To publish the types, you will need to explicitly include the dist directory in the files field of your package.json. Merely specifying the file in types won't actually include it. For main this is different, as it will automatically include the specified file. According to the npm package.json docs:

Certain files are always included, regardless of settings:

  • package.json
  • README
  • LICENSE / LICENCE
  • The file in the "main" field

To easily verify which files are included, npm pack --dry-run is handy.

Sign up to request clarification or add additional context in comments.

3 Comments

I've added files: ["dist/"] to tsconfig.json to no avail.
You'll need to add it to the package.json: "files": ["src", "typings"] ~> "files": ["dist", "src", "typings"]. Let me add this to the answer.
Ah I see, npm pack is now showing the files being packed as intended. Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.