console.log(require)
All the time.
The behavior should be similar to node executing the same one liner in REFL interactive shell.
$ node
Welcome to Node.js v12.18.0.
Type ".help" for more information.
> console.log(require)
[Function: require] {
resolve: [Function: resolve] { paths: [Function: paths] },
main: undefined,
extensions: [Object: null prototype] {
'.js': [Function],
'.json': [Function],
'.node': [Function]
},
cache: [Object: null prototype] {}
}
$ node test.js
(node:13608) ExperimentalWarning: The ESM module loader is experimental.
file:///somedirectory/test.js:1
console.log(require);
^
ReferenceError: require is not defined
The issue does not happen in older node versions. See text output below
$ node --version
v8.10.0
$ node test.js
{ [Function: require]
resolve: { [Function: resolve] paths: [Function: paths] },
main:
Module {
id: '.',
exports: {},
parent: null,
filename: '/somedirectory/test.js',
loaded: false,
children: [],
paths:
[ '/somedirectory/node_modules',
...,
...,
'/node_modules' ] },
extensions: { '.js': [Function], '.json': [Function], '.node': [Function] },
cache:
{ '/somedirectory/test.js':
Module {
id: '.',
exports: {},
parent: null,
filename: '/somedirectory/test.js',
loaded: false,
children: [],
paths: [Array] } } }
cc @nodejs/modules-active-members
Hey thank you for the report and engaging, this is actually intentional. You cannot require
from an ESM module - you need to use createRequire
:]
import { createRequire } from 'module';
const require = createRequire(import.meta.url);
// can now use `require` in an ESM
See this section in the docs regarding why and how to work around it.
(I'm closing since I don't think this is a bug - as usual, if anyone feels strongly feel free to reopen)
Can you verify that this happens in any directory? By default a .js file shouldn’t be treated as a module. I suspect that you may be in a directory (or nested directory below) where a package.json has a “type” field set to “module”.
BTW: Apologies, I somehow missed that OP did not realize they were running inside module context 🙏 Thanks for the comment Jan.
Let's reopen for now.
@jkrems You are right. The module "type" was causing the issue. Thank you for flagging that out.
We could perhaps expand the error message to include something like “file.js is loaded as an ES module due to /path/to/package.json containing "type": "module"”
We could perhaps expand the error message to include something like “file.js is loaded as an ES module due to /path/to/package.json containing "type": "module"”
Yes. That will be very helpful!
Let's reopen it the meantime for the error message improvement.
I could work on this
Thanks for this thread, was facing the same issue. More precise error message would definitely help people.
Hey thank you for the report and engaging, this is actually intentional. You cannot
require
from an ESM module - you need to usecreateRequire
:]import { createRequire } from 'module'; const require = createRequire(import.meta.url); // can now use `require` in an ESM
See this section in the docs regarding why and how to work around it.
(I'm closing since I don't think this is a bug - as usual, if anyone feels strongly feel free to reopen)
Thanks so much, I was having this issue using a type:module in package.json but now it's working thanks to your solution.
ReferenceError: createRequire is not defined
@priya-concetto did you import it from "module" ?
Most helpful comment
We could perhaps expand the error message to include something like “file.js is loaded as an ES module due to /path/to/package.json containing "type": "module"”