Currently, I am in the process of transitioning to using imports instead of requires for modules. Here is an example of my previous code:
const { NETWORK } = require(`${basePath}/constants/network.js`);
The content of network.js file is as follows:
export const NETWORK = {
eth: "eth",
sol: "sol",
};
module.exports = {
NETWORK,
};
When attempting to import, I have tried various syntaxes such as:
import { NETWORK } from '../constants/network.js';
import NETWORK from '../constants/network.js';
import * as NETWORK from '../constants/network.js';
However, I encounter an error that states:
This file is being treated as an ES module because it has a '.js' file extension and '..\package.json' contains "type": "module". To treat it as a CommonJS script, rename it to use the '.cjs' file extension.
at file:///../constants/network.js:6:1
at ModuleJob.run (node:internal/modules/esm/module_job:193:25)
at async Promise.all (index 0)
at async ESMLoader.import (node:internal/modules/esm/loader:526:24)
at async loadESM (node:internal/process/esm_loader:91:5)
at async handleMainPromise (node:internal/modules/run_main:65:12)
Even when renaming the file to 'network.cjs', I face another error:
SyntaxError: Unexpected token 'export'
I am seeking guidance on how to appropriately import variables from js files using the import statement.