My Node version is V14.17.4 and my system version is MacOS Bigsur 11.6. When I was preparing to release a self-configured CLI project, I ran into a problem when I introduced node_modules using the common.js specification

const program = require("commander");
const download = require("download-git-repo");
const inquirer = require("inquirer");
const ora = require("ora");
const chalk = require("chalk");
const packageData = require("./package.json");
const fs = require("fs");
const path = require("path");
Copy the code

The console reported an error when the test -v command was executed

ReferenceError: require is not defined in ES module scope, you can use import instead
This file is being treated as an ES module because it has a '.js' file extension and '/Users/xxx//package.json' contains "type": "module". To treat it as a CommonJS script, rename it to use the '.cjs' file extension.
    at file:///Users/xxx/cli-demo/index.js:4:17
    at ModuleJob.run (internal/modules/esm/module_job.js:170:25)
    at async Loader.import (internal/modules/esm/loader.js:178:24)
    at async Object.loadESM (internal/process/esm_loader.js:68:5)
Copy the code

Following the error, I modified it by adding the configuration item “type”: “module” to the package.json file and changing the introduction of node_modules

import program from "commander";
import download from "download-git-repo";
import inquirer from "inquirer";
import ora from "ora";
import chalk from "chalk";
import packageJson from "./package.json";
import fs from "fs";
import path from "path";
Copy the code

But a new problem appeared and reported the following error

internal/process/esm_loader.js:74
    internalBinding('errors').triggerUncaughtException(
                              ^

TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".json" for /Users/lixue/code_boy/vue_product/cli-demo/package.json
    at Loader.defaultGetFormat [as _getFormat] (internal/modules/esm/get_format.js:71:15)
    at Loader.getFormat (internal/modules/esm/loader.js:105:42)
    at Loader.getModuleJob (internal/modules/esm/loader.js:243:31)
    at async ModuleWrap.<anonymous> (internal/modules/esm/module_job.js:75:21)
    at async Promise.all (index 5)
    at async link (internal/modules/esm/module_job.js:80:9) {
  code: 'ERR_UNKNOWN_FILE_EXTENSION'
}
Copy the code

I didn’t recognize the package.json file I introduced like this. Take a look at the fs.directory API in the Nodejs documentation, which explains how to use it

fs.readFileSync(path[, The options]) path < string > | < Buffer > | < URL > | < integer > filename or file descriptors options < Object > | < string > encoding < string > | < null > Default: NULL flag <string> See support for file system flags. Default value: 'r'. Returns: < string > | < Buffer > returns the content of the path. See the documentation for the asynchronous version of this API: fs.readfile () for more information. This function returns a string if the Encoding option is specified. Otherwise it returns the buffer. Similar to fs.readfile (), fs.readfilesync () behaves platform-specific when the path is a directory.Copy the code

You can introduce it like this

const packageJsonData = JSON.parse(fs.readFileSync('package.json'.'utf8'))
// Since fs.readFileSync returns a JSON string, parse to an object
// We can call packagejsonData.name to get the value of name in the current package.json
Copy the code

The problem has been solved.