Support for .cjs in plugins

Hi all :wave: I am trying to write a Netlify plugin for my svelte-kit app following this blog post: How to Contextualize your env vars with Netlify Build Plugins

Netlify specifically looks for an index.js for their plugins as shown in this subreddit answer:

When we create a brand new svelte-kit app, the package.json has type: module specified. Because of that, running netlify build to try the build locally fails because:

Could not import plugin:
  ReferenceError: module is not defined in ES module scope
  This file is being treated as an ES module because it has a '.js' file extension and '/Users/website/package.json' contains "type": "module". To treat it as a CommonJS script, rename it to use the '.cjs' file extension.

But, if I change the extension to .cjs, Netlify doesn’t find the plugin as it looks specifically for index.js as mentioned before.

Has anyone had this problem? And how have you dealt with it?

I have pushed a demo version of my problem in this repo:

Thanks for your help!

Hi @chengjo0,

Would adding the full path to the plugin (including the file extension) work?

[[plugins]]
  package = './netlify/plugins/netlify-plugin-env-var-by-context/index.cjs'

Another possibility could be to either:

  • Use ES modules instead of CommonJS with your local plugin
  • Remove "type": "module" from package.json. Looking at your repository, it seems to me the rest of the codebase might still work.
2 Likes

Thank you @ehmicky , adding the full path to the plugin did the trick :smile:

2 Likes