!npmnpmnpm-url
!nodenodenode-url
!depsdepsdeps-url
!teststeststests-url
!coveragecovercover-url
!chatchatchat-url
!sizesizesize-url
Type:
Type:
webpack.config.js
```js
module.exports = {
target: "node",
node: {
webpack.config.js
```js
module.exports = {
target: "node",
node: {
node-loader
A Node.js add-ons loader.
Allows to connect native node modules with .node
extension.
⚠node-loader
only works on thenode
/async-node
/electron-main
/electron-renderer
/electron-preload
targets.
Getting Started
To begin, you'll need to installnode-loader
:
```console
$ npm install node-loader --save-dev
```
Setup the target
option to node
/async-node
/electron-main
/electron-renderer
/electron-preload
value and do not mock the __dirname
global variable.
webpack.config.js
```js
module.exports = {
target: "node",
node: {
__dirname: false,
},
module: {
rules: [
{
test: /\.node$/,
loader: "node-loader",
},
],
},
};
```
Inline
index.js ```js import node from "node-loader!./file.node"; ``` And runwebpack
via your preferred method.
Configuration
index.js ```js import node from "file.node"; ``` Then add the loader to yourwebpack
config. For example:
webpack.config.js
```js
module.exports = {
target: "node",
node: {
__dirname: false,
},
module: {
rules: [
{
test: /\.node$/,
loader: "node-loader",
},
],
},
};
```
And run webpack
via your preferred method.
Options
| Name | Type | Default | Description | | :-------------------: | :------------------: | :---------------------: | :----------------------------------------------------------- | |flags
| {Number}
| undefined
| Enables/Disables url
/image-set
functions handling |
| name
| {String\|Function}
| '[contenthash].[ext]'
| Specifies a custom filename template for the target file(s). |
flags
Type: Number
Default: undefined
The flags
argument is an integer that allows to specify dlopen behavior.
See the process.dlopen
documentation for details.
index.js
```js
import node from "file.node";
```
webpack.config.js
```js
const os = require("os");
module.exports = {
target: "node",
node: {
__dirname: false,
},
module: {
rules: [
{
test: /\.node$/,
loader: "node-loader",
options: {
flags: os.constants.dlopen.RTLD_NOW,
},
},
],
},
};
```
name
Type: String|Function
Default: '[contenthash].[ext]'
Specifies a custom filename template for the target file(s).
String
webpack.config.js
```js
module.exports = {
target: "node",
node: {
__dirname: false,
},
module: {
rules: [
{
test: /\.node$/,
loader: "node-loader",
options: {
name: "[path][name].[ext]",
},
},
],
},
};
```
Function
webpack.config.js
```js
module.exports = {
target: "node",
node: {
__dirname: false,
},
module: {
rules: [
{
test: /\.node$/,
loader: "node-loader",
options: {
name(resourcePath, resourceQuery) {
// `resourcePath` - `/absolute/path/to/file.js`
// `resourceQuery` - `?foo=bar`
if (process.env.NODE_ENV === "development") {
return "[path][name].[ext]";
}
return "[contenthash].[ext]";
},
},
},
],
},
};
```