简体   繁体   中英

import statement doesn't throw any error when a exported property is not defined in the export file

while checking import/ export feature, it was surprising to see that if a named property is used in import and it is not defined in the exported file than it does not throw any error?

below is the example

_export.js

export const ALPHA = 'alpha';
export const BETA = 'beta';

_import.js

import {ALPHA, BETA, GAMMA} from './export_'

console.log("alpha is ", ALPHA);
console.log("beta is", BETA);
console.log("gamma is", GAMMA);

Here GAMMA is not defined in _export.js but do not throw any error regarding this in _import.js ?

I was facing this issue as I have accidentally imported a wrong file ( same file name in 2 directories) in import statement but it does not throw any error that the given exported named property is not found in the file.

Is this behavior is correct?

or

Is there any linting rule in linter ( eg. eslint) available which identifies such mistake and throws error or warning to the developer?

As far as i know, esLint doesnt provide this check out of the box.
You may want to use eslint-plugin-import .
I use it and it throws an error of import/named when a module wasn't found.
For example:

MyModule not found in '../../modules'

examples

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM