getStaticProps and node apis
January 7, 2020 at 10:46amThe Next.js community has a new home. This thread is preserved for historical purposes. The content of this conversation may be innaccurrate or out of date. Go to new community home →
getStaticProps and node apis
January 7, 2020 at 10:46amHi, v quick question: is it possible to use node apis like
fs
and child_process
inside getStaticProps? I've been using preval
until now for one-time generated local/filesystem stuff, but it would be nice to make explicit use of the framework's features.Thanks!
January 7, 2020 at 1:02pm
Great, thank you. So any errors I get around "can't resolve module
child_process
" are an issue with my code...It looks like there are problems running node things in dev mode, while they work fine when built and served. Is there a recommended approach for this?
January 8, 2020 at 10:44am
I've just had a go at building a standalone test case and had more trouble than before! Unfortunately, I'm behind a corporate firewall which means I can't push a repo to GH or anything useful, but I've set up the absolute minimum next install from scratch (
npm i next react react-dom isomorphic-unfetch
) then created pages/index.js
as listed here.Oh, enter posts the message. Code to follow:
pages/index.js
import React from 'react';import fetch from 'isomorphic-unfetch';import getRecentCommits from '../src/git';export async function getStaticProps() {const res = await fetch('https://api.github.com/repos/zeit/next.js');const json = await res.json();const commits = getRecentCommits();console.log(json, commits);return {props: {stars: json.stargazers_count,commits,},};}function HomePage({ stars }) {return <div>Next stars: {stars}</div>;}export default HomePage;
src/git.js contains a little
spawn
util that runs git log
in a child process and returns the stdout. It's a good analogue of what I was trying to do in the main projectThing is, getStaticProps doesn't seem to get called in either dev mode or when built, and including the import of
src/git
(which in turn imports child_process
) throws module not found on both dev and build. Without that git import, next build
outputs the home page as static, not SSG, according to the console.I must have missed something really simple and obvious, but I'm a bit thrown. V grateful for any suggestions! (next is installed at 9.1.7)
build log for reference:
> next buildCreating an optimized production buildFailed to compile../src/git.jsModule not found: Can't resolve 'child_process' in '/next-test/src'
and build log without the local
git
import:> next buildCreating an optimized production buildCompiled successfully.Automatically optimizing pagesPage Size─ ○ / 505 B+ shared by all 68.4 kB├ chunks/commons.js 62.6 kB├ runtime/main.js 5 kB└ runtime/webpack.js 746 Bλ (Server) server-side renders at runtime (uses getInitialProps or getServerProps)○ (Static) automatically rendered as static HTML (uses no initial props)● (SSG) automatically generated as static HTML + JSON (uses getStaticProps)
ah,
getStaticProps
doesn't exist yet. If you want to test the experimental feature, you need to rename that function to unstable_getStaticProps
.i.e.
export function unstable_getStaticProps() { ... }
Just realized you linked to the non
unstable
variant of this function in our docs. Do note, docs prefixed with _
are hidden / unstable! You can find the latest version on https://nextjs.org!January 9, 2020 at 9:23am
I have now made
unstable_getStaticProps
work perfectly with a whole set of node apis - fantastic stuff. Of course, now I have a new question, which I'll raise separately. Thanks for sorting me out.