Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js command line with folder name as parameter

Tags:

node.js

I was looking at the https://github.com/mars/heroku-cra-node github repo and I noticed that in the package.json, there is a script defined as "start": node server (server being the name of the folder containing the index.js file).

What exactly does this line do? When we pass a folder name as parameter does node search inside of this folder for the file index.js?

like image 411
Observablerxjs Avatar asked Feb 24 '26 16:02

Observablerxjs


1 Answers

Node will do a limited search when an exact file can't be found.

In the case of supplying server as the "script" argument to node:

First node will try to open the file named server.
If not found then it will look for server.js, server.json then server.node
If those don't exist and the parameter is a directory, node will attempt to open and server/index.js, server/index.json then server/index.node.

First match wins.

$ strace -e stat node server
syscall_332(0xffffff9c, 0x7ffeca72d028, 0, 0xfff, 0x7ffeca72cc10, 0x7ffeca72cd20) = -1 (errno 38)
stat("/server", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0
stat("/server.js", 0x7ffeca72cc90)      = -1 ENOENT (No such file or directory)
stat("/server.json", 0x7ffeca72cc90)    = -1 ENOENT (No such file or directory)
stat("/server.node", 0x7ffeca72cc90)    = -1 ENOENT (No such file or directory)
stat("/server/index.js", 0x7ffeca72cbe0) = -1 ENOENT (No such file or directory)
stat("/server/index.json", 0x7ffeca72cbe0) = -1 ENOENT (No such file or directory)
stat("/server/index.node", 0x7ffeca72cbe0) = -1 ENOENT (No such file or directory)
stat("/server", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0
stat("/server.js", 0x7ffeca72cad0)      = -1 ENOENT (No such file or directory)
stat("/server.json", 0x7ffeca72cad0)    = -1 ENOENT (No such file or directory)
stat("/server.node", 0x7ffeca72cad0)    = -1 ENOENT (No such file or directory)
stat("/server/index.js", 0x7ffeca72ca20) = -1 ENOENT (No such file or directory)
stat("/server/index.json", 0x7ffeca72ca20) = -1 ENOENT (No such file or directory)
stat("/server/index.node", 0x7ffeca72ca20) = -1 ENOENT (No such file or directory)
internal/modules/cjs/loader.js:896
  throw err;
  ^

Error: Cannot find module '/server'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:893:15)
    at Function.Module._load (internal/modules/cjs/loader.js:743:27)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)
    at internal/main/run_main_module.js:17:47 {
  code: 'MODULE_NOT_FOUND',
  requireStack: []
}
+++ exited with 1 +++
like image 69
Matt Avatar answered Feb 26 '26 07:02

Matt