Directory Structure
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.js
import test.js
the module objects and variables exported by the file under the file
Solve the problem
Right-click on the corresponding terminal in VSCode,
enter the command npm init -y
to create a new package.json
file in the terminal
, add "type": "modules"
to modify the imported file name,
main.js
and the final output is: