{"id":43577,"library":"pg-require-sql","title":"pg-require-sql","description":"A Node.js module that loads SQL files from disk and exports them as parameterized query functions for use with the `pg` (node-postgres) library. v1.0.0, stable but no recent updates. Key differentiator: auto-generates named query functions from .sql files, reducing boilerplate when embedding SQL in Node.js applications. Not actively maintained; ESM imported with .js extension.","status":"maintenance","version":"1.0.0","language":"javascript","source_language":"en","source_url":"https://github.com/lanetix/node-pg-require-sql","tags":["javascript","pg","sql","require","postgres"],"install":[{"cmd":"npm install pg-require-sql","lang":"bash","label":"npm"},{"cmd":"yarn add pg-require-sql","lang":"bash","label":"yarn"},{"cmd":"pnpm add pg-require-sql","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Peer dependency: exports functions that take a pg client/pool instance","package":"pg","optional":false}],"imports":[{"note":"Package is ESM-only since v1.0.0; top-level default export only","wrong":"const loadQueries = require('pg-require-sql');","symbol":"default (named import from module)","correct":"import loadQueries from 'pg-require-sql';"},{"note":"Returns object with SQL filenames (camelCased) as keys bound to a pg client","symbol":"loadQueries function","correct":"const queries = loadQueries(pathToDir, pgClient);"},{"note":"CommonJS require() does not apply; package has no named exports","wrong":"const { loadQueries } = require('pg-require-sql');","symbol":"require() usage","correct":"import loadQueries from 'pg-require-sql';"}],"quickstart":{"code":"import loadQueries from 'pg-require-sql';\nimport pg from 'pg';\nimport path from 'path';\n\nconst pool = new pg.Pool({\n  connectionString: process.env.DATABASE_URL ?? 'postgres://localhost/myapp'\n});\n\nconst sqlDir = path.resolve('./sql');\nconst queries = loadQueries(sqlDir, pool);\n\n// Assumes ./sql/users.sql exists with: SELECT * FROM users WHERE id = $1\nconst { users } = queries;\nconst result = await users('123');\nconsole.log(result.rows);","lang":"typescript","description":"Shows how to load SQL files from a directory, bind them to a pg Pool, and execute a named query."},"warnings":[{"fix":"Ensure SQL filenames are lowercase with hyphens or underscores; avoid special characters.","message":"SQL file names are converted to camelCase: 'getUser.sql' becomes queries.getUser.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Call the function, e.g., `queries.getUser(id)`, not `client.query(...)`.","message":"The function returned for each SQL file expects you to pass query parameters (not client). It returns a standard pg result.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Use `path.resolve()` to build absolute path.","message":"The path to the SQL directory must be relative to the current working directory or absolute.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Use `path.resolve('./sql')` for cross-platform compatibility.","message":"On Windows, directory separators may cause issues; use forward slashes or `path.join`.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"search_vec":"'activ':56 'applic':54 'auto':39 'auto-gener':38 'boilerpl':48 'differenti':37 'disk':13 'embed':50 'esm':58 'export':15 'extens':62 'file':11,46 'function':20,43 'generat':40 'import':59 'javascript':63 'js':61 'key':36 'librari':29 'load':9 'maintain':57 'modul':7 'name':41 'node':27 'node-postgr':26 'node.js':6,53 'parameter':18 'pg':2,25,64 'pg-require-sql':1 'postgr':28,67 'queri':19,42 'recent':34 'reduc':47 'requir':3,66 'sql':4,10,45,51,65 'stabl':31 'updat':35 'use':22 'v1.0.0':30","created_at":"2026-06-05T17:00:27.742345+00:00","updated_at":"2026-06-05T17:00:27.742345+00:00","problems":[{"fix":"Run `npm install pg-require-sql` and ensure package.json includes 'type': 'module' or use .mjs extension.","cause":"Node.js cannot locate the module; likely not installed or using wrong resolution.","error":"Cannot find module 'pg-require-sql'"},{"fix":"Use `import loadQueries from 'pg-require-sql'` and set 'type': 'module' in package.json.","cause":"Package is ESM-only; using CommonJS require() fails.","error":"require() of ES Module not supported"},{"fix":"Ensure you pass a `pg.Pool` instance (e.g., `new pg.Pool(...)`) as the second argument.","cause":"Passed a pool incorrectly or used a client instead of a pool.","error":"TypeError: pool.query is not a function"}],"ecosystem":"npm","meta_description":null,"install_score":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":null,"cli_name":null,"cli_version":null,"type":"library","homepage":"https://github.com/lanetix/node-pg-require-sql","github":"https://github.com/lanetix/node-pg-require-sql","docs":null,"changelog":null,"pypi":null,"npm":"pg-require-sql","openapi_spec":null,"status_page":null,"smithery":null,"categories":["database"],"base_url":null,"auth_type":null,"provenance":{"verified_status":null,"verified_at":null,"last_verified":"2026-06-05","next_check":"2026-09-03","install_tag":null}}