Socket
Socket
Sign inDemoInstall

node-loader

Package Overview
Dependencies
81
Maintainers
8
Versions
7
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    node-loader

A Node loader module for enhanced-require


Version published
Weekly downloads
318K
decreased by-44.57%
Maintainers
8
Install size
429 kB
Created
Weekly downloads
 

Package description

What is node-loader?

The node-loader npm package is a loader for webpack that allows you to import Node.js modules in your webpack bundle. It enables the use of Node.js built-in modules and other Node.js-specific features within a webpack project.

What are node-loader's main functionalities?

Importing Node.js Modules

This feature allows you to import and use Node.js built-in modules like 'fs' within your webpack project. The code sample demonstrates reading a file synchronously using the 'fs' module.

const fs = require('fs');
const data = fs.readFileSync('/path/to/file', 'utf8');
console.log(data);

Using Node.js Specific Features

This feature enables the use of Node.js-specific features such as the 'path' module. The code sample shows how to join directory paths using the 'path' module.

const path = require('path');
const fullPath = path.join(__dirname, 'file.txt');
console.log(fullPath);

Other packages similar to node-loader

Changelog

Source

2.0.0 (2021-04-05)

⚠ BREAKING CHANGES

  • minimum supported webpack version is 5

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

Readme

Source

npm node deps tests coverage chat 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/electron-main/electron-main targets.

Getting Started

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

$ npm install node-loader --save-dev

Setup the target option to node/electron-main/electron-main value and do not mock the __dirname global variable.

webpack.config.js

module.exports = {
  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

NameTypeDefaultDescription
flags{Number}undefinedEnables/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][https://nodejs.org/api/process.html#process_process_dlopen_module_filename_flags] 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

Keywords

FAQs

Last updated on 05 Apr 2021

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc