Netlify doesn't install dependencies, build script only works by manually adding `npm install`

I’m running a fairly standard Eleventy site but for some reason it’s started failing to build on Netlify since I’ve started using the markdown-it and markdown-it-attrs packages. It builds fine locally.

They are listed in package.json:

"dependencies": {
  "markdown-it": "^13.0.1",
  "markdown-it-attrs": "^4.1.4",
  "sass": "^1.53.0"
}

But when running my build command, I get the error:

6:50:27 PM: [11ty] 1. Error in your Eleventy config file '/opt/build/repo/.eleventy.js'. You may need to run `npm install`. (via EleventyConfigError)
6:50:27 PM: [11ty] 2. Cannot find module 'markdown-it'

I’ve tried tweaking my eleventy.js config and my build script and the only thing that seems to work is by prepending my build command with npm install.

I thought Netlify automatically ran npm install so I’m unsure why I need to do this. Anyone have any insight?

My build settings:

  • Base directory: _site
  • Build command: npm run build
  • Publish directory: Not set
  • Builds: Active

My build script:

"build": "npx sass style:_site/style && npx @11ty/eleventy"

Build log:

6:07:20 PM: Build ready to start
6:07:22 PM: build-image version: 1ac2360cf8aee6cb2695dd2682b312fefc908a7f (xenial)
6:07:22 PM: build-image tag: v3.15.0
6:07:22 PM: buildbot version: fe6512288e75c8fa5aadaebb51ed1f96e9314fd4
6:07:22 PM: Fetching cached dependencies
6:07:22 PM: Starting to download cache of 73.3MB
6:07:23 PM: Finished downloading cache in 785.515846ms
6:07:23 PM: Starting to extract cache
6:07:23 PM: Finished extracting cache in 464.754139ms
6:07:23 PM: Finished fetching cache in 1.32089547s
6:07:23 PM: Starting to prepare the repo for build
6:07:24 PM: Preparing Git Reference refs/heads/main
6:07:24 PM: Parsing package.json dependencies
6:07:25 PM: Different publish path detected, going to use the one specified in the Netlify configuration file: '_site' versus '' in the Netlify UI
6:07:25 PM: Starting build script
6:07:25 PM: Installing dependencies
6:07:25 PM: Python version set to 2.7
6:07:25 PM: Started restoring cached node version
6:07:26 PM: Finished restoring cached node version
6:07:27 PM: v12.18.0 is already installed.
6:07:27 PM: Now using node v12.18.0 (npm v6.14.4)
6:07:27 PM: Started restoring cached build plugins
6:07:27 PM: Finished restoring cached build plugins
6:07:27 PM: Attempting ruby version 2.7.2, read from environment
6:07:28 PM: Using ruby version 2.7.2
6:07:28 PM: Using PHP version 5.6
6:07:28 PM: Started restoring cached go cache
6:07:28 PM: Finished restoring cached go cache
6:07:28 PM: go version go1.14.4 linux/amd64
6:07:28 PM: go version go1.14.4 linux/amd64
6:07:28 PM: Installing missing commands
6:07:28 PM: Verify run directory
6:07:29 PM: ​
6:07:29 PM: ────────────────────────────────────────────────────────────────
6:07:29 PM:   Netlify Build                                                 
6:07:29 PM: ────────────────────────────────────────────────────────────────
6:07:29 PM: ​
6:07:29 PM: ❯ Version
6:07:29 PM:   @netlify/build 27.9.1
6:07:29 PM: ​
6:07:29 PM: ❯ Flags
6:07:29 PM:   baseRelDir: true
6:07:29 PM:   buildId: 62f142c81d69b800082c8506
6:07:29 PM:   deployId: 62f142c81d69b800082c8508
6:07:29 PM:   systemLogFile: 3
6:07:29 PM: ​
6:07:29 PM: ❯ Current directory
6:07:29 PM:   /opt/build/repo/_site
6:07:29 PM: ​
6:07:29 PM: ❯ Config file
6:07:29 PM:   No config file was defined: using default values.
6:07:29 PM: ​
6:07:29 PM: ❯ Context
6:07:29 PM:   production
6:07:29 PM: ​
6:07:29 PM: ────────────────────────────────────────────────────────────────
6:07:29 PM:   1. Build command from Netlify app                             
6:07:29 PM: ────────────────────────────────────────────────────────────────
6:07:29 PM: ​
6:07:29 PM: $ npm run build
6:07:29 PM: > @ build /opt/build/repo
6:07:29 PM: > npx sass style:_site/style && npx @11ty/eleventy
6:07:31 PM: npx: installed 17 in 1.633s
6:07:46 PM: npx: installed 328 in 15.064s
6:07:47 PM: [11ty] Eleventy CLI Fatal Error: (more in DEBUG output)
6:07:47 PM: [11ty] 1. Error in your Eleventy config file '/opt/build/repo/.eleventy.js'. You may need to run `npm install`. (via EleventyConfigError)
6:07:47 PM: [11ty] 2. Cannot find module 'markdown-it'
6:07:47 PM: [11ty] Require stack:
6:07:47 PM: [11ty] - /opt/build/repo/.eleventy.js
6:07:47 PM: [11ty] - /opt/buildhome/.npm/_npx/1547/lib/node_modules/@11ty/eleventy/src/TemplateConfig.js
6:07:47 PM: [11ty] - /opt/buildhome/.npm/_npx/1547/lib/node_modules/@11ty/eleventy/src/TemplateRender.js
6:07:47 PM: [11ty] - /opt/buildhome/.npm/_npx/1547/lib/node_modules/@11ty/eleventy/src/TemplateData.js
6:07:47 PM: [11ty] - /opt/buildhome/.npm/_npx/1547/lib/node_modules/@11ty/eleventy/src/Eleventy.js
6:07:47 PM: [11ty] - /opt/buildhome/.npm/_npx/1547/lib/node_modules/@11ty/eleventy/cmd.js (via Error)
6:07:47 PM: [11ty]
6:07:47 PM: [11ty] Original error stack trace: Error: Cannot find module 'markdown-it'
6:07:47 PM: [11ty] Require stack:
6:07:47 PM: [11ty] - /opt/build/repo/.eleventy.js
6:07:47 PM: [11ty] - /opt/buildhome/.npm/_npx/1547/lib/node_modules/@11ty/eleventy/src/TemplateConfig.js
6:07:47 PM: [11ty] - /opt/buildhome/.npm/_npx/1547/lib/node_modules/@11ty/eleventy/src/TemplateRender.js
6:07:47 PM: [11ty] - /opt/buildhome/.npm/_npx/1547/lib/node_modules/@11ty/eleventy/src/TemplateData.js
6:07:47 PM: [11ty] - /opt/buildhome/.npm/_npx/1547/lib/node_modules/@11ty/eleventy/src/Eleventy.js
6:07:47 PM: [11ty] - /opt/buildhome/.npm/_npx/1547/lib/node_modules/@11ty/eleventy/cmd.js
6:07:47 PM: [11ty]     at Function.Module._resolveFilename (internal/modules/cjs/loader.js:966:15)
6:07:47 PM: [11ty]     at Function.Module._load (internal/modules/cjs/loader.js:842:27)
6:07:47 PM: [11ty]     at Module.require (internal/modules/cjs/loader.js:1026:19)
6:07:47 PM: [11ty]     at require (internal/modules/cjs/helpers.js:72:18)
6:07:47 PM: [11ty]     at Object.<anonymous> (/opt/build/repo/.eleventy.js:1:20)
6:07:47 PM: [11ty]     at Module._compile (internal/modules/cjs/loader.js:1138:30)
6:07:47 PM: [11ty]     at Object.Module._extensions..js (internal/modules/cjs/loader.js:1158:10)
6:07:47 PM: [11ty]     at Module.load (internal/modules/cjs/loader.js:986:32)
6:07:47 PM: [11ty]     at Function.Module._load (internal/modules/cjs/loader.js:879:14)
6:07:47 PM: [11ty]     at Module.require (internal/modules/cjs/loader.js:1026:19)
6:07:47 PM: npm ERR! code ELIFECYCLE
6:07:47 PM: npm ERR! errno 1
6:07:47 PM: npm ERR! @ build: `npx sass style:_site/style && npx @11ty/eleventy`
6:07:47 PM: npm ERR! Exit status 1
6:07:47 PM: npm ERR!
6:07:47 PM: npm ERR! Failed at the @ build script.
6:07:47 PM: npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
6:07:47 PM: npm WARN Local package.json exists, but node_modules missing, did you mean to install?
6:07:47 PM: npm ERR! A complete log of this run can be found in:
6:07:47 PM: npm ERR!     /opt/buildhome/.npm/_logs/2022-08-08T17_07_47_595Z-debug.log
6:07:47 PM: ​
6:07:47 PM: ────────────────────────────────────────────────────────────────
6:07:47 PM:   "build.command" failed                                        
6:07:47 PM: ────────────────────────────────────────────────────────────────
6:07:47 PM: ​
6:07:47 PM:   Error message
6:07:47 PM:   Command failed with exit code 1: npm run build (https://ntl.fyi/exit-code-1)
6:07:47 PM: ​
6:07:47 PM:   Error location
6:07:47 PM:   In Build command from Netlify app:
6:07:47 PM:   npm run build
6:07:47 PM: ​
6:07:47 PM:   Resolved config
6:07:47 PM:   build:
6:07:47 PM:     base: /opt/build/repo/_site
6:07:47 PM:     command: npm run build
6:07:47 PM:     commandOrigin: ui
6:07:47 PM:     publish: /opt/build/repo/_site
6:07:47 PM:     publishOrigin: default
6:07:48 PM: Caching artifacts
6:07:48 PM: Started saving build plugins
6:07:48 PM: Finished saving build plugins
6:07:48 PM: Started saving pip cache
6:07:48 PM: Finished saving pip cache
6:07:48 PM: Started saving emacs cask dependencies
6:07:48 PM: Finished saving emacs cask dependencies
6:07:48 PM: Started saving maven dependencies
6:07:48 PM: Finished saving maven dependencies
6:07:48 PM: Started saving boot dependencies
6:07:48 PM: Finished saving boot dependencies
6:07:48 PM: Started saving rust rustup cache
6:07:48 PM: Finished saving rust rustup cache
6:07:48 PM: Started saving go dependencies
6:07:48 PM: Finished saving go dependencies
6:07:48 PM: Build failed due to a user error: Build script returned non-zero exit code: 2
6:07:48 PM: Creating deploy upload records
6:07:48 PM: Failing build: Failed to build site
6:07:48 PM: Failed during stage 'building site': Build script returned non-zero exit code: 2 (https://ntl.fyi/exit-code-2)
6:07:48 PM: Finished processing build request in 26.039673378s

Bump, as I’ve tried pulling the build image and debugging on Docker (as detailed in [Support Guide] Debugging Netlify site builds) and still can’t figure this out.

Hi,

I got a similar issue, I got a hint by setting an environment variable in netlify UI
NPM_FLAGS to --verbose

It shows me that I was trying to install via our private repository, so I did update by updating my .npmrc.

Does your package.json and package-lock.json are at the root of your repo or in _site.
If it is at the root of your repo, you should consider using npm workspaces.

Hey there, @codemacabre :wave:

Thanks for reaching out! Can you share your site so that we can look into your specific case further?

Thanks, @hillary. My site is https://codemacabre.com and the repo is at GitHub.

EDIT: I tried setting node version in the environment variables, as I noticed it was using 12.18.0 on Netlify. Now it uses 16.16.0 (the same as I’m using locally), but I’m getting the same issues.

I also tried creating a fresh package.json using npm init and added only the essential dependencies (@11ty/eleventy and sass). That built on Netlify, no problem. As soon as I added markdown-it, it failed again (on Netlify; local is fine), with the same issues as above.

:man_shrugging:t3:

The problem is due to your build cache: Netlify App as compared to Netlify App.

We save the SHA of your package.json in the build cache. If the SHA doesn’t change, we restore Node Modules from cache and skip installing them again. If the SHA doesn’t change, we simply restore the cache.

The problem in your case is that, somehow either your package.json’s SHA is not changing, or we’re not detecting it and thus, the “cached” version of Node Modules doesn’t contain the newly added dependencies.

Could you confirm that if you clear cache and deploy, it works every time?

Hi @hrishikesh, unfortunately I’ve tried using the ‘clear cache and deploy’ option several times (and just tried again) but still get the same error when it attempts to access markdown-it.

Well I’ve fixed it. :tada:

Based on what you suggested, @hrishikesh, that it was a cache issue, I just unlinked my entire repo, then re-linked it. That appears to have solved the problem. No idea why.