npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

nextjs-server-modules

v4.8.2

Published

A NextJS Server Module is a NextJS project that has been packaged to be easily used as an npm module. You might use a server module to...

Downloads

15,335

Readme

NextJS Server Modules

A NextJS Server Module is a NextJS project that has been packaged to be easily used as an npm module. You might use a server module to...

  • Package a "fake" or "mock" server for easy use in test suites
  • Allow another server to easily "call into" your NextJS server
  • Improve the organization of a large project by organizing NextJS servers into server sub-components

The nextjs-server-module cli will help you create a custom nextjs runtime to run your projects with, so they can be run without nextjs at runtime.

Usage

First, install with npm install nextjs-server-modules

You can now add a build script or just run nsm build

The build process will output a .nsm/index.ts file which can be used to create your server or invoke requests against it

import myNextJSModule from "./.nsm"

const server = await myNextJSModule({ port: 3030 })

// your server is running on localhost:3030!

server.close()

Middlewares

You can provide middlewares to your server like so:

import myNextJSModule from "./.nsm"

const myMiddleware = (next) => (req, res) => {
  req.token = req.headers.get("authorization").split("Bearer ")?.[1]
  return next(req, res)
}

const server = await myNextJSModule({
  port: 3030,
  middlewares: [myMiddleware],
})

// your server is running on localhost:3030!

server.close()

Common Scripts

Many times you'll want to add nsm when you start testing API endpoints on a NextJS project, it makes a lot of sense to use nextless and skip-build which runs a very fast build.

{
  "scripts": {
    "build:test": "nsm build --nextless --skip-build --only-api-files"
  }
}

Internal: How it Works

Not all NextJS features are currently supported. Particularly:

  • SSR
  • getStaticProps

We build the api endpoints and export static pages with next build. We then analyze .next/server/pages/api to get all the api routes to server.

We then construct a main export file that knows how to parse next.config.js and route to the correct files, which we've statically analyzed and are included in the generated dist/index.js file.

Caveats

Vercel's Edge Runtime is supported only when running in a Node.js environment. In other words, a bundled nsm project cannot be run in the Edge Runtime.

nsm's implementation of the Edge Runtime does not support:

FAQ

Why can't Next.js bundle into an npm module?

You can bundle nextjs into npm modules, but some static analysis isn't available since nextjs uses the directory structure to determine what to load in at runtime.

You'll also have problems running a next server within a Vercel endpoint, because some vercel optimizations get rid of some webpack/next modules as an optimization.

Why is my module bigger?

NSM base64 encodes static files so that they're bundleable, it's very inefficient.