Skip to content
Advertisement

Node:fs not found while all code running in node

I was runing a test script for a much larger program running entirely on node. I was testing the ‘rename’ module through npm. Here is my code:

const {
  rename
} = require("node:fs")
rename('./1.txt', './2.txt', )

Does anyone know what I can do to prevent this error:

Error: Cannot find module 'node:fs'
Require stack:
- C:UsersarinbDocumentsGitHubNodeJS-Card-Game-Foundation-with-Discord.js-frontendindex.js
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:880:15)
    at Function.Module._load (internal/modules/cjs/loader.js:725:27)
    at Module.require (internal/modules/cjs/loader.js:952:19)
    at require (internal/modules/cjs/helpers.js:88:18)
    at Object.<anonymous> (C:UsersarinbDocumentsGitHubNodeJS-Card-Game-Foundation-with-Discord.js-frontendindex.js:1:20)
    at Module._compile (internal/modules/cjs/loader.js:1063:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
    at Module.load (internal/modules/cjs/loader.js:928:32)
    at Function.Module._load (internal/modules/cjs/loader.js:769:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [
    'C:\Users\arinb\Documents\GitHub\NodeJS-Card-Game-Foundation-with-Discord.js-frontend\index.js'
  ]
}

Advertisement

Answer

fs(File system) is an internal module in itself, so you can use it by just requiring it:

Method 1:

const fs = require("fs");
fs.rename('./1.txt', './2.txt', )

Method 2: By restructuring the rename function from the fs module

const {rename} = require("fs");
rename('./1.txt', './2.txt', )
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement