2

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-emptyis 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?

3
  • Use require(). Commented Aug 21, 2020 at 3:38
  • @Take-Some-Bytes is it some new change in node? In my 2018 tutorial, it works as above. Commented Aug 21, 2020 at 3:56
  • It looks like you are now combining esm syntax in your cjs module. Just use only one style of module would resolve your issue Commented Aug 21, 2020 at 4:02

2 Answers 2

2

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)
    }
}
``
Sign up to request clarification or add additional context in comments.

1 Comment

Is it something new? Because the import above` worked fine on the tutorial which I'm learning from.
0

What they're trying to say is that you cannot mix the two. You can use ESM and use only requires or you can use imports. 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().

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.