Next.js Discord

Discord Forum

Trying to create an NPM package " Can't resolve 'net'" problem

Unanswered
Long-legged ant posted this in #help-forum
Open in Discord
Long-legged antOP
Hello, i create a next js 15 project, and i wanted to create an npm package (with only react). I isolate the components of the package, with a specific package.json, ect... I m using "tsup" for the fuild of this package, i build a dist folder and a package.tgz. However, on my next js app, when i import the package, i have this strange problem. Do you have any idea ? This is my tsup.config.ts :
import { defineConfig } from 'tsup'

export default defineConfig({
  entry: ['src/index.tsx'],
  format: ['esm', 'cjs'],
  dts: true,
  clean: true,
  sourcemap: false,
  bundle: true,
  splitting: false,

  external: [
    'react',
    'next',
    'agent-base',
    'react-dom',
    'fs',
    'path',
    'http',
    'https',
    'net',
  ],

  loader: {
    '.scss': 'empty',
  },
})
`and this is my package.json (of the created package)
{
  "name": "blabla",
  "version": "1.0.1",
  "description": "blabla",
  "scripts": {
    "build": "tsup"
  },
  "files": [
    "dist",
    "src/**/*.scss"
  ],
  "author": "Stephen Loiola Bastos",
  "license": "MIT",
  "private": false,
  "main": "dist/index.js",
  "module": "dist/index.js",
  "types": "dist/index.d.ts",
  "sideEffects": [
    "**/*.scss"
  ],
  "peerDependencies": {
    "@tippyjs/react": "^4.2.6",
    "react": ">=18",
    "react-dom": ">=18",
    "yup": "^1.6.1"
  },
  "devDependencies": {
    "fs-extra": "^11.3.2",
    "postcss": "^8.5.6",
    "postcss-modules": "^6.0.1",
    "react": "^18.3.1",
    "react-dom": "^18.3.1",
    "sass": "^1.94.2",
    "tsup": "^8.5.1",
    "typescript": "^5.9.2"
  }
}
`. Thank you.

1 Reply

Western paper wasp
The error occurs because the package pulls in Node.js modules like net, fs or http, which aren’t available in the browser. For a client side package, you need to remove the Node API usage, move that code to the server side or build a separate browser version without those dependencies