{"id":20649,"library":"val-loader","title":"val-loader","description":"A webpack loader that executes a given module at build-time and returns the result as the module's code. Current stable version is 6.0.0 (2024-01-15). It allows custom loader logic without writing a full loader; the target module receives (options, loaderContext) and must return an object with at least a 'code' property. Supports returning source maps, AST, dependencies, and context dependencies. Requires webpack 5.x and Node.js >= 18.12.0. Releases follow semver, with breaking changes only for Node.js and webpack version bumps. Key differentiator: simplifies creating loaders for static value generation or compile-time computation.","status":"active","version":"6.0.0","language":"javascript","source_language":"en","source_url":"https://github.com/webpack-contrib/val-loader","tags":["javascript","webpack","loader","execute","val"],"install":[{"cmd":"npm install val-loader","lang":"bash","label":"npm"},{"cmd":"yarn add val-loader","lang":"bash","label":"yarn"},{"cmd":"pnpm add val-loader","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Peer dependency: requires webpack 5.x to function as a loader","package":"webpack","optional":false}],"imports":[{"note":"val-loader is a webpack loader, not a JavaScript module. It is configured in webpack config, not imported in source code.","wrong":"import valLoader from 'val-loader'","symbol":"val-loader","correct":"module.exports = { module: { rules: [ { test: /\\.js$/, use: 'val-loader' } ] } }"},{"note":"The target file must use CommonJS (module.exports) because webpack loaders expect a function exported via module.exports.","wrong":"export default (options, loaderContext) => { ... }","symbol":"target module function","correct":"module.exports = (options, loaderContext) => { return { code: '...' }; }"},{"note":"The executableFile option expects an absolute path. Relative paths may fail silently.","wrong":"options: { executableFile: './exec.js' }","symbol":"executableFile","correct":"options: { executableFile: path.resolve(__dirname, 'exec.js') }"}],"quickstart":{"code":"// target-file.js\nmodule.exports = (options, loaderContext) => {\n  return { code: 'module.exports = ' + JSON.stringify(options) + ';' };\n};\n\n// webpack.config.js\nconst path = require('path');\nmodule.exports = {\n  module: {\n    rules: [\n      {\n        test: /\\.val\\.js$/,\n        use: [\n          {\n            loader: 'val-loader',\n            options: { greeting: 'Hello, world!' }\n          }\n        ]\n      }\n    ]\n  }\n};\n\n// input.val.js\nmodule.exports = (options, loaderContext) => {\n  return { code: `module.exports = \"${options.greeting}\";` };\n};\n\n// entry.js\nconst greeting = require('./input.val.js');\nconsole.log(greeting); // 'Hello, world!'","lang":"javascript","description":"Shows how to configure val-loader with options and a target module that uses those options to generate code."},"warnings":[{"fix":"Upgrade Node.js to >=18.12.0","message":"Node.js version 18.12.0 or higher is required. Older versions are not supported.","severity":"breaking","affected_versions":">=6.0.0"},{"fix":"Upgrade webpack to 5.x","message":"webpack version 5.x is required. Not compatible with webpack 4.","severity":"breaking","affected_versions":">=3.0.0"},{"fix":"Upgrade Node.js to >=14.15.0 or use val-loader v4","message":"Node.js version 14.15.0 or higher is required for val-loader v5.","severity":"breaking","affected_versions":">=5.0.0 <6.0.0"},{"fix":"Use module.exports = (options, loaderContext) => { ... }","message":"The target module must export a function via module.exports; ES module exports (export default) will not work.","severity":"gotcha","affected_versions":">=2.0.0"},{"fix":"Always return { code: '...' } from the target function.","message":"The return object must include a 'code' property; omitting it will cause an error.","severity":"gotcha","affected_versions":">=2.0.0"},{"fix":"Use path.resolve(__dirname, 'file.js') to provide an absolute path.","message":"The executableFile option requires an absolute path. Using a relative path may resolve incorrectly.","severity":"gotcha","affected_versions":">=3.1.0"}],"env_vars":null,"last_verified":"2026-04-25T00:00:00.000Z","next_check":"2026-07-24T00:00:00.000Z","problems":[{"fix":"Ensure the webpack rule test matches the target file and that use: 'val-loader' is specified.","cause":"The target file is not matching the test pattern in webpack config, or val-loader is not applied.","error":"Error: Module parse failed: Unexpected token (1:0)\nYou may need an appropriate loader to handle this file type, currently no loaders are configured to process this file."},{"fix":"Upgrade to webpack 5 and val-loader v3+.","cause":"Using an outdated version of webpack or the loader context does not support addDependency.","error":"TypeError: loaderContext.addDependency is not a function"},{"fix":"Ensure the target function returns { code: '...' }.","cause":"The target function returned an object without the required 'code' property.","error":"Error: The 'val-loader' loader doesn't return an object with a 'code' property."},{"fix":"Run 'npm install val-loader --save-dev' or equivalent.","cause":"val-loader is not installed as a devDependency.","error":"Module not found: Error: Can't resolve 'val-loader' in ..."}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}