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.
import()
link or something given you'd already accepted the answer. :-)