Status: Deprecated
There's no need for it anymore. I've added an example in next 14 to demo including different native modules. Solution in NextJS 14 is to add a:
experimental: {
serverComponentsExternalPackages: ['yournativemodule'],
}
For a dependency built with neon bindings, you might also need to configure externals
:
/** @type {import('next').NextConfig} */
const nextConfig = {
experimental: {
serverComponentsExternalPackages: ['mynativemodule'],
},
/** @type {import('webpack').Configuration} */
webpack: (config, context) => {
if (context.isServer) {
config.externals = [
...config.externals,
{'mynativemodule': 'commonjs mynativemodule'},
]
}
return config;
},
};
A huge thank you to everyone who contributed to this project! ❤️
For historical purposes, the original README is preserved below.
This is a custom loader for Webpack that allows you to include native Node.js .node
modules in your Next.js project.
It simplifies the process of loading native modules by providing a standardized interface that works seamlessly with
Next.js.
This is a modified version of Node loader.
More context on the why and example use of the loader in my blog post.
To begin, you'll need to install a nextjs-node-loader
:
npm install nextjs-node-loader --save-dev
next.config.js
module.exports = {
webpack: (config, { dev, isServer, webpack, nextRuntime }) => {
config.module.rules.push({
test: /\.node$/,
use: [
{
loader: "nextjs-node-loader",
options: {
flags: os.constants.dlopen.RTLD_NOW,
outputPath: config.output.path
}
},
],
});
return config;
},
};
And use in e.g. your api route;
import module from "node-module";
export default function handler(req, res) {
// ...
}
Name | Type | Default | Description |
---|---|---|---|
flags | {Number} |
undefined |
Enables/Disables url /image-set functions handling |
outputPath | {String} |
webpack's outputPath | The root path of shared node libraries |
includeWebpackPublicPath | {String} |
false | If webpack_public_path should be included in a path for loading node module. For nextjs >13.2.5 should be false. |