-
-
Notifications
You must be signed in to change notification settings - Fork 87
Description
Detail Bug Report
Summary
- Context: The
template.jsfile is the core module of@browserless/functionthat analyzes user-provided code to detect if it uses the Puppeteer page API, using AST parsing to determine whether to inject browser connection logic. - Bug: The file requires
acornandacorn-walkat the module level, but these packages are listed asdevDependenciesinstead ofdependenciesinpackage.json. - Actual vs. expected: The packages should be listed under
dependenciesso they are installed in production environments, but they are only indevDependencieswhich means they won't be installed when users install@browserless/function. - Impact: Any production installation of
@browserless/functionwill crash at runtime whentemplate.jsis loaded, making the package completely unusable.
Code with Bug
In packages/function/src/template.js:
const walk = require('acorn-walk') // <-- BUG 🔴 Required at runtime but not declared as a dependency
const acorn = require('acorn') // <-- BUG 🔴 Required at runtime but not declared as a dependencyIn packages/function/package.json:
"devDependencies": {
"@browserless/test": "^10.11.1",
"acorn": "~8.16.0", // <-- BUG 🔴 Runtime dependency incorrectly listed as devDependency
"acorn-walk": "~8.3.5", // <-- BUG 🔴 Runtime dependency incorrectly listed as devDependency
"ava": "5",
"lodash": "latest"
}Explanation
packages/function/src/template.js loads acorn and acorn-walk at module initialization. Because packages/function/package.json lists them only under devDependencies, they are not guaranteed to be installed for consumers in production installs, leading to Cannot find module 'acorn-walk'/'acorn' at runtime.
The issue has been masked by a transitive dependency: the declared dependency isolated-function currently depends on acorn/acorn-walk, so Node may resolve them from isolated-function's node_modules. This is an accidental, fragile coupling; if isolated-function changes its dependencies, @browserless/function will break.
Codebase Inconsistency
The published package confirms the mismatch:
$ npm view @browserless/function dependencies
{
'@browserless/errors': '^10.11.1',
'isolated-function': '~0.1.49',
'require-one-of': '~1.0.24'
}acorn and acorn-walk are absent from the package’s declared dependencies despite being required at runtime.
Recommended Fix
Move acorn and acorn-walk from devDependencies to dependencies in packages/function/package.json so they are installed for consumers.
History
This bug was introduced in commit d45585f. The commit added a performance optimization to parse user code with AST analysis (using acorn/acorn-walk) to detect whether the page object is used, avoiding unnecessary Puppeteer setup when not needed. When adding the new template.js file with these runtime dependencies, the acorn packages were mistakenly added to devDependencies instead of dependencies in package.json.