Skip to content

Serverless fails to bundle modules when using pnpm #1953

@noelforte

Description

@noelforte

Describe the bug
I use pnpm to manage my node_modules and have encountered a bug with Eleventy Serverless that occurs when the project is using pnpm, most likely due to the nature of the structure of node_modules being a virtual store that hard links to a global store.

When a serverless bundle is generated from a project that is using pnpm, the resulting bundle fails to execute properly with the error: [11ty] > Cannot find module '.pnpm'. Further inspection of the error shows that within the bundle itself, in eleventy-app-config-modules.js the following statement is made:

require('.pnpm');

which is different than what's placed there when the project is using npm:

require('@11ty/eleventy');
Raw Output from netlify dev
❯ netlify dev
◈ Netlify Dev ◈
◈ Ignored general context env var: LANG (defined in process)
◈ Loaded function dyn.
◈ Functions server is listening on 34855
◈ Starting Netlify Dev with Eleventy
[11ty] Writing output/static/index.html from ./input/static.njk
[11ty] Serverless: 3 files bundled to ./netlify/functions/dyn.
[11ty] Wrote 1 file in 0.11 seconds (v1.0.0-canary.41)
[11ty] Watching…
◈ Reloading function dyn...
◈ Reloaded function dyn
[Browsersync] Access URLs:
 -----------------------------------
    Local: http://localhost:8080
 External: http://172.30.86.161:8080
 -----------------------------------
[Browsersync] Serving files from: output
◈ Reloading function dyn...
◈ Reloaded function dyn
[11ty] Unhandled rejection in promise: (more in DEBUG output)
[11ty] > Cannot find module '.pnpm'
Require stack:
- /home/noel/projects/eleventy-serverless/netlify/functions/dyn/eleventy-app-config-modules.js
- /home/noel/projects/eleventy-serverless/netlify/functions/dyn/eleventy-bundler-modules.js
- /home/noel/projects/eleventy-serverless/netlify/functions/dyn/index.js
- /home/noel/projects/eleventy-serverless/node_modules/.pnpm/@11ty+eleventy@1.0.0-canary.41/node_modules/@11ty/eleventy/src/Plugins/ServerlessBundlerPlugin.js
- /home/noel/projects/eleventy-serverless/node_modules/.pnpm/@11ty+eleventy@1.0.0-canary.41/node_modules/@11ty/eleventy/src/Eleventy.js
- /home/noel/projects/eleventy-serverless/node_modules/.pnpm/@11ty+eleventy@1.0.0-canary.41/node_modules/@11ty/eleventy/cmd.js

`Error` was thrown:
[11ty]     Error: Cannot find module '.pnpm'
    Require stack:
    - /home/noel/projects/eleventy-serverless/netlify/functions/dyn/eleventy-app-config-modules.js
    - /home/noel/projects/eleventy-serverless/netlify/functions/dyn/eleventy-bundler-modules.js
    - /home/noel/projects/eleventy-serverless/netlify/functions/dyn/index.js
    - /home/noel/projects/eleventy-serverless/node_modules/.pnpm/@11ty+eleventy@1.0.0-canary.41/node_modules/@11ty/eleventy/src/Plugins/ServerlessBundlerPlugin.js
    - /home/noel/projects/eleventy-serverless/node_modules/.pnpm/@11ty+eleventy@1.0.0-canary.41/node_modules/@11ty/eleventy/src/Eleventy.js
    - /home/noel/projects/eleventy-serverless/node_modules/.pnpm/@11ty+eleventy@1.0.0-canary.41/node_modules/@11ty/eleventy/cmd.js
        at Function.Module._resolveFilename (internal/modules/cjs/loader.js:885:15)
        at Function.Module._load (internal/modules/cjs/loader.js:730:27)
        at Module.require (internal/modules/cjs/loader.js:957:19)
        at require (internal/modules/cjs/helpers.js:88:18)
        at Object.<anonymous> (/home/noel/projects/eleventy-serverless/netlify/functions/dyn/eleventy-app-config-modules.js:1:1)
        at Module._compile (internal/modules/cjs/loader.js:1068:30)
        at Object.Module._extensions..js (internal/modules/cjs/loader.js:1097:10)
        at Module.load (internal/modules/cjs/loader.js:933:32)
        at Function.Module._load (internal/modules/cjs/loader.js:774:14)
        at Module.require (internal/modules/cjs/loader.js:957:19)

To Reproduce
Steps to reproduce the behavior:

  1. Install pnpm: $ npm i -g pnpm
  2. Move to your project: $ cd <project>
  3. Follow steps at: https://www.11ty.dev/docs/plugins/serverless/#usage
  4. Bundle serverless function by running eleventy

Expected behavior
When running with npm/npx the resulting bundle can be previewed (ie with $ netlify dev) or deployed without issues or errors by including correct dependencies (in this case looks to be missing require('@11ty/eleventy')

Environment:

  • OS and Version: Ubuntu 20.04.2 LTS (Focal Fossa) using kernel 5.4.72-microsoft-standard-WSL2
  • Eleventy Version: v1.0.0-canary.41

Additional context
My next step for trying to tackle this bug is to look into the workarounds documented here: https://pnpm.io/faq#pnpm-does-not-work-with-your-project-here

I'm no Node expert by any means, but this appears to be a direct result of modules not resolving correctly due to a different-than-expected node_modules structure.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions