Skip to content

Surprising require('./file') behavior when ran from REPL #30808

Closed
@eridal

Description

@eridal

Where?

  • Version: v10.15.3, v12.13.1, v13.3.0
  • Platform: x86_64 GNU/Linux
  • Subsystem: require

Repo Steps

Given the following tree:

mkdir testrequire
cd testrequire

mkdir node_modules
echo 'module.exports = `yes`' > node_modules/foo.js

The following code will produce different behavior when executed as a main module, required module, or from the REPL

require('./foo') // this should always throw an error, but works on the REPL

Tests

1. As main module:

↪ echo 'require(`./foo`)' > main.js; node main.js
internal/modules/cjs/loader.js:800
    throw err;
    ^

Error: Cannot find module './foo'

2. As stdin input:

↪ echo 'require(`./foo`)' | node
internal/modules/cjs/loader.js:800
    throw err;
    ^

Error: Cannot find module './foo'

3. As REPL commands:

This is the surprising behavior

↪ node
Welcome to Node.js v12.13.1.
Type ".help" for more information.
> require('./foo')
'yes'

4. As REPL but indirectly loaded

↪ echo 'require(`./foo`)' > main.js
↪ node
Welcome to Node.js v12.13.1.
Type ".help" for more information.
> require('./main')
Thrown:
Error: Cannot find module './foo'

Is this a bug?

I'm not sure if this is a bug or not, but it is quite surprising to me.

I'd expect require to behave the same no matter from where it is being executed. Why is that the REPL behaves differently?

Metadata

Metadata

Assignees

No one assigned

    Labels

    confirmed-bugIssues with confirmed bugs.replIssues and PRs related to the REPL subsystem.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions