简体   繁体   English

获取不能在 node.js 中的模块外使用导入语句

[英]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 .我对 node.js 很陌生,并按照教程使用node v13.6.0 I'd like to import is-empty' into this file:我想将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)
    }
}

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: 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

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您应该使用 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)
    }
}
``

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.你可以使用 ESM 并且只使用require或者你可以使用import Since Node already treats your javascript as commonJS modules, you can use imports, but the moment you introduce require, the whole thing falls over.由于 Node 已经将您的 javascript 视为 commonJS 模块,因此您可以使用导入,但是在您引入 require 的那一刻,整个事情就崩溃了。

I've just been through this with an old protractor project that was using requires.我刚刚使用了一个使用 requires 的旧 protractor 项目。 It's all or nothing, one or the other.要么全无,要么全无。

https://nodejs.org/api/esm.html#esm_interoperability_with_commonjs 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.不支持使用 require 加载 ES 模块,因为 ES 模块具有异步执行。 Instead, use import() to load an ES module from a CommonJS module.相反,使用 import() 从 CommonJS 模块加载 ES 模块。

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

No require, exports or module.exports没有 require、exports 或 module.exports
In most cases, the ES module import can be used to load CommonJS modules.大多数情况下,可以使用 ES 模块导入来加载 CommonJS 模块。
If needed, a require function can be constructed within an ES module using module.createRequire().如果需要,可以使用 module.createRequire() 在 ES 模块中构建 require function。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SyntaxError:无法在 node.js 上的模块外部使用 import 语句 - SyntaxError: Cannot use import statement outside a module on node.js Node.js:语法错误:无法在模块外使用导入语句 - Node.js: SyntaxError: Cannot use import statement outside a module 不能使用 jest 在模块 node.js 之外使用 import 语句 - Cannot use import statement outside a module node.js using jest 未捕获的语法错误:无法在节点 js 中的模块外使用导入语句 - Uncaught SyntaxError: Cannot use import statement outside a module in node js WASM 和 Node.js 不能在模块外使用“import.meta” - WASM and Node.js Cannot use 'import.meta' outside a module JS 导入导出语法错误:不能在模块外使用导入语句 - JS import export SyntaxError: Cannot use import statement outside a module 不能在 /node_modules 中的模块外使用 import 语句 - Cannot use import statement outside a module in /node_modules 运行 js 测试 - 得到“不能在模块外使用 import 语句” - Running js tests - getting “Cannot use import statement outside of a module” Nodejs运行js报错:Cannot use import statement outside a module - Nodejs run js error: Cannot use import statement outside a module JS npm 包,不能在模块外使用 import 语句 - JS npm package, Cannot use import statement outside a module
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM