I have Problem with finding a json file

Hello Everyone, I just want to share my problem maybe someone could help, I’m new on netlify, I deployed my first function using node.js on netlify, the project work well on my local machine by netlify-lambda serve, but when I deployed it, I couldn’t read a json file loacated on ./src/all/channels.json file, I spent a lot of time and effort looking for solation but until now I found nothing I almost give up.

Thank you in advance, here’s my netlify domain: app.netlify.com

Hi @9orsaan :wave:

How are you reading the JSON file in your function? If you are using readFileSync or other fs method you will need to use the included_files configuration option in the project’s netlify.toml. E.g.

[functions]
  included_files = ["all/channels.json"]

Check out this thread for more information on usage We don't understand how the included_files [functions] parameter works

If you are using require or import, then this file is bundled with the function at build time, and there is no need for the above configuration. E.g.

const testJSON = require('../data/test.json')

exports.handler = async(event) => {

  return {
    statusCode: 200,
    body: JSON.stringify(testJSON),
    headers: {
      'Content-Type': 'application/json',
    }
  }

}
1 Like

Hi @coelmay Thank you so much for your help, I’ll try it I hope to work for me

Hi @coelmay This Method it’s work but not 100% I included the file path in .toml file as the first example but I still couldn’t read from the file, what kind of work is importing the file as the second example that you share with me but I still couldn’t read from the json file directly I could only get it’s content through the testJSON variable

Can you share the code for the function? Can you share the repository you are working from?

This’s the netlify.toml

[build]

functions = "functions"
included_files = ["all/channels.json"]

This’s the package.json

{
  "name": "tv-pro",
  "version": "1.0.0",
  "description": "",
  "main": "api.js",
  "scripts": {
    "start": "./node_modules/.bin/netlify-lambda serve src",
    "build":"./node_modules/.bin/netlify-lambda build src"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "dotenv": "^16.0.2",
    "express": "^4.18.1",
    "netlify-lambda": "^2.0.15",
    "serverless-http": "^3.0.2"
  }
}

This’s the code snippet

require('dotenv').config();
const fs = require('fs');
const _path = require("path");
const express = require('express');
const serverless = require('serverless-http');
const { json } = require('body-parser');

const testJSON = require('../all/channels.json')

exports.handler = async(event) => {

  return {
    statusCode: 200,
    body: JSON.stringify(testJSON),
    headers: {
      'Content-Type': 'application/json',
    }
  }
}
const app = express();
app.use(express.json());
const router = express.Router();

router.get('/data',(req,res) =>{

    // I tried '../all/channels.json' and 'all/channels.json' as a Path but still get undefine
   //  The testJSON variable returns the data that I want, but jsonData return undefine
    // the 'all' folder located in the project root
    fs.readFile('./all/channels.json', 'utf-8', (err, jsonData) => {
        if(data != null){
            res.send(jsonData);
        }else{
            res.send(testJSON);
        }
        
    });
});

app.use('/.netlify/functions/api', router);
module.exports.handler = serverless(app);

It should be:

[functions]
  directory = "functions"
  included_files = ["all/channels.json"]

Also,

That’s redundant. Netlify builds the functions automatically, no need to use outdated tools like netlify-lambda.

1 Like

Hi @hrishikesh Thank you for support, I tried to change in the .toml file as you suggested but unfortunately It won’t to build anymore It gives an error also when I tried to deploy it, the deployment failed and return :

We couldn’t deploy your site. Check out our Build docs for tips on troubleshooting your build, or ask us for debugging advice.

11:52:22 AM: Error message
11:52:22 AM: Command failed with exit code 1: npm run build (Search results for '"non-zero exit code: 1"' - Netlify Support Forums)
11:52:22 AM: ​
11:52:22 AM: Error location
11:52:22 AM: In Build command from Netlify app:
11:52:22 AM: npm run build

My New Update of netlify.toml file:

[functions]
  directory = "functions"
  included_files = ["all/channels.json"]

It only build and deployed successfully with the old .toml code

[build]

  functions = "functions"
  included_files = ["./all/channels.json"]

Well, can’t comment much without seeing your site. You’ve shared an irrelevant part of your deploy log, which doesn’t help much and lack of any site address in this thread is a blocker.

1 Like

Here’s my website: https://pro-tv.netlify.app/
I have an empty index.htm on it, I’ll be glad to share anything would help solving this issue.
and This’s the whole Deploy log:

11:52:07 AM: Build ready to start
11:52:08 AM: build-image version: d7b3dbfb0846505993c9a131894d1858074c90b4 (focal)
11:52:08 AM: build-image tag: v4.10.1
11:52:08 AM: buildbot version: 37262c48b1f3cfa88c6caed707d29b19aef6a5a7
11:52:08 AM: Fetching cached dependencies
11:52:08 AM: Starting to download cache of 115.3MB
11:52:09 AM: Finished downloading cache in 1.151917165s
11:52:09 AM: Starting to extract cache
11:52:10 AM: Finished extracting cache in 824.861672ms
11:52:10 AM: Finished fetching cache in 2.037069768s
11:52:10 AM: Starting to prepare the repo for build
11:52:11 AM: Preparing Git Reference refs/heads/main
11:52:12 AM: Parsing package.json dependencies
11:52:13 AM: Starting build script
11:52:13 AM: Installing dependencies
11:52:13 AM: Python version set to 2.7
11:52:13 AM: Started restoring cached node version
11:52:13 AM: Finished restoring cached node version
11:52:14 AM: v16.17.0 is already installed.
11:52:14 AM: Now using node v16.17.0 (npm v8.15.0)
11:52:14 AM: Started restoring cached build plugins
11:52:14 AM: Finished restoring cached build plugins
11:52:14 AM: Attempting ruby version 2.7.2, read from environment
11:52:15 AM: Using ruby version 2.7.2
11:52:15 AM: Using PHP version 8.0
11:52:15 AM: No npm workspaces detected
11:52:15 AM: Started restoring cached node modules
11:52:15 AM: Finished restoring cached node modules
11:52:16 AM: Started restoring cached go cache
11:52:16 AM: Finished restoring cached go cache
11:52:16 AM: Installing Go version 1.17 (requested 1.17)
11:52:21 AM: unset GOOS;
11:52:21 AM: unset GOARCH;
11:52:21 AM: export GOROOT='/opt/buildhome/.gimme/versions/go1.17.linux.amd64';
11:52:21 AM: export PATH="/opt/buildhome/.gimme/versions/go1.17.linux.amd64/bin:${PATH}";
11:52:21 AM: go version >&2;
11:52:21 AM: export GIMME_ENV="/opt/buildhome/.gimme/env/go1.17.linux.amd64.env"
11:52:21 AM: go version go1.17 linux/amd64
11:52:21 AM: Installing missing commands
11:52:21 AM: Verify run directory
11:52:22 AM: ​
11:52:22 AM: ────────────────────────────────────────────────────────────────
11:52:22 AM:   Netlify Build                                                 
11:52:22 AM: ────────────────────────────────────────────────────────────────
11:52:22 AM: ​
11:52:22 AM: ❯ Version
11:52:22 AM:   @netlify/build 27.17.1
11:52:22 AM: ​
11:52:22 AM: ❯ Flags
11:52:22 AM:   baseRelDir: true
11:52:22 AM:   buildId: 631dbdd74c3cfa000c563e72
11:52:22 AM:   deployId: 631dbdd74c3cfa000c563e74
11:52:22 AM: ​
11:52:22 AM: ❯ Current directory
11:52:22 AM:   /opt/build/repo
11:52:22 AM: ​
11:52:22 AM: ❯ Config file
11:52:22 AM:   /opt/build/repo/netlify.toml
11:52:22 AM: ​
11:52:22 AM: ❯ Context
11:52:22 AM:   production
11:52:22 AM: ​
11:52:22 AM: ────────────────────────────────────────────────────────────────
11:52:22 AM:   1. Build command from Netlify app                             
11:52:22 AM: ────────────────────────────────────────────────────────────────
11:52:22 AM: ​
11:52:22 AM: $ npm run build
11:52:22 AM: > tv-pro@1.0.0 build
11:52:22 AM: > ./node_modules/.bin/netlify-lambda build src
11:52:22 AM: netlify-lambda: Building functions
11:52:22 AM: TypeError: Cannot read properties of undefined (reading 'environment')
11:52:22 AM:     at Object.exports.loadContext (/opt/build/repo/node_modules/netlify-lambda/lib/config.js:37:30)
11:52:22 AM:     at webpackConfig (/opt/build/repo/node_modules/netlify-lambda/lib/build.js:87:24)
11:52:22 AM:     at async Object.exports.run (/opt/build/repo/node_modules/netlify-lambda/lib/build.js:208:18)
11:52:22 AM: ​
11:52:22 AM: ────────────────────────────────────────────────────────────────
11:52:22 AM:   "build.command" failed                                        
11:52:22 AM: ────────────────────────────────────────────────────────────────
11:52:22 AM: ​
11:52:22 AM:   Error message
11:52:22 AM:   Command failed with exit code 1: npm run build (https://ntl.fyi/exit-code-1)
11:52:22 AM: ​
11:52:22 AM:   Error location
11:52:22 AM:   In Build command from Netlify app:
11:52:22 AM:   npm run build
11:52:22 AM: ​
11:52:22 AM:   Resolved config
11:52:22 AM:   build:
11:52:22 AM:     command: npm run build
11:52:22 AM:     commandOrigin: ui
11:52:22 AM:     environment:
11:52:22 AM:       - ACCESS_TOKEN
11:52:22 AM:       - NETLIFY_GRAPH_TOKEN
11:52:22 AM:       - ONEGRAPH_AUTHLIFY_TOKEN
11:52:22 AM:     publish: /opt/build/repo/dist
11:52:22 AM:     publishOrigin: ui
11:52:22 AM:   functions:
11:52:22 AM:     '*':
11:52:22 AM:       included_files:
11:52:22 AM:         - all/channels.json
11:52:22 AM:   functionsDirectory: /opt/build/repo/functions
11:52:23 AM: Caching artifacts
11:52:23 AM: Started saving node modules
11:52:23 AM: Finished saving node modules
11:52:23 AM: Started saving build plugins
11:52:23 AM: Finished saving build plugins
11:52:23 AM: Started saving pip cache
11:52:23 AM: Finished saving pip cache
11:52:23 AM: Started saving emacs cask dependencies
11:52:23 AM: Finished saving emacs cask dependencies
11:52:23 AM: Started saving maven dependencies
11:52:23 AM: Finished saving maven dependencies
11:52:23 AM: Started saving boot dependencies
11:52:23 AM: Finished saving boot dependencies
11:52:23 AM: Started saving rust rustup cache
11:52:23 AM: Finished saving rust rustup cache
11:52:23 AM: Started saving go dependencies
11:52:23 AM: Finished saving go dependencies
11:52:23 AM: Build failed due to a user error: Build script returned non-zero exit code: 2
11:52:23 AM: Creating deploy upload records
11:52:23 AM: Failing build: Failed to build site
11:52:23 AM: Failed during stage 'building site': Build script returned non-zero exit code: 2 (https://ntl.fyi/exit-code-2)
11:52:24 AM: Finished processing build request in 15.455598943s

The build you’ve shared failed exactly because of what I asked you to remove:

That tool should no longer be used.

So, I have to remove the scripts part and leave it empty?

Yeah, you can remove the scripts and change your build command to an empty string. Functions would be bundled automatically by Netlify.

I’ll try it and let you know the result, just a question how suppose to run/build the project locally if I would test it before deploy it?

Using CLI:

1 Like

Should I leave the script like this?

"scripts": {
    "start": ""
  }

because I did and it failed to deploy

it failed, because you did not do this

To clarify, I meant, build command in Netlify

I’m newbie at Netlify platform I don’t know what build command in Netlify mean, if you give me an example I’ll be Thankful, it’ll be much easier for me

It’s the command that’s set here: Netlify App

1 Like

It deployed successfully, but when I try to go to a route it gives me

Page Not Found