Skip to content
This repository was archived by the owner on Oct 22, 2024. It is now read-only.

Commit 26bf469

Browse files
authored
Merge pull request #54 from eisberg-labs/fix-loader
fix: update readme and example
2 parents ebb4220 + 5685397 commit 26bf469

File tree

12 files changed

+807
-147
lines changed

12 files changed

+807
-147
lines changed

README.md

Lines changed: 95 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,95 @@
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)
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import msnodesql from 'msnodesqlv8';
2+
3+
export async function GET() {
4+
console.log(msnodesql)
5+
return Response.json({'status': 'ok'});
6+
}

examples/next-14/app/api/slugify/[slug]/route.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,5 @@ export async function GET(
66
{ params }: { params: { slug: string } }
77
) {
88
const slug = slugifier.slugify(params.slug);
9-
109
return Response.json({message: slug});
1110
}

examples/next-14/inspectnext.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
const path = require("path");
2+
const nextBuild = require('next/dist/build').default;
3+
4+
async function inspectWebpackConfig() {
5+
const appDir = path.resolve(__dirname);
6+
const debugOutput = true;
7+
const runLint = false;
8+
const noMangling = true;
9+
const appDirOnly = true;
10+
const turboNextBuild = false;
11+
const experimentalBuildMode = false;
12+
13+
try {
14+
void await nextBuild(appDir, {
15+
16+
// Hook into the webpack configuration before it builds
17+
webpack(config, { isServer }) {
18+
// Inspect or modify the webpack configuration here
19+
console.log('Inspecting Webpack configuration...');
20+
console.log(config);
21+
22+
// Optionally, save the Webpack config to a file for easier inspection
23+
fs.writeFileSync(
24+
path.join(__dirname, 'webpack-config.json'),
25+
JSON.stringify(config, null, 2)
26+
);
27+
28+
console.log('Webpack config saved to webpack-config.json');
29+
30+
// Return the modified or original config
31+
return config;
32+
},
33+
}, debugOutput, runLint, noMangling, appDirOnly, turboNextBuild, experimentalBuildMode);
34+
35+
console.log('Next.js build completed successfully.');
36+
} catch (error) {
37+
console.error('Error during Next.js build:', error);
38+
}
39+
}
40+
41+
inspectWebpackConfig();

examples/next-14/next.config.mjs

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,16 @@
1-
import os from 'os';
2-
31
/** @type {import('next').NextConfig} */
42
const nextConfig = {
5-
webpack: (config) => {
6-
config.module.rules.push({
7-
test: /\.node$/,
8-
use: [
9-
{
10-
loader: 'nextjs-node-loader',
11-
options: {
12-
flags: os.constants.dlopen.RTLD_NOW,
13-
outputPath: config.output.path
14-
},
15-
},
16-
],
17-
});
18-
3+
experimental: {
4+
serverComponentsExternalPackages: ['msnodesqlv8','robotjs', 'node-el-slugify'],
5+
},
6+
/** @type {import('webpack').Configuration} */
7+
webpack: (config, context) => {
8+
if (context.isServer) {
9+
config.externals = [
10+
...config.externals,
11+
{'node-el-slugify': 'commonjs node-el-slugify'},
12+
]
13+
}
1914
return config;
2015
},
2116
};

0 commit comments

Comments
 (0)