0

It is my understanding that the core modules, like "fs" are part of the node.js build, and no special configuration is needed to make them available for importing, so I'm at a loss as to how "fs" could be missing when running on render.com. I have no problems building or running in development mode locally. And the service deploys and builds perfectly on render.com, but running fails with:

Jan 31 01:13:22 PM  ==> Starting service with 'node index.js'
Jan 31 01:13:23 PM  internal/modules/cjs/loader.js:888
Jan 31 01:13:23 PM    throw err;
Jan 31 01:13:23 PM    ^
Jan 31 01:13:23 PM  
Jan 31 01:13:23 PM  **Error: Cannot find module 'node:fs'**
Jan 31 01:13:23 PM  Require stack:
Jan 31 01:13:23 PM  - /opt/render/project/src/node_modules/@fastify/send/lib/SendStream.js
Jan 31 01:13:23 PM  - /opt/render/project/src/node_modules/@fastify/send/index.js
Jan 31 01:13:23 PM  - /opt/render/project/src/node_modules/@fastify/static/index.js
Jan 31 01:13:23 PM  - /opt/render/project/src/index.js
Jan 31 01:13:23 PM      at Function.Module._resolveFilename (internal/modules/cjs/loader.js:885:15)
Jan 31 01:13:23 PM      at Function.Module._load (internal/modules/cjs/loader.js:730:27)
Jan 31 01:13:23 PM      at Module.require (internal/modules/cjs/loader.js:957:19)
Jan 31 01:13:23 PM      at require (internal/modules/cjs/helpers.js:88:18)
Jan 31 01:13:23 PM      at Object.<anonymous> (/opt/render/project/src/node_modules/@fastify/send/lib/SendStream.js:10:12)
Jan 31 01:13:23 PM      at Module._compile (internal/modules/cjs/loader.js:1068:30)
Jan 31 01:13:23 PM      at Object.Module._extensions..js (internal/modules/cjs/loader.js:1097:10)
Jan 31 01:13:23 PM      at Module.load (internal/modules/cjs/loader.js:933:32)
Jan 31 01:13:23 PM      at Function.Module._load (internal/modules/cjs/loader.js:774:14)
Jan 31 01:13:23 PM      at Module.require (internal/modules/cjs/loader.js:957:19) {
Jan 31 01:13:23 PM    code: 'MODULE_NOT_FOUND',
Jan 31 01:13:23 PM    requireStack: [
Jan 31 01:13:23 PM      '/opt/render/project/src/node_modules/@fastify/send/lib/SendStream.js',
Jan 31 01:13:23 PM      '/opt/render/project/src/node_modules/@fastify/send/index.js',
Jan 31 01:13:23 PM      '/opt/render/project/src/node_modules/@fastify/static/index.js',
Jan 31 01:13:23 PM      '/opt/render/project/src/index.js'
Jan 31 01:13:23 PM    ]
Jan 31 01:13:23 PM  }

The service was running merrily along until I deployed a new version today that requires the @fastify/static package, like this:

fastify.register(require('@fastify/static'), { root: path.join(__dirname,'public'), prefix:'/public/' })

I never import "fs" directly, but @fastify/static apparently does, like this:

const statSync = require('fs').statSync

I tried importing fs explicitly before importing @fastify, but the error doesn't change. Webpack is not involved. I've tried building using both npm and yarn, no difference - not that building should affect core modules. Is there some critical environmental setup I have neglected to do on Render.com?

3
  • try adding const fs = require('fs'); Commented Jan 31, 2023 at 21:51
  • Thank you, but as I mentioned, I tried importing fs explicitly before importing @fastify, but the error doesn't change Commented Jan 31, 2023 at 21:59
  • try To use the promise-based APIs: import * as fs from 'node:fs/promises'; To use the callback and sync APIs: import * as fs from 'node:fs'; Commented Jan 31, 2023 at 22:10

1 Answer 1

2

Ronnie's comment (thks!) made me start wondering if my assumption that "node:fs" was the same as "fs" was correct. It was not. Render.com defaults to node.js version 14.17.0. The "node:" module reference syntax was not added until a later version.

The solution was to request node version 16.0.0 on render.com using an environment variable containing the version string, and it fixed the error.

The details for specifying the version on render.com is at https://render.com/docs/node-version

An explanation of the core modules syntax is at https://nodejs.org/api/modules.html

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.