ErrorCaptureStackTrace(err); Error [ERR_MODULE_NOT_FOUND]: Cannot find module

Directory Structure

insert image description here
main.js

import { Name, say, Person } from './test'

console.log(Name)

test.js

const Name = 'life'
function say() {
	console.log('Mine')
}
let Person = { name: `good` }

export { Name, say, Person }

problem analysis

The steps are a bit long-winded, but the reason for the error is actually very simple, that is, the name of the file to be imported is not written in full, so the error that the corresponding file was not found will be reported as follows.

Error [ERR_MODULE_NOT_FOUND]: Cannot find module ‘d:\Project_Files\VSCode_Projects\React_Learning\《React全栈》\第1章\ES6语言特性\e7_Modules\test’ imported from d:\Project_Files\VSCode_Projects\React_Learning\《React全栈》\第1章\ES6语言特性\e7_Modules\main.js
at new NodeError (node:internal/errors:371:5)
at finalizeResolution (node:internal/modules/esm/resolve:416:11)
at moduleResolve (node:internal/modules/esm/resolve:932:10)
at defaultResolve (node:internal/modules/esm/resolve:1044:11)
at ESMLoader.resolve (node:internal/modules/esm/loader:422:30)
at ESMLoader.getModuleJob (node:internal/modules/esm/loader:222:40)
at ModuleWrap. (node:internal/modules/esm/module_job:76:40)
at link (node:internal/modules/esm/module_job:75:36) {
code: ‘ERR_MODULE_NOT_FOUND’
}

need

You want to main.jsimport test.jsthe module objects and variables exported by the file under the file

Solve the problem

Right-click on the corresponding terminal in VSCode,
insert image description here
enter the command npm init -yto create a new package.jsonfile in the terminal
insert image description here
, add "type": "modules"
insert image description here
to modify the imported file name,
main.js
insert image description here
and the final output is:
insert image description here

Guess you like

Origin blog.csdn.net/kokool/article/details/127481496