Why is the result of import module from node_modules different from that of direct import dist source file?.

problem description

it is normal to import modules from node_modules, but the direct import dist file is undefined,

related codes

// from node_modules, work well
import Layout from "@xxx/layout-auth/dist/layout" 

// from source dist, resolve to undefined
import Layout from "/User/xx...xxx/layout-auth/dist/layout" 

you have to see what the package.json,main field is


according to node's module parsing rules, if the second path is not in the current node path, it cannot be parsed


import import must be exported in the original file, but the dist folder is generally compiled files, may not be exported


this question found the answer?


because of the module under the node_module folder that you refer to, usually the project will be compiled into an es6 module by bebel, and then provided to the front end for reference. If you do not import du dialect from the node_module file (your current module may be the commonjs module), you need to simply configure the babel configuration file to translate the file you imported

Menu