2

i want to run my script "script.js" on node.js use command "node script.js" but i got this error

  node:internal/modules/cjs/loader:936
  throw err;
  ^
    Error: Cannot find module 'C:\Users\User\script.js'
    [90m    at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)[39m
    [90m    at Function.Module._load (node:internal/modules/cjs/loader:778:27)[39m
    [90m    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:79:12)[39m
    [90m    at node:internal/main/run_main_module:17:47[39m {
      code: [32m'MODULE_NOT_FOUND'[39m,
      requireStack: []
    }

can anyone help ?

isfanafely
  • 23
  • 1
  • 5
  • Is the location where the script.js file is located 'C:\Users\User\script.js'? – ddon Sep 02 '21 at 05:58
  • Please refer to this document. : https://stackoverflow.com/questions/9023672/how-do-i-resolve-cannot-find-module-error-using-node-js – ddon Sep 02 '21 at 06:03
  • yes it is, been trying to move it to other partition or creating new folder not work either – isfanafely Sep 02 '21 at 06:33

1 Answers1

0

Please ensure that you are running the node command in the same folder that the script.js file exist. If you are opening command prompt from a different location and try running node script.js, an error will be thrown as

internal/modules/cjs/loader.js:883 throw err; ^

Error: Cannot find module 'C:\yourpath\script.js' [90m at Function.Module._resolveFilename (internal/modules/cjs/loader.js:880:15)[39m [90m at Function.Module._load (internal/modules/cjs/loader.js:725:27)[39m [90m at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)[39m [90m at internal/main/run_main_module.js:17:47[39m { code: [32m'MODULE_NOT_FOUND'[39m, requireStack: [] }

This indicates that the file doesnot exist in the desired path.

As per your question, you are running the command from C:\Users\User, so your script.js file must be there at the location C:\Users\User\script.js. If your script.js is at some other location, say C:\Users\yourusername\somerandomfolderpath, then you have to open your command prompt at that folder or go to that location using command prompt using these commads.

C:
cd C:\Users\yourusername\somerandomfolderpath

The first command takes you to the required drive (expecting it to be C drive here). The second command takes to the required folder in that drive.

After this you can run your script using.

node script.js
Nitheesh
  • 19,238
  • 3
  • 22
  • 49
  • i run them from the same folder either "node script.js" or "node C:\Users\User\script.js" not work. been trying reinstall through cmd and windows program not work either. – isfanafely Sep 02 '21 at 06:29
  • @isfanafely ist not the issue with windows. Its just the issue with our usage of command. – Nitheesh Sep 02 '21 at 06:33
  • i saw thread the ubuntu user solve this issu by reinstall it so i tried them – isfanafely Sep 02 '21 at 06:35