Support config.includeFiles for @now/next
See original GitHub issueThere’s a message/thread in Spectrum about this.
Basically I can’t fs.readFileSync()
inside an pages/api/whatever.ts
because the files aren’t copied.
I’ve done some investigation in the code for the builders, and I don’t see any parity for the functionality that exists in @now/node. I imagine something should be added around here for that, and am happy to try a PR if you don’t think this is too crazy.
Here’s the info I posted in Spectrum:
I’m having this same issue with @now/next
, so I have this in a now.json
file:
{
"version": 2,
"builds": [
{
"src": "next.config.js",
"use": "@now/next",
"config": {
"includeFiles": ["email-templates/**"]
}
}
]
}
Where a directory with this structure exists in the root of the app/directory:
email-templates
├── login
│ ├── html.pug
│ └── subject.pug
├── main.pug
└── mixins.pug
And I can’t read the files at all (I’ve tried all kinds of combinations to just find the files, and here’s the latest):
const templateFilePath = path.join(__dirname, 'email-templates', 'login', 'html.pug');
const templateContent = fs.readFileSync(templateFilePath).toString('utf8');
The error I get is:
Unhandled Promise Rejection {"errorType":"Runtime.UnhandledPromiseRejection","errorMessage":"Error: ENOENT: no such file or directory, open '/email-templates/login/html.pug'","stack":["Runtime.UnhandledPromiseRejection: Error: ENOENT: no such file or directory, open '/email-templates/login/html.pug'"," at process.on (/var/runtime/index.js:37:15)"," at process.emit (events.js:203:15)"," at emitPromiseRejectionWarnings (internal/process/promises.js:119:20)"," at process._tickCallback (internal/process/next_tick.js:69:34)"],"reason":{"errorType":"Error","errorMessage":"ENOENT: no such file or directory, open '/email-templates/login/html.pug'","code":"ENOENT","stack":["Error: ENOENT: no such file or directory, open '/email-templates/login/html.pug'"," at Object.openSync (fs.js:443:3)"," at Object.readFileSync (fs.js:343:35)"," at Object.sendLoginEmail (/var/task/.next/serverless/pages/api/v0/signup.js:564:71)"," at module.exports.L76w.__webpack_exports__.default (/var/task/.next/serverless/pages/api/v0/signup.js:471:67)"," at process._tickCallback (internal/process/next_tick.js:68:7)"],"errno":-2,"syscall":"open","path":"/email-templates/login/html.pug"},"promise":{}}
2019-09-26T14:35:19.168Z bea5a417-7224-4397-b4ec-81b6838540a2 ERROR Unhandled rejection: { Error: ENOENT: no such file or directory, open '/email-templates/login/html.pug'
at Object.openSync (fs.js:443:3)
at Object.readFileSync (fs.js:343:35)
at Object.sendLoginEmail (/var/task/.next/serverless/pages/api/v0/signup.js:564:71)
at module.exports.L76w.__webpack_exports__.default (/var/task/.next/serverless/pages/api/v0/signup.js:471:67)
at process._tickCallback (internal/process/next_tick.js:68:7)
errno: -2,
syscall: 'open',
code: 'ENOENT',
path: '/email-templates/login/html.pug' }
And while the path looks weird (I’d expect __dirname
to be /var/task
, not /
), it also doesn’t work if I try to read /var/task/email-templates/login/html.pug
.
Thanks!
Issue Analytics
- State:
- Created 4 years ago
- Reactions:4
- Comments:11 (5 by maintainers)
Thanks! If I run into these issues again in the future I’ll add more info.
I just realized
require('./file.json')
works fine, so we just need to generate a json file at build time:package.json
:"build": "node script.js>data.json && next build"
require('../../data.json')
In my case it was even simpler. The output was static so I just wrote it directly into
/public
.Hope this helps!