Security News
ESLint is Now Language-Agnostic: Linting JSON, Markdown, and Beyond
ESLint has added JSON and Markdown linting support with new officially-supported plugins, expanding its versatility beyond JavaScript.
node-loader
Advanced tools
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.
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);
The webpack-node-externals package is a webpack plugin that excludes Node.js modules from the output bundle. It is useful for server-side applications where you want to keep the Node.js modules separate from the client-side bundle. Unlike node-loader, which allows importing Node.js modules, webpack-node-externals focuses on excluding them.
The babel-loader package is a webpack loader that allows you to transpile JavaScript files using Babel. While it does not specifically target Node.js modules, it can be used in conjunction with other loaders to handle modern JavaScript syntax and features. It complements node-loader by enabling the use of ES6+ syntax in Node.js modules.
A Node.js add-ons loader.
Allows to connect native node modules with .node
extension.
⚠
node-loader
only works on thenode
/electron-main
/electron-main
targets.
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',
},
],
},
};
index.js
import node from 'node-loader!./file.node';
And run webpack
via your preferred method.
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.
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
][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]';
},
},
},
],
},
};
Please take a moment to read our contributing guidelines if you haven't yet done so.
FAQs
A Node loader module for enhanced-require
We found that node-loader demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 8 open source maintainers collaborating on the project.
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.
Security News
ESLint has added JSON and Markdown linting support with new officially-supported plugins, expanding its versatility beyond JavaScript.
Security News
Members Hub is conducting large-scale campaigns to artificially boost Discord server metrics, undermining community trust and platform integrity.
Security News
NIST has failed to meet its self-imposed deadline of clearing the NVD's backlog by the end of the fiscal year. Meanwhile, CVE's awaiting analysis have increased by 33% since June.