Skip to content
Discussion options

You must be logged in to vote

If it's a JSON file, you can import it directly, but you should do it from a *.server file so the file doesn't get bundled with the client.

However, if you need to load a regular file, here's a pattern that should work.

// app/data/getData.server.ts
import * as fs from "fs";

let data: string;

declare global {
  var __data__: string;
}

// store data on global in development mode
// to survive purge of require cache
if (process.env.NODE_ENV === "production") {
  data = readData();
} else {
  if (!global.__data__) {
    global.__data__ = readData();
  }
  data = global.__data__;
}

function readData() {
  let contents = fs.readFileSync(`${process.cwd()}/app/data/data.json`, "utf8");
  return

Replies: 1 comment 3 replies

Comment options

You must be logged in to vote
3 replies
@meglio
Comment options

@meglio
Comment options

@meglio
Comment options

Answer selected by meglio
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants