0

I am writing my very first npm package and I'm having trouble using it on a project that depends on it.

I have a NextJS application that I need to build which requires that the backend interfaces with our gRPC server. I have just finished building this interface and I am trying to publish it as an npm package. I was able to do so but when I try to use it on my NextJS application, I get a bunch of errors.

So I think the issue is how I am compiling my library (just my wild educated guess).

tsconfig.json

{
  "compilerOptions": {
    "module": "commonjs",
    "target": "es2017",
    "declaration": true,
    "outDir": "./dist",
    "esModuleInterop": true
  },
  "include": ["./src/**/*"]
}

package.json

{
  "name": "@company/grpc-client-js",
  "version": "0.1.0",
  "main": "./dist/index.js",
  "types": "./dist/index.d.ts",
  "files": [
    "./dist/",
    "./libraries/",
    "./node_modules/"
  ],
  "devDependencies": {
    "@babel/core": "^7.21.0",
    "@babel/preset-env": "^7.20.2",
    "@babel/preset-typescript": "^7.21.0",
    "@grpc/grpc-js": "^1.8.0",
    "@grpc/proto-loader": "^0.7.4",
    "@jest/test-sequencer": "^29.5.0",
    "@types/jest": "^29.4.0",
    "babel-jest": "^29.4.3",
    "grpc-tools": "^1.12.4",
    "jest": "^29.4.3",
    "ts-jest": "^29.0.5",
    "ts-node": "^10.9.1",
    "ts-protoc-gen": "^0.15.0",
    "typescript": "^4.9.4"
  },
  "dependencies": {
    "dotenv": "^16.0.3",
    "sha.js": "^2.4.11"
  }
}

Once the library is published as an npm package, I add it as a dependency on my main project:

yarn add @company/grpc-client-js

But then I get errors like:

Module not found: Can't resolve 'fs'

I try to fix that by adding a fallback as mentioned in this answer which did remove that error but I would just get more and more errors that I need to add to the fallbacks. But I feel like that's more of a patch up solution rather than the desired solution. How do I properly compile and publish this Typescript project that I have been working on so I can start using it on my main project?

dokgu
  • 4,957
  • 3
  • 39
  • 77
  • 1
    Are you sure you aren't using that module in the frontend? `fs` doesn't exist in the browser. – kelsny Mar 14 '23 at 20:13
  • @vr. ah silly me.. I was testing this library out from a component.. I should've been doing my test from the backend. I'll report back with what I find later. Thank you! – dokgu Mar 14 '23 at 20:15
  • Thank you @vr. I totally wasn't paying attention. Testing the library from `pages/api` does seem to work. Thanks again for pointing out my mistake.. it's been a long day! – dokgu Mar 14 '23 at 20:55

0 Answers0