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?
Where?
Repo Steps
Given the following tree:
The following code will produce different behavior when executed as a main module, required module, or from the REPL
Tests
1. As main module:
2. As stdin input:
3. As REPL commands:
This is the surprising behavior
4. As REPL but indirectly loaded
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
requireto behave the same no matter from where it is being executed. Why is that the REPL behaves differently?