Local Build Plugin not npm installing by default, resulting in MODULE_NOT_FOUND

Hey y’all!

I have been working through the Build Plugins docs and trying to get my local build plugin to build correctly.

In the plugin, I have a dependency on an npm module (axios). When I “npm install” directly, it will install the plugin, and everything runs just fine. If I remove the node_modules directory, and run “netlify build” to simulate a cloud build, it doesn’t seem to run npm install and install the necessary dependencies. Thus I get the below.

My versions:
netlify-cli/6.10.4 darwin-x64 node-v12.19.1

Should I be including a node_modules with my local build plugin?

Thanks, y’all!
Andy

────────────────────────────────────────────────────────────────
Netlify Build
────────────────────────────────────────────────────────────────

❯ Version
@netlify/build 18.13.10

❯ Flags
dry: false
offline: false

❯ Current directory
/Users/andy/src/github.com/waviisoft/my-poc

❯ Config file
/Users/andy/src/github.com/waviisoft/my-poc/netlify.toml

❯ Context
production

❯ Loading plugins

  • /netlify-plugins/my-plugin@0.1.0 from netlify.toml

────────────────────────────────────────────────────────────────────
Plugin “/netlify-plugins/my-plugin” internal error
────────────────────────────────────────────────────────────────────

Error message
Error: Could not import plugin:
Cannot find module ‘axios’
Require stack:

  • /Users/andy/src/github.com/waviisoft/my-poc/netlify-plugins/my-plugin/src/index.js
  • /Users/andy/.nvm/versions/node/v12.19.1/lib/node_modules/netlify-cli/node_modules/@netlify/build/src/plugins/child/logic.js
  • /Users/andy/.nvm/versions/node/v12.19.1/lib/node_modules/netlify-cli/node_modules/@netlify/build/src/plugins/child/load.js
  • /Users/andy/.nvm/versions/node/v12.19.1/lib/node_modules/netlify-cli/node_modules/@netlify/build/src/plugins/child/main.js

Plugin details
Package: /netlify-plugins/my-plugin
Version: 0.1.0

Error location
While loading “/netlify-plugins/my-plugin” from netlify.toml
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:831:15)
at Function.Module._load (internal/modules/cjs/loader.js:687:27)
at Module.require (internal/modules/cjs/loader.js:903:19)
at require (internal/modules/cjs/helpers.js:74:18)
at Object. (/Users/andy/src/github.com/waviisoft/my-poc/netlify-plugins/my-plugin/src/index.js:11:15)
at Module._compile (internal/modules/cjs/loader.js:1015:30)
at Object.Module._extensions…js (internal/modules/cjs/loader.js:1035:10)
at Module.load (internal/modules/cjs/loader.js:879:32)
at Function.Module._load (internal/modules/cjs/loader.js:724:14)
at Module.require (internal/modules/cjs/loader.js:903:19)

Error properties
{ code: ‘MODULE_NOT_FOUND’ }

Resolved config
build:
publish: /Users/andy/src/github.com/waviisoft/my-poc
publishOrigin: default
plugins:
- inputs: {}
origin: config
package: /netlify-plugins/my-plugin

Hi @andy-waviisoft,

Local plugins’ dependencies must be installed with npm install. In production, this is done automatically using your top-level package.json. However, Netlify CLI works differently and npm install must be manually run instead there.

As a side note, this is not specific to local Build plugins. For example, if your build command has dependencies, npm install will also be run automatically in production, but needs to be run manually with Netlify CLI.

The reason is that, in production, Netlify can be sure that npm install should be run once. However, with Netlify CLI on a local machine, you might want to run netlify build several times in a row. In that instance, running npm install before each local build might be unnecessary and much slower.

Please let us know if this helps.