Node: require does not work as described in the documentation

Created on 10 Feb 2018  路  7Comments  路  Source: nodejs/node

  • v9.5.0:
  • Windows 10 Pro 64-bit:
  • modules:


I have this file structure:
image

  • app.js
    console.log( require.resolve('./hello') );

  • package.json
    { "main": "test" }

other files are empty.

run: node app.js

if rely on the documentation require should find file underlined in the picture below:
image
hello.js

but it finds this:
image
test.js

It should find file in step 3.a but it finds in step 3.b
image

confirmed-bug

Most helpful comment

@richardlau racy :D

It is handled as semver-major because it was wrong for a very long time and there is a chance that code relies on the faulty behavior. So it will only be released in v.10.0.0.

All 7 comments

I guess this how to interpret the module loading logic - my annotations in grey:

Input:
X=./hello
Y=/require_test

  1. If X begins with './' or '/' or '../'
    [TRUE]
    a. LOAD_AS_FILE(Y + X)
    ['/require_test' + './hello'] = ['./require_test/hello']
    b. LOAD_AS_DIRECTORY(Y + X)
  2. LOAD_NODE_MODULES(X, dirname(Y))
  3. THROW "not found"

LOAD_AS_FILE(X) [X='./require_test/hello']
[FAILS AS X is a folder now]

  1. If X is a file, load X as JavaScript text. STOP
  2. If X.js is a file, load X.js as JavaScript text. STOP
  3. If X.json is a file, parse X.json to a JavaScript Object. STOP
  4. If X.node is a file, load X.node as binary addon. STOP

LOAD_AS_DIRECTORY(X)
X=['./require_test/hello']

  1. If X/package.json is a file,
    [TRUE]
    a. Parse X/package.json, and look for "main" field.
    [TRUE], test
    b. let M = X + (json main field)
    ./require_test/hello/test
    c. LOAD_AS_FILE(M)
    DONE!

It is working as per the documentation. Closing as answered, let me know if you think otherwise.

I think this is addressed by https://github.com/nodejs/node/pull/15015?

@smkoyan this was indeed a bug and it was fixed recently. It was fixed recently by https://github.com/nodejs/node/pull/15015.

@richardlau racy :D

It is handled as semver-major because it was wrong for a very long time and there is a chance that code relies on the faulty behavior. So it will only be released in v.10.0.0.

@BridgeAR / @richardlau - can you please clarify for my understanding? - I don't see any deviation from the doc, nor I see a doc update as part of #15015!

@gireeshpunathil the issue is this part of your annotations:

LOAD_AS_FILE(X) [X='./require_test/hello']
[FAILS AS X is a folder now]

This describes the incorrect behaviour, it should not fail here and X should be tried as a file. https://github.com/nodejs/node/pull/15015 fixes the behaviour so that it matches the doc.

ah! ok, thanks @richardlau for the clarification.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

stevenvachon picture stevenvachon  路  3Comments

danialkhansari picture danialkhansari  路  3Comments

willnwhite picture willnwhite  路  3Comments

loretoparisi picture loretoparisi  路  3Comments

cong88 picture cong88  路  3Comments