簡體   English   中英

獲取不能在 node.js 中的模塊外使用導入語句

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

我對 node.js 很陌生,並按照教程使用node v13.6.0 我想將is-empty'導入此文件:

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)
    }
}

但我得到這個錯誤:

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定義如下:

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

我想知道我該如何解決這個問題?

您應該使用 require 而不是 import 如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)
    }
}
``

他們想說的是,你不能將兩者混為一談。 你可以使用 ESM 並且只使用require或者你可以使用import 由於 Node 已經將您的 javascript 視為 commonJS 模塊,因此您可以使用導入,但是在您引入 require 的那一刻,整個事情就崩潰了。

我剛剛使用了一個使用 requires 的舊 protractor 項目。 要么全無,要么全無。

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

不支持使用 require 加載 ES 模塊,因為 ES 模塊具有異步執行。 相反,使用 import() 從 CommonJS 模塊加載 ES 模塊。

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

沒有 require、exports 或 module.exports
大多數情況下,可以使用 ES 模塊導入來加載 CommonJS 模塊。
如果需要,可以使用 module.createRequire() 在 ES 模塊中構建 require function。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM