简体   繁体   中英

Get Cannot use import statement outside a module in node.js

I'm quite new to node.js and following a tutorial, using node v13.6.0 . I'd like to import is-empty' into this file:

const Validator = require('validator');

import isEmpty from './is-empty';

module.exports = function validateRegisterInput(data) {
    
    let errors = {};

    if(Validator.isLength(data.name), {min:2, max: 30}) {
        errors.name = 'name is too short or too long';
        }
    return {
        errors, 
        isValid: isEmpty(errors)
    }
}

But I get this error:

SyntaxError: Cannot use import statement outside a module
    at wrapSafe (internal/modules/cjs/loader.js:1060:16)
    at Module._compile (internal/modules/cjs/loader.js:1108:27)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1164:10)
    at Module.load (internal/modules/cjs/loader.js:993:32)
    at Function.Module._load (internal/modules/cjs/loader.js:892:14)
    at Module.require (internal/modules/cjs/loader.js:1033:19)
    at require (internal/modules/cjs/helpers.js:72:18)
    at Object.<anonymous> (/home/me/myapp/routes/api/users.js:12:31)
    at Module._compile (internal/modules/cjs/loader.js:1144:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1164:10)
    at Module.load (internal/modules/cjs/loader.js:993:32)
    at Function.Module._load (internal/modules/cjs/loader.js:892:14)
    at Module.require (internal/modules/cjs/loader.js:1033:19)
    at require (internal/modules/cjs/helpers.js:72:18)
    at Object.<anonymous> (/home/me/myapp/server.js:7:15)
    at Module._compile (internal/modules/cjs/loader.js:1144:30)

is-empty is defined like this:

const isEmpty = value =>
    value === undefined ||
    value === null ||
    (typeof value === 'object' && Object.keys(value).length === 0) ||
    (typeof value === 'string' && value.trim().length === 0);

    module.exports = isEmpty

I'm wondering how can I fix this?

You should be using require instead of import as documented in https://nodejs.org/api/esm.html#esm_package_json_type_field

const Validator = require('validator');

const isEmpty = require('./is-empty');

module.exports = function validateRegisterInput(data) {
    
    let errors = {};

    if(Validator.isLength(data.name), {min:2, max: 30}) {
        errors.name = 'name is too short or too long';
        }
    return {
        errors, 
        isValid: isEmpty(errors)
    }
}
``

What they're trying to say is that you cannot mix the two. You can use ESM and use only require s or you can use import s. Since Node already treats your javascript as commonJS modules, you can use imports, but the moment you introduce require, the whole thing falls over.

I've just been through this with an old protractor project that was using requires. It's all or nothing, one or the other.

https://nodejs.org/api/esm.html#esm_interoperability_with_commonjs

Using require to load an ES module is not supported because ES modules have asynchronous execution. Instead, use import() to load an ES module from a CommonJS module.

https://nodejs.org/api/esm.html#esm_differences_between_es_modules_and_commonjs

No require, exports or module.exports
In most cases, the ES module import can be used to load CommonJS modules.
If needed, a require function can be constructed within an ES module using module.createRequire().

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