{"record":{"id":"c7e385e4f26e2ae9","repo":"parcel-bundler/parcel","slug":"library-targets-are-not-supported-in-serve-mode","errorCode":null,"errorMessage":"Library targets are not supported in serve mode.","messagePattern":"Library targets are not supported in serve mode\\.","errorType":"exception","errorClass":"ThrowableDiagnostic","httpStatus":null,"severity":"error","filePath":"packages/core/core/src/requests/TargetRequest.js","lineNumber":378,"sourceCode":"        });\n\n      // Explicit targets were not provided. Either use a modern target for server\n      // mode, or simply use the package.json targets.\n      if (\n        this.options.serveOptions &&\n        targets.every(t => BROWSER_ENVS.has(t.env.context))\n      ) {\n        // In serve mode, we only support a single browser target. Since the user\n        // hasn't specified a target, use one targeting modern browsers for development\n        let distDir = toProjectPath(\n          this.options.projectRoot,\n          this.options.serveOptions.distDir,\n        );\n        let mainTarget = targets.length === 1 ? targets[0] : null;\n\n        if (mainTarget?.env.isLibrary) {\n          let loc = mainTarget.loc;\n          throw new ThrowableDiagnostic({\n            diagnostic: {\n              origin: '@parcel/core',\n              message: md`\nLibrary targets are not supported in serve mode.\n              `,\n              codeFrames: loc\n                ? [\n                    {\n                      filePath: fromProjectPath(\n                        this.options.projectRoot,\n                        loc.filePath,\n                      ),\n                      codeHighlights: [\n                        convertSourceLocationToHighlight(\n                          loc,\n                          'Target declared here',\n                        ),\n                      ],","sourceCodeStart":360,"sourceCodeEnd":396,"githubUrl":"https://github.com/parcel-bundler/parcel/blob/59484858a1a0bcbb71f74088956bb437a2db6505/packages/core/core/src/requests/TargetRequest.js#L360-L396","documentation":"Parcel's serve (dev-server) mode resolves a single browser target from package.json. When the only resolved target is a library target (one whose env.isLibrary is true, e.g. \"main\"/\"module\"/\"browser\" fields that point at a library build), TargetRequest refuses to serve it because library targets emit consumable artifacts, not runnable app bundles. The diagnostic is thrown only in serve mode when no explicit target was given and every target is browser-context and the lone target is a library.","triggerScenarios":"Running `parcel serve` (or the dev server) against a package whose package.json only defines library-style output fields (main/module/browser) that resolve to a library env, without passing an explicit `--target`.","commonSituations":"A library package misused as an application; pointing a dev server at a package designed to be published rather than served; migrating an app that accidentally declares `main` to a library output format; forgetting to add an `entries`/source field while only having `main`/`module`.","solutions":["Remove or rename the library target field (e.g. delete `main`/`module`) so Parcel treats the package as an app, or add an explicit app source entry.","Run serve with an explicit non-library target, e.g. `parcel serve src/index.html --target default`.","If this is genuinely a library, use `parcel build` instead of `parcel serve`."],"exampleFix":"// before (package.json)\n{\n  \"name\": \"my-lib\",\n  \"main\": \"dist/index.js\",\n  \"targets\": { \"main\": { \"isLibrary\": true } }\n}\n// $ parcel serve\n\n// after (package.json) — treat as app\n{\n  \"name\": \"my-app\",\n  \"source\": \"src/index.html\"\n}\n// $ parcel serve src/index.html","handlingStrategy":"validation","validationCode":"import { readFileSync } from 'fs';\nfunction assertServeSafe(pkgPath) {\n  const pkg = JSON.parse(readFileSync(pkgPath, 'utf8'));\n  const libFields = ['main','module','browser'];\n  const isLibTarget = (f) =>\n    pkg.targets?.[f]?.isLibrary === true ||\n    (libFields.includes(f) && pkg[f] && !pkg.source && !pkg.entries);\n  if (libFields.some(isLibTarget)) {\n    throw new Error('Cannot serve: package.json declares only library targets. Add a `source` field or use `parcel build`.');\n  }\n}","typeGuard":"function isLibraryTargetDescriptor(d) {\n  return d != null && (d.isLibrary === true || d.outputFormat === 'global');\n}","tryCatchPattern":"try {\n  const b = new Parcel({ entries, serveOptions: { port } });\n  await b.run();\n} catch (e) {\n  if (/Library targets are not supported in serve mode/.test(e.message)) {\n    console.error('Use `parcel build` for libraries, or add an app `source` field.');\n  } else throw e;\n}","preventionTips":["Distinguish library packages from app packages before invoking `parcel serve`.","Add a `source` (or `entries`) field to package.json for apps.","CI: run a pre-check that rejects `parcel serve` when package.json has only `main`/`module` with isLibrary."],"tags":["parcel","serve-mode","targets","library","config"],"backgroundTag":null,"analyzedSha":"59484858a1a0bcbb71f74088956bb437a2db6505","analyzedAt":"2026-08-13T04:06:35.925Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}