I published a library user-credits on npm, but I'm unable to access it from my other projects.

When accessing it locally, pointing to the folder as follows:

package.json

{
  "name": "user-credits-test",
  "version": "1.0.0",
  "description": "Testing user-credits library",
  "scripts": {
    "build": "tsc",
    "test": "node ./dist/test.js"
  },
  "type": "module",
  "dependencies": {
    "user-credits": "../../UserCredits"
  },
  "devDependencies": {
    "typescript": "^5.2.2"
  }
}

And this tsconfig.js:

{
  "compilerOptions": {
    "moduleResolution": "node",
    "target": "ESNext",
    "module": "ESNext",
    "outDir": "dist",
    "rootDir": "src",
    "baseUrl": ".",
  },
  "include": ["src/**/*.ts"],
  "exclude": ["node_modules", "dist"]
}

It compiles and runs with no problem!

But when I publish that exact directory to npm and import it from as a remote dependency ("user-credits": "0.9.4-alpha"):

package.json

{
  "name": "user-credits-test",
  "version": "1.0.0",
  "description": "Testing user-credits library",
  "scripts": {
    "build": "tsc",
    "test": "node ./dist/test.js"
  },
  "type": "module",
  "dependencies": {
    "user-credits": "0.9.4-alpha"
  },
  "devDependencies": {
    "typescript": "^5.2.2"
  }
}

Now it complains with the error

src/test.ts:1:36 - error TS2307: Cannot find module 'user-credits' or its corresponding type declarations.

1 import { IOffer, OfferCycle } from 'user-credits';
                                     ~~~~~~~~~~~~~~

I started this test project because I lost an entire day trying to fix the error below on my sveltekit project.

ReferenceError: exports is not defined

So I thought I'd verify that user-credits is properly published. The testing project here has a single test.ts file.

I don't want to enter into details about how user-credits is structured, as I think there's something I'm missing about using exactly the same project locally then publishing it and having a different behavior.

1

There are 1 best solutions below

0
On

Ok,

This was related to my package.json file that was missing
  "files": [
    "dist" 
  ],

Updating it and publishing worked as expected:

{
  "name": "user-credits",
  "type": "module",
  "version": "0.9.05-alpha",
  "license": "MIT",
  "description": "user-credits is an open-source library designed to simplify the implementation of pay-as-you-go features in your web or mobile applications. Credit flow is managed locally instead of being sent back and forth to payment platforms as Stripe, in addition, you own your data without paying the price of long development hours.",
  "main": "./dist/index.js",
  "module": "./dist/index.js",
  "exports": {
    ".": {
      "import": "./dist/index.js",
      "require": "./dist/index.js"
    }
  },
  "files": [
    "dist"
  ],
  "scripts": {
    "buildX": "rm -rf ./dist && npx tsc",
    "build": "rmdir /s /q .\\dist & npx tsc",
    "lint": "npx eslint --fix .",
    "prepublishOnly": "npm run lint && npm run build && npm test",
    "test": "node --experimental-vm-modules node_modules/jest/bin/jest.js --coverage --coverageDirectory='coverage'"
  },
  "devDependencies": {
    "@jest/globals": "^29.7.0",
    "@tsconfig/recommended": "^1.0.3",
    "@types/jest": "^29.5.5",
    "@typescript-eslint/eslint-plugin": "^6.7.2",
    "@typescript-eslint/parser": "^6.7.2",
    "eslint": "^8.50.0",
    "eslint-config-prettier": "^9.0.0",
    "eslint-plugin-prettier": "^5.0.0",
    "eslint-plugin-simple-import-sort": "^10.0.0",
    "eslint-plugin-sort-destructure-keys": "^1.5.0",
    "eslint-plugin-sort-keys-fix": "^1.1.2",
    "eslint-plugin-typescript-sort-keys": "^3.0.0",
    "expect": "^29.7.0",
    "jest": "^29.7.0",
    "mongodb-memory-server": "^8.15.1",
    "prettier": "^3.0.3",
    "ts-jest": "^29.1.1",
    "typescript": "^5.2.2"
  },
  "engines": {
    "node": ">=18"
  },
  "dependencies": {
    "awilix": "^8.0.1",
    "bson": "^6.1.0",
    "dotenv": "^16.3.1",
    "mongoose": "^7.5.3",
    "stripe": "^13.9.0"
  }
}