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?