I'm doing a small Node.js script, where I wanted to use:
in the same file. But I'm struggling on how to import both libraries at the same time.
If my type package.json
type is commonJs
:
- I get
SyntaxError: Cannot use import statement outside a module
when trying to importimport { loadJsonFile } from "load-json-file"
- I get
Error [ERR_REQUIRE_ESM]: require() of ES Module D:\Dev\my-project\node_modules\load-json-file\index.js from D:\Dev\fsvl-date-check\index.js not supported. Instead change the require of D:\Dev\my-project\node_modules\load-json-file\index.js in D:\Dev\my-project\index.js to a dynamic import() which is available in all CommonJS modules.
if I try to load withrequire("load-json-file")()
.
Now if I try to switch my package.json
to module
:
- I get
SyntaxError: Named export 'prompt' not found. The requested module 'prompt-sync' is a CommonJS module, which may not support all module.exports as named exports.
when I try to import like this:import { prompt } from "prompt-sync";
. - I get
ReferenceError: require is not defined in ES module scope, you can use import instead
if I try to import it like this:var prompt = require("prompt-sync")();
How can I use both those packages in the same project?