Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
eridal opened this issue Dec 5, 2019 · 2 comments
Closed

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

eridal opened this issue Dec 5, 2019 · 2 comments
Labels
confirmed-bug Issues with confirmed bugs. repl Issues and PRs related to the REPL subsystem.

Comments

@eridal
Copy link

eridal commented Dec 5, 2019

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?

@addaleax addaleax added the repl Issues and PRs related to the REPL subsystem. label Dec 5, 2019
@addaleax addaleax changed the title Surprising require('./file') behavior when ran from cli Surprising require('./file') behavior when ran from REPL Dec 5, 2019
@addaleax
Copy link
Member

addaleax commented Dec 5, 2019

@eridal I would consider this a bug as well.

(I’ve modified the title since “cli” would more typically refer to node --print .../node --eval ... rather than the REPL interface.)

@nodejs/repl

@BridgeAR BridgeAR added the confirmed-bug Issues with confirmed bugs. label Dec 5, 2019
@ZYSzys
Copy link
Member

ZYSzys commented Dec 7, 2019

I think #30835 is able to fix this.

@ZYSzys ZYSzys closed this as completed in 7629fb2 Dec 10, 2019
targos pushed a commit that referenced this issue Dec 10, 2019
Fixes: #30808

PR-URL: #30835
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Guy Bedford <guybedford@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
targos pushed a commit that referenced this issue Jan 14, 2020
Fixes: #30808

PR-URL: #30835
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Guy Bedford <guybedford@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
BethGriggs pushed a commit that referenced this issue Feb 6, 2020
Fixes: #30808

PR-URL: #30835
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Guy Bedford <guybedford@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
confirmed-bug Issues with confirmed bugs. repl Issues and PRs related to the REPL subsystem.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants