isaacs/rimraf

Typescript: Cannot find type declarations

caillou opened this issue · 2 comments

I hope this is not user error.

Starting with v5.0.2, I get an error when importing rimraf in typescript:

$ ./node_modules/.bin/tsc import-ts.ts

import-ts.ts:1:24 - error TS2307: Cannot find module 'rimraf' or its corresponding type declarations.

1 import { rimraf } from 'rimraf';
                         ~~~~~~~~

Here is a repo to reproduce: https://github.com/caillou/rimraf-5.0.4-error

Updating the import to the following fixes the error:

import { rimraf } from 'rimraf/dist/esm';

PS: I love the rimraf module. I use it on most my projects. Thanks for the hard work!

Looking at the package.json, it seems that the following properties went missing:

  • "main": "./dist/cjs/src/index.js",
  • "module": "./dist/mjs/index.js",
  • "types": "./dist/mjs/index.d.ts",
{
  "name": "rimraf",
  "version": "5.0.1",
  "main": "./dist/cjs/src/index.js",
  "module": "./dist/mjs/index.js",
  "types": "./dist/mjs/index.d.ts",
  "bin": "./dist/cjs/src/bin.js",
  "exports": {
    ".": {
      "import": {
        "types": "./dist/mjs/index.d.ts",
        "default": "./dist/mjs/index.js"
      },
      "require": {
        "types": "./dist/cjs/src/index.d.ts",
        "default": "./dist/cjs/src/index.js"
      }
    }
  },
  "files": [
    "dist"
  ],
  "description": "A deep deletion module for node (like `rm -rf`)",
  "author": "Isaac Z. Schlueter <i@izs.me> (http://blog.izs.me/)",
  "license": "ISC",
  "repository": "git://github.com/isaacs/rimraf.git",
  "scripts": {
    "preversion": "npm test",
    "postversion": "npm publish",
    "prepublishOnly": "git push origin --follow-tags",
    "preprepare": "rm -rf dist",
    "prepare": "tsc -p tsconfig.json && tsc -p tsconfig-esm.json",
    "postprepare": "bash fixup.sh",
    "pretest": "npm run prepare",
    "presnap": "npm run prepare",
    "test": "c8 tap",
    "snap": "c8 tap",
    "format": "prettier --write . --loglevel warn",
    "benchmark": "node benchmark/index.js",
    "typedoc": "typedoc --tsconfig tsconfig-esm.json ./src/*.ts"
  },
  "prettier": {
    "semi": false,
    "printWidth": 80,
    "tabWidth": 2,
    "useTabs": false,
    "singleQuote": true,
    "jsxSingleQuote": false,
    "bracketSameLine": true,
    "arrowParens": "avoid",
    "endOfLine": "lf"
  },
  "devDependencies": {
    "@types/node": "^18.11.9",
    "@types/tap": "^15.0.7",
    "c8": "^7.12.0",
    "eslint-config-prettier": "^8.6.0",
    "mkdirp": "^3.0.0",
    "prettier": "^2.8.2",
    "tap": "^16.3.4",
    "ts-node": "^10.9.1",
    "typedoc": "^0.23.21",
    "typescript": "^5.0.4"
  },
  "tap": {
    "coverage": false,
    "libtap-settings": "libtap-settings.js",
    "node-arg": [
      "--no-warnings",
      "--loader",
      "ts-node/esm"
    ],
    "ts": false
  },
  "funding": {
    "url": "https://github.com/sponsors/isaacs"
  },
  "engines": {
    "node": ">=14"
  },
  "dependencies": {
    "glob": "^10.2.5"
  },
  "keywords": [
    "rm",
    "rm -rf",
    "rm -fr",
    "remove",
    "directory",
    "cli",
    "rmdir",
    "recursive"
  ]
}
{
  "name": "rimraf",
  "version": "5.0.2",
  "bin": "./dist/esm/bin.mjs",
  "exports": {
    "./package.json": "./package.json",
    ".": {
      "import": {
        "types": "./dist/esm/index.d.ts",
        "default": "./dist/esm/index.js"
      },
      "require": {
        "types": "./dist/commonjs/index.d.ts",
        "default": "./dist/commonjs/index.js"
      }
    }
  },
  "files": [
    "dist"
  ],
  "description": "A deep deletion module for node (like `rm -rf`)",
  "author": "Isaac Z. Schlueter <i@izs.me> (http://blog.izs.me/)",
  "license": "ISC",
  "repository": "git://github.com/isaacs/rimraf.git",
  "scripts": {
    "preversion": "npm test",
    "postversion": "npm publish",
    "prepublishOnly": "git push origin --follow-tags",
    "prepare": "tshy",
    "pretest": "npm run prepare",
    "presnap": "npm run prepare",
    "test": "tap",
    "snap": "tap",
    "format": "prettier --write . --loglevel warn",
    "benchmark": "node benchmark/index.js",
    "typedoc": "typedoc --tsconfig .tshy/esm.json ./src/*.ts"
  },
  "prettier": {
    "semi": false,
    "printWidth": 80,
    "tabWidth": 2,
    "useTabs": false,
    "singleQuote": true,
    "jsxSingleQuote": false,
    "bracketSameLine": true,
    "arrowParens": "avoid",
    "endOfLine": "lf"
  },
  "devDependencies": {
    "@types/node": "^20.6.5",
    "mkdirp": "^3.0.1",
    "prettier": "^2.8.2",
    "tap": "^18.1.4",
    "tshy": "^1.1.1",
    "typedoc": "^0.25.1",
    "typescript": "^5.2"
  },
  "funding": {
    "url": "https://github.com/sponsors/isaacs"
  },
  "engines": {
    "node": ">=14"
  },
  "dependencies": {
    "glob": "^10.3.7"
  },
  "keywords": [
    "rm",
    "rm -rf",
    "rm -fr",
    "remove",
    "directory",
    "cli",
    "rmdir",
    "recursive"
  ],
  "type": "module",
  "tshy": {
    "exports": {
      "./package.json": "./package.json",
      ".": "./src/index.ts"
    }
  }
}
isaacs commented

The types you were getting previously were incorrect in many cases.

You need to tell tsc to support package.json exports. The easiest way to do this is to set module and moduleResolution to node16 or nodenext in the compilerOptions object in tsconfig.json.