Internal error during "Functions bundling"

Error message
11:03:07 PM: Error: EACCES: permission denied, scandir ‘/root’
11:03:07 PM: ​
11:03:07 PM: Error location
11:03:07 PM: During Functions bundling
11:03:07 PM: ​
11:03:07 PM: Error properties
11:03:07 PM: { errno: -13, code: ‘EACCES’, syscall: ‘scandir’, path: ‘/root’ }
11:03:07 PM: ​
11:03:07 PM: Resolved config
11:03:07 PM: build:
11:03:07 PM: command: npm install hexo-generator-feed --save;npm install --save hexo-blog-encrypt;npm run build
11:03:07 PM: commandOrigin: config
11:03:07 PM: environment:
11:03:07 PM: - NETLIFY_EMAILS_DIRECTORY
11:03:07 PM: - NETLIFY_EMAILS_MAILGUN_DOMAIN
11:03:07 PM: - NETLIFY_EMAILS_PROVIDER
11:03:07 PM: - NETLIFY_EMAILS_PROVIDER_API_KEY
11:03:07 PM: - NETLIFY_EMAILS_SECRET
11:03:07 PM: - NETLIFY_GRAPH_TOKEN
11:03:07 PM: - ONEGRAPH_AUTHLIFY_TOKEN
11:03:07 PM: - NODE_VERSION
11:03:07 PM: publish: /opt/build/repo/public
11:03:07 PM: publishOrigin: config
11:03:07 PM: functions:
11:03:07 PM: emails:
11:03:07 PM: included_files:
11:03:08 PM: - /**
plugins:
- inputs: {}
origin: ui

  package: '@netlify/plugin-emails'

This is the log, please help me, my github repository xiaohao8.github.io/_config.fluid.yml at main · xiaohao8/xiaohao8.github.io · GitHub

Hey @xiaohao,

This has been escalated to the devs.

Hi there, thanks for raising this! Could you confirm what your email directory configuration setting is please?

I’m having the same issue. Has there been any solution?

8:57:13 PM: Netlify Build
8:57:13 PM: ────────────────────────────────────────────────────────────────
8:57:13 PM: ​
8:57:13 PM: ❯ Version
8:57:13 PM: @netlify/build 29.31.1
8:57:13 PM: ​
8:57:13 PM: ❯ Flags
8:57:13 PM: baseRelDir: true
8:57:13 PM: buildId: 658bae86a3f3030008482219
8:57:13 PM: deployId: 658bae86a3f303000848221b
8:57:13 PM: ​
8:57:13 PM: ❯ Current directory
8:57:13 PM: /opt/build/repo
8:57:13 PM: ​
8:57:13 PM: ❯ Config file
8:57:13 PM: /opt/build/repo/netlify.toml
8:57:13 PM: ​
8:57:13 PM: ❯ Context
8:57:13 PM: production
8:57:13 PM: ​
8:57:13 PM: build.command from netlify.toml
8:57:13 PM: ────────────────────────────────────────────────────────────────
8:57:13 PM: ​
8:57:13 PM: $ npm install -g npm@latest && npm install
8:57:23 PM: removed 11 packages, and changed 44 packages in 10s
8:57:23 PM: 28 packages are looking for funding
8:57:23 PM: run npm fund for details
8:57:27 PM: changed 1 package, and audited 2269 packages in 4s
8:57:27 PM: 218 packages are looking for funding
8:57:27 PM: run npm fund for details
8:57:27 PM: 8 vulnerabilities (3 moderate, 5 high)
8:57:27 PM: To address all issues, run:
8:57:27 PM: npm audit fix
8:57:27 PM: Run npm audit for details.
8:57:27 PM: ​
8:57:27 PM: (build.command completed in 14.2s)
8:57:27 PM: ​
8:57:27 PM: Functions bundling
8:57:27 PM: ────────────────────────────────────────────────────────────────
8:57:27 PM: ​
8:57:27 PM: Packaging Functions from functions directory:
8:57:27 PM: - api.js
8:57:27 PM: ​
8:57:27 PM: ​
8:57:27 PM: Internal error during “Functions bundling”
8:57:27 PM: ────────────────────────────────────────────────────────────────
8:57:27 PM: ​
8:57:27 PM: Error message
8:57:27 PM: Error: EACCES: permission denied, scandir “/root”
8:57:27 PM: ​
8:57:27 PM: Error location
8:57:27 PM: During Functions bundling
8:57:27 PM: ​
8:57:27 PM: Error properties
8:57:27 PM: { errno: -13, code: “EACCES”, syscall: “scandir”, path: “/root” }
8:57:27 PM: ​
8:57:27 PM: Resolved config
8:57:27 PM: build:
8:57:27 PM: command: npm install
8:57:27 PM: commandOrigin: config
8:57:27 PM: environment:
8:57:27 PM: - CI
8:57:27 PM: - DATABASE_URL
8:57:27 PM: - REG_TOKEN
8:57:27 PM: - SESSION_SECRET
8:57:27 PM: publish: /opt/build/repo
8:57:27 PM: publishOrigin: ui
8:57:27 PM: functions:
8:57:27 PM: "":
8:57:27 PM: included_files:
8:57:28 PM: Failed during stage “building site”: Build script returned non-zero exit code: 4
8:57:28 PM: - views/**
- public/css/**
- public/downloads/**
- models/**
- /**.js
node_bundler: esbuild
functionsDirectory: /opt/build/repo/functions
redirects:
- force: true
from: /

status: 200
to: /.netlify/functions/api/:splat
redirectsOrigin: config
8:57:28 PM: Build failed due to an internal system error: Build script returned non-zero exit code: 4
8:57:28 PM: Failing build: Failed to build site
8:57:28 PM: Finished processing build request in 46.658s

Can you answer the question that has been asked above?

I’m not sure what is meant by email directory configuration. If you are referring to the netlify.toml configuration file here it is.

**This issue only started happening recently. It was fine 3 weeks ago.
[build]
functions = “functions/”

[functions]
included_files = [“views/", "public/css/”, “public/downloads/", "models/”, “/**.js”]
external_node_modules = [“express”]
node_bundler = “esbuild”

[[redirects]]
from=“/*”
to= “/.netlify/functions/api/:splat”
status = 200
force = true

[dev]

Doesn’t look like you’re using the emails plugin at all. Could you share your function’s code?

I’m not using any plugins. That’s why I am confused as to why I am getting this error. My function code is quite simple. As I said before, this is a recent issue. I have changed nothing about my function code or .toml file in the last few months.

if (process.env.NODE_ENV !== ‘production’) {
require(‘dotenv’).config()
}

const session = require(‘express-session’)
const express = require(‘express’)
const serverless = require(“serverless-http”)
const flash = require(‘express-flash’)
const router = express.Router()
const app = express()
const bcrypt = require(‘bcrypt’)
const passport = require(‘passport’)
const methodOverride = require(‘method-override’)
const MongoClient = require(‘mongodb’).MongoClient
const MongoStore = require(‘connect-mongo’)
const alert = require(‘alert’)
const ejs = require(‘ejs’)
const crypto = require(‘crypto’);
const fs = require(‘fs’);
const path = require(‘path’);

const client = new MongoClient(process.env.DATABASE_URL, { useNewUrlParser: true, useUnifiedTopology: true })

app.use(express.static(‘public’))
app.set(‘view-engine’, ‘ejs’)
app.engine(‘ejs’, ejs.__express)
app.use(session({
secret: process.env.SESSION_SECRET,
resave: false,
saveUninitialized: false,
cookie: {
maxAge: 60 * 60 * 1000,
sameSite: ‘strict’,
},
rolling: true,
store: MongoStore.create({
mongoUrl: process.env.DATABASE_URL,
autoRemove: ‘native’,
})
}))
app.use(passport.initialize())
app.use(passport.session())
app.use(flash())
app.use(methodOverride(‘_method’))
app.use(express.urlencoded({ extended: false }))
app.use(flash())

const mongoose = require(‘mongoose’)
const IdCounter = require(‘…/models/idcounter’)
const User = require(‘…/models/user’)
const regToken = require(‘…/models/regToken’)

mongoose.connect(process.env.DATABASE_URL, {useUnifiedTopology: true, useNewUrlParser: true,})
const db = mongoose.connection
db.on(‘error’, error => console.error(error))
db.once(‘open’, async () => {
console.log(‘Connected to Mongoose’)

const counter = await IdCounter.findOne()
if (!counter) {
const newCounter = new IdCounter({ counter: 0 })
await newCounter.save()
}

})

client.connect(err => {
if (err) {
console.error(‘Error connecting to MongoDB:’, err)
return
}

console.log(“Database connected”)

})
const collection = client.db(“mydatabase”).collection(“userdatas”)
const initializePassport = require(‘…/passport-config’)
initializePassport(passport, uid => collection.findOne({ uid: uid }), id => collection.findOne({id}))

router.get(‘/404’, (req, res) => {
res.render(‘404.ejs’)
})

router.get(‘/’, (req, res) => {
console.log(req.headers.range); // Check if range header is present
res.render(‘index.ejs’)
})

router.get(‘/external’, checkAuthenticated, (req, res) => {
res.render(‘external.ejs’)
})

router.get(‘/register’, checkNotAuthenticated, (req, res) => {
res.render(‘register.ejs’)
})

router.get(‘/login’, checkNotAuthenticated, (req, res) => {
res.render(‘login.ejs’)
})

router.get(‘/route1’, (req, res) =>{
res.render(‘jitter.ejs’)
})

router.get(‘/route2’, (req, res) =>{
res.render(‘rewasd.ejs’)
})

router.get(‘/route3’, (req, res) =>{
res.render(‘valorant.ejs’)
})

const filesDirectory = ‘./public/downloads’;

const getFileStream = (fileName) => {
const filePath = path.join(filesDirectory, fileName);

if (fs.existsSync(filePath)) {
return fs.createReadStream(filePath);
} else {
return null;
}
};

// Route for downloading a specific file
router.get(‘/download/:fileName’, checkAuthenticated, (req, res) => {
const { fileName } = req.params;
const fileStream = getFileStream(fileName);

if (!fileStream) {
return res.status(404).json({ message: ‘File not found’ });
}

// Set headers for the download
const stat = fs.statSync(path.join(filesDirectory, fileName));
const fileSize = stat.size;

res.setHeader(‘Content-Type’, ‘application/octet-stream’);
res.setHeader(‘Content-Disposition’, attachment; filename="${fileName}");
res.setHeader(‘Content-Length’, fileSize);

// Stream the file to the response
fileStream.pipe(res);
});

app.post(‘/login’, checkNotAuthenticated, passport.authenticate(‘local’, {
successRedirect: ‘/external’,
failureRedirect: ‘/login’,
failureFlash: true
}))

app.post(‘/register’, checkNotAuthenticated, async (req, res) => {
try {
const hashedPassword = await bcrypt.hash(req.body.password, 10)

const counter = await IdCounter.findOne()
counter.counter += 1
await counter.save()

const newUser = new User({
  id: counter.counter,
  name: req.body.name,
  uid: req.body.uid,
  password: hashedPassword,
})

const token = req.body.regToken

// Check if the provided token exists and is not used
const existingToken = await regToken.findOne({ token })

if (!existingToken) {
  return res.render('register', { error: 'Incorrect regToken' })
}

if (existingToken.isUsed) {
  return res.render('register', { error: 'Token has already been used' })
}

// Mark the token as used
existingToken.isUsed = true
await existingToken.save()
await newUser.save()

const newToken = await generateAndStoreToken()
res.redirect('/login')

} catch {
res.redirect(‘/register’)
}
})

app.delete(‘/logout’, (req, res) => {
req.logOut()
res.redirect(‘/login’)
})

function checkAuthenticated(req, res, next) {
console.log(“Session:”, req.session)
if (req.isAuthenticated()) {
return next()
}
console.log(“user not authenticated”)
res.redirect(‘/login’)
}

function checkNotAuthenticated(req, res, next) {
if (req.isAuthenticated() && req.session.cookie.expires > new Date()) {
return res.redirect(‘/external’)
}
next()
}

app.use(/, router)

module.exports.handler = serverless(app)

process.on(‘SIGTERM’, () => {
mongoose.disconnect(() => {
console.log(‘Disconnected from Mongoose’)
client.close(() => {
console.log(‘Disconnected from MongoClient’)
server.close(() => {
console.log(‘Server closed’)
})
})
})
})

Can you try linking your repo to a new site? And for the build command, use echo Hi! instead of npm i npm. Maybe that would make some difference.

Tried both. Same issue.

Netlify Build
1:20:47 PM: ────────────────────────────────────────────────────────────────
1:20:47 PM: ​
1:20:47 PM: ❯ Version
1:20:47 PM: @netlify/build 29.31.1
1:20:47 PM: ​
1:20:47 PM: ❯ Flags
1:20:47 PM: baseRelDir: true
1:20:47 PM: buildId: 6591ccf55e2c054690fdff66
1:20:47 PM: deployId: 6591ccf55e2c054690fdff68
1:20:47 PM: ​
1:20:47 PM: ❯ Current directory
1:20:47 PM: /opt/build/repo
1:20:47 PM: ​
1:20:47 PM: ❯ Config file
1:20:47 PM: /opt/build/repo/netlify.toml
1:20:47 PM: ​
1:20:47 PM: ❯ Context
1:20:47 PM: production
1:20:47 PM: ​
1:20:47 PM: Build command from Netlify app
1:20:47 PM: ────────────────────────────────────────────────────────────────
1:20:47 PM: ​
1:20:47 PM: $ echo Hi!
1:20:47 PM: Hi!
1:20:47 PM: ​
1:20:47 PM: (build.command completed in 6ms)
1:20:47 PM: ​
1:20:47 PM: Functions bundling
1:20:47 PM: ────────────────────────────────────────────────────────────────
1:20:47 PM: ​
1:20:47 PM: Packaging Functions from functions directory:
1:20:47 PM: - api.js
1:20:47 PM: ​
1:20:47 PM: ​
1:20:47 PM: Internal error during “Functions bundling”
1:20:47 PM: ────────────────────────────────────────────────────────────────
1:20:47 PM: ​
1:20:47 PM: Error message
1:20:47 PM: Error: EACCES: permission denied, scandir “/root”
1:20:47 PM: ​
1:20:47 PM: Error location
1:20:47 PM: During Functions bundling
1:20:47 PM: ​
1:20:47 PM: Error properties
1:20:47 PM: { errno: -13, code: “EACCES”, syscall: “scandir”, path: “/root” }
1:20:47 PM: ​
1:20:47 PM: Resolved config
1:20:47 PM: build:
1:20:47 PM: base: /opt/build/repo
1:20:47 PM: command: echo Hi!
1:20:47 PM: commandOrigin: ui
1:20:47 PM: environment:
1:20:47 PM: - CI
1:20:47 PM: - DATABASE_URL
1:20:47 PM: - SESSION_SECRET
1:20:47 PM: publish: /opt/build/repo
1:20:47 PM: publishOrigin: ui
1:20:47 PM: functions:
1:20:47 PM: "":
1:20:47 PM: external_node_modules:
1:20:47 PM: - express
1:20:47 PM: included_files:
1:20:48 PM: Failed during stage “building site”: Build script returned non-zero exit code: 4
1:20:48 PM: - views/**
- public/css/**
- public/downloads/**
- models/**
- /**.js
node_bundler: esbuild
functionsDirectory: /opt/build/repo/functions
redirects:
- force: true
from: /

status: 200
to: /.netlify/functions/api/:splat
redirectsOrigin: config
1:20:48 PM: Build failed due to an internal system error: Build script returned non-zero exit code: 4
1:20:48 PM: Failing build: Failed to build site
1:20:48 PM: Finished processing build request in 16.739s

hi are you still having issues with your build?

Yes. I’ve tried everything posted on the forums and nothing seems to work.

Can you share your repo?

https://github.com/Ryan2k05/test

Here is the repo without the .env file.

Prefixing all your included_files with ./ worked:

included_files = ["./views/**", "./public/css/**", "./public/downloads/**", "./models/**"]

(note: I have removed /**.js as I don’t think you wish to bundle literally every .js file even from the node_modules folder, but if that’s intended, please add it back in a similar manner)

This fixed all my issues. Thanks

Hi :wave:t6: glad to hear you got this issue fixed.