Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

node-loader

webpack-contrib1.7mMIT2.1.0

A Node loader module for enhanced-require

webpack

readme

npm node tests coverage discussion size

node-loader

A Node.js add-ons loader.

Allows to connect native node modules with .node extension.

node-loader only works on the node/async-node/electron-main/electron-renderer/electron-preload targets.

Getting Started

To begin, you'll need to install node-loader:

npm install node-loader --save-dev

or

yarn add -D node-loader

or

pnpm add -D node-loader

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

module.exports = {
  resolve: {
    extensions: ["...", ".node"],
  },
  target: "node",
  node: {
    __dirname: false,
  },
  module: {
    rules: [
      {
        test: /\.node$/,
        loader: "node-loader",
      },
    ],
  },
};

Inline

index.js

import node from "node-loader!./file.node";

And run webpack via your preferred method.

Configuration

index.js

import node from "file.node";

Then add the loader to your webpack config. For example:

webpack.config.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

import node from "file.node";

webpack.config.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

module.exports = {
  target: "node",
  node: {
    __dirname: false,
  },
  module: {
    rules: [
      {
        test: /\.node$/,
        loader: "node-loader",
        options: {
          name: "[path][name].[ext]",
        },
      },
    ],
  },
};

Function

webpack.config.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]";
          },
        },
      },
    ],
  },
};

Contributing

Please take a moment to read our contributing guidelines if you haven't yet done so.

CONTRIBUTING

License

MIT

changelog

Changelog

All notable changes to this project will be documented in this file. See standard-version for commit guidelines.

2.1.0 (2024-11-13)

Features

  • display helpful description and links on error (#54) (31c0009)

Bug Fixes

  • dlopen relative part problem in mac system (0507d0b)

2.0.0 (2021-04-05)

⚠ BREAKING CHANGES

  • minimum supported webpack version is 5

  • dropped webpack@4 (#33) (de9d1a5)

1.0.3 (2021-04-05)

Bug Fixes

  • concat directory separators instead of "/" to make it work on Windows #39 (#40) (3eb7041)

1.0.2 (2020-10-09)

Chore

  • update schema-utils

1.0.1 (2020-07-28)

Bug Fixes

1.0.0 (2020-06-26)

⚠ BREAKING CHANGES

  • minimum supported Node.js version is 10.13
  • minimum supported webpack version is 4

Features

Fixes

  • copy .node files to the build directory
  • do not put absolute paths in the bundle file (no problems with [contenthash])

Change Log

All notable changes to this project will be documented in this file. See standard-version for commit guidelines.