Repository navigation
ReferenceError: require is not defined #33741
Copy link
Copy link
Closed
Labels
errorsIssues and PRs related to JavaScript errors originating in Node.js core.Issues and PRs related to JavaScript errors originating in Node.js core.esmIssues and PRs related to the ECMAScript Modules implementation.Issues and PRs related to the ECMAScript Modules implementation.
Description
- Version:v12.18.0
- Platform:Linux dev 5.3.0-53-generic V8 upgrades and what they mean for versioning #47~18.04.1-Ubuntu SMP Thu May 7 13:10:50 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux
- Subsystem:require
What steps will reproduce the bug?
- Download LTS 12.18.0 from https://nodejs.org/dist/v12.18.0/node-v12.18.0-linux-x64.tar.xz
- Untar compressed file
- Save a js file with the following code: (Let's called it test.js)
console.log(require) - Run //node-v12.18.0-linux-x64/bin/node test.js
- Node returns error output stating that require is not defined.
How often does it reproduce? Is there a required condition?
All the time.
What is the expected behavior?
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] {}
}
What do you see instead?
$ 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
Additional information
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] } } }
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
errorsIssues and PRs related to JavaScript errors originating in Node.js core.Issues and PRs related to JavaScript errors originating in Node.js core.esmIssues and PRs related to the ECMAScript Modules implementation.Issues and PRs related to the ECMAScript Modules implementation.
Activity
ark334 commentedon Oct 10, 2020
Thanks so much, I was having this issue using a type:module in package.json but now it's working thanks to your solution.
priya-concetto commentedon Oct 28, 2020
ReferenceError: createRequire is not defined
benjamingr commentedon Oct 28, 2020
@priya-concetto did you import it from "module" ?
module: clarify CJS global-like variables not defined error message
module: clarify CJS global-like variables not defined error message
module: clarify CJS global-like variables not defined error message
module: clarify CJS global-like variables not defined error message
module: clarify CJS global-like variables not defined error message
reuben4310 commentedon Jun 23, 2021
Not working for me
iva2k commentedon Jul 29, 2021
There's an interesting problem with node checking package.json for
type: modulewhen a package is built with a help of a script which is run by node, e.g.:Then node throws the error "ReferenceError: require is not defined" inside the scripts/increment_version.js. Removing
"type": "module",line removes the error, however, the resulting package is not correct as it needs"type": "module",in package.json.Is there a work-around for that? If not, then please reopen this issue to fix for the normal use of js scripts in package build time.
ljharb commentedon Jul 29, 2021
@iva2k either name the script
.cjs, or better, removetypeentirely and name your ESM files as.mjs.iva2k commentedon Jul 29, 2021
@ljharb - not my package, it is existing github repo I cloned and plan PR to add a feature. Changing whole build setup (which involves CI and CD) and renaming all files in the package is very steep price to pay. Is there a better fix, e.g. I'm pondering to add package.json file without "type: module" to scripts/ dir.
ljharb commentedon Jul 29, 2021
@iva2k that's another option (specifically, you'd need
scripts/package.jsonto have"type": "commonjs"), but in a type:module package, you might as well use.cjsfor your CJS files.iva2k commentedon Jul 29, 2021
It turns out an empty {} package.json in scripts/ solved that particular problem. I added "type: common" to be more specific, and that is clean enough workaround for this situation, works too.
Anyhow, it seems too far-fetched conclusion that node makes based on package.json contents. What is its rationale?
iva2k commentedon Jul 29, 2021
I jumped to conclusion too early that stub package.json works. Now
npm run buildfails in another .js file, now it is failing in node_modules, log snippet:Throwing package.json files around node_modules tree is not an easy workaround.
Also chasing few hundred package maintainers trying to convince them to rename all their .js files sounds like a terrible idea.
ljharb commentedon Jul 29, 2021
typein a package.json only affects that package's boundary. If you have type module, a webpack config probably needs to be named.cjsalso.iva2k commentedon Jul 29, 2021
webpack-cli does not seem to find .cjs config file.
So the summary:
In a "type: module" package should not even try calling any of commonly used cli devDependencies, like webpack-cli - they will all break because node now infers that all .js executed are type: module based on project's package.json.
what a pickle!
ljharb commentedon Jul 29, 2021
unfortunately type module causes nothing but problems ¯\_(ツ)_/¯
iva2k commentedon Jul 29, 2021
so module npm packages are a bad idea
what is the node's changelog that brought this change?
(this problematic project's travis.yml has "node_js: - '11'", and I have global node v12.20.0)
aduh95 commentedon Jul 29, 2021
The rationale was to allow users to write ES modules using
.jsas file extension. In the browser, we can use<script type="module">to tell the JS engine to parse the file as an ES module rather than a script, it was decided that usingpackage.jsonwould be the Node.js equivalent of that.For your particular issue, here's the solutions that could work:
"type": "module"from thepackage.jsonand use the.mjsextension for all ES modules as @ljharb suggested.webpack.config.cjsand create awebpack.config.jsthat simply containsexport { default } from './webpack.config.cjs';webpack.config.jsto an ES module by hand. You might be able to do that by replacingmodule.exportswithexport defaultand adding this snippet at the top of the file:reuben4310 commentedon Jul 29, 2021
benjamingr commentedon Dec 27, 2021
@reuben4310 you are getting the emails because you commented on this post here: #33741 (comment)
@ArashAmini345 please open a new issue in the help repo https://github.com/nodejs/help/issues :)