Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

require() not working in module type nodejs script

In my package.json file I've specified that my nodejs app is of type module, because if I do not do that, it seems that I can not use import statements. This is how it looks like now:

{
  "name": "...",
  "version": "1.0.0",
  "description": "....",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "repository": {
    "type": "git",
    "url": "...."
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "lodash": "^4.17.15"
  },
  "type": "module"
}

But if I add the "type": "module" to my package.json file, I can't use require statements anymore, because I get a ReferenceError: require is not defined error.

If I remove the "type": "module" line from package.json, and rewrite all of my imports to requires, everything works without an error.

I can't seem to find any indication, that import and require can not be mixed or used together in the same script, am I missing something here, or I am having some other bug? How could I resolve to use these two kind of statements in the same script?

Why I would need this, is because I want to require some config files based on dynamic paths, and only if the files exists, which I think I can not do with import.

DISCLAIMER: I am rather new to nodejs server side programming, so it is possible that I am approaching this situation very wrongly, if that's the case, please advice me something, based on the Why I've mentioned above.

NOTE: I am running this node script from the server terminal, and not from the browser.

like image 308
Adam Baranyai Avatar asked Dec 22 '19 10:12

Adam Baranyai


People also ask

Why is require not working in js?

This usually happens because your JavaScript environment doesn't understand how to handle the call to require() function you defined in your code. Here are some known causes for this error: Using require() in a browser without RequireJS. Using require() in Node.

For what require () is used in NodeJS?

In NodeJS, require() is a built-in function to include external modules that exist in separate files. require() statement basically reads a JavaScript file, executes it, and then proceeds to return the export object.

How do you fix require is not defined in NodeJS?

It happens when you declare your package type as module in your package. json . If you do this, certain CommonJS variables can't be used, including require . To fix this, remove "type": "module" from your package.


1 Answers

But if I add the "type": "module" to my package.json file, I can't use require statements anymore, because I get a ReferenceError: require is not defined error.

Right. It's either/or. Either you use ESM (JavaScript modules, type = "module") or you use CJS (CommonJS-like Node.js native modules, require).

But, if you're using type="module":

  1. You can still use CJS modules, you just import them via import instead of require (or via import() [dynamic import] if necessary). See details here and here.

  2. You can use createRequire to effectively get a require function you can use in your ESM module, which brings us to...

Why I would need this, is because I want to require some config files based on dynamic paths, and only if the files exists, which I think I can not do with import.

That's right. You have to use createRequire for that instead (or readFile and JSON.parse), more here.

createRequire version:

import { createRequire } from "module";
const require = createRequire(import.meta.url);
const yourData = require("./your.json");
like image 50
T.J. Crowder Avatar answered Sep 22 '22 10:09

T.J. Crowder