|
1 | | -# nextjs-node-loader |
2 | | - |
3 | | -This is a custom loader for Webpack that allows you to include native Node.js `.node` modules in your Next.js project. |
4 | | -It simplifies the process of loading native modules by providing a standardized interface that works seamlessly with |
5 | | -Next.js. |
6 | | -This is a modified version of [Node loader](https://github.com/webpack-contrib/node-loader). |
7 | | -More context on the [why and example use of the loader in my blog post](https://www.amarjanica.com/nextjs-and-rust-creating-a-custom-webpack-loader-for-native-node-modules). |
8 | | - |
9 | | -## Getting Started |
10 | | - |
11 | | -To begin, you'll need to install a `nextjs-node-loader`: |
12 | | - |
13 | | -```console |
14 | | -npm install nextjs-node-loader --save-dev |
15 | | -``` |
16 | | - |
17 | | -**next.config.js** |
18 | | - |
19 | | -```js |
20 | | -module.exports = { |
21 | | - webpack: (config, { dev, isServer, webpack, nextRuntime }) => { |
22 | | - config.module.rules.push({ |
23 | | - test: /\.node$/, |
24 | | - use: [ |
25 | | - { |
26 | | - loader: "nextjs-node-loader", |
27 | | - options: { |
28 | | - flags: os.constants.dlopen.RTLD_NOW, |
29 | | - outputPath: config.output.path |
30 | | - } |
31 | | - }, |
32 | | - ], |
33 | | - }); |
34 | | - return config; |
35 | | - }, |
36 | | -}; |
37 | | -``` |
38 | | - |
39 | | -And use in e.g. your api route; |
40 | | - |
41 | | -```javascript |
42 | | -import module from "node-module"; |
43 | | - |
44 | | -export default function handler(req, res) { |
45 | | - // ... |
46 | | -} |
47 | | -``` |
48 | | - |
49 | | -## Options |
50 | | - |
51 | | -| Name | Type | Default | Description | |
52 | | -|:----------:|:----------:|:--------------------:| :---------------------------------------------------- | |
53 | | -| flags | `{Number}` | `undefined` | Enables/Disables `url`/`image-set` functions handling | |
54 | | -| outputPath | `{String}` | webpack's outputPath | The root path of shared node libraries | |
55 | | -| 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. | |
56 | | - |
57 | | -## License |
58 | | - |
59 | | -[MIT](./LICENSE) |
| 1 | +# nextjs-node-loader (No Longer Maintained) |
| 2 | + |
| 3 | +**Status: Deprecated** |
| 4 | + |
| 5 | +There's no need for it anymore. I've added an example in [next 14](./examples/next-14) to demo including different native modules. |
| 6 | +Solution in NextJS 14 is to add a: |
| 7 | +```javascript |
| 8 | +experimental: { |
| 9 | + serverComponentsExternalPackages: ['yournativemodule'], |
| 10 | +} |
| 11 | +``` |
| 12 | +For a dependency built with neon bindings, you might also need to configure `externals`: |
| 13 | +```javascript |
| 14 | +/** @type {import('next').NextConfig} */ |
| 15 | +const nextConfig = { |
| 16 | + experimental: { |
| 17 | + serverComponentsExternalPackages: ['mynativemodule'], |
| 18 | + }, |
| 19 | + /** @type {import('webpack').Configuration} */ |
| 20 | + webpack: (config, context) => { |
| 21 | + if (context.isServer) { |
| 22 | + config.externals = [ |
| 23 | + ...config.externals, |
| 24 | + {'mynativemodule': 'commonjs mynativemodule'}, |
| 25 | + ] |
| 26 | + } |
| 27 | + return config; |
| 28 | + }, |
| 29 | +}; |
| 30 | +``` |
| 31 | + |
| 32 | +A huge thank you to everyone who contributed to this project! ❤️ |
| 33 | + |
| 34 | +For historical purposes, the original README is preserved below. |
| 35 | + |
| 36 | + |
| 37 | +# nextjs-node-loader |
| 38 | + |
| 39 | +This is a custom loader for Webpack that allows you to include native Node.js `.node` modules in your Next.js project. |
| 40 | +It simplifies the process of loading native modules by providing a standardized interface that works seamlessly with |
| 41 | +Next.js. |
| 42 | +This is a modified version of [Node loader](https://github.com/webpack-contrib/node-loader). |
| 43 | +More context on the [why and example use of the loader in my blog post](https://www.amarjanica.com/nextjs-and-rust-creating-a-custom-webpack-loader-for-native-node-modules). |
| 44 | + |
| 45 | +## Getting Started |
| 46 | + |
| 47 | +To begin, you'll need to install a `nextjs-node-loader`: |
| 48 | + |
| 49 | +```console |
| 50 | +npm install nextjs-node-loader --save-dev |
| 51 | +``` |
| 52 | + |
| 53 | +**next.config.js** |
| 54 | + |
| 55 | +```js |
| 56 | +module.exports = { |
| 57 | + webpack: (config, { dev, isServer, webpack, nextRuntime }) => { |
| 58 | + config.module.rules.push({ |
| 59 | + test: /\.node$/, |
| 60 | + use: [ |
| 61 | + { |
| 62 | + loader: "nextjs-node-loader", |
| 63 | + options: { |
| 64 | + flags: os.constants.dlopen.RTLD_NOW, |
| 65 | + outputPath: config.output.path |
| 66 | + } |
| 67 | + }, |
| 68 | + ], |
| 69 | + }); |
| 70 | + return config; |
| 71 | + }, |
| 72 | +}; |
| 73 | +``` |
| 74 | + |
| 75 | +And use in e.g. your api route; |
| 76 | + |
| 77 | +```javascript |
| 78 | +import module from "node-module"; |
| 79 | + |
| 80 | +export default function handler(req, res) { |
| 81 | + // ... |
| 82 | +} |
| 83 | +``` |
| 84 | + |
| 85 | +## Options |
| 86 | + |
| 87 | +| Name | Type | Default | Description | |
| 88 | +|:----------:|:----------:|:--------------------:| :---------------------------------------------------- | |
| 89 | +| flags | `{Number}` | `undefined` | Enables/Disables `url`/`image-set` functions handling | |
| 90 | +| outputPath | `{String}` | webpack's outputPath | The root path of shared node libraries | |
| 91 | +| 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. | |
| 92 | + |
| 93 | +## License |
| 94 | + |
| 95 | +[MIT](./LICENSE) |
0 commit comments