简体   繁体   English

导入 vs 需要带斜线

[英]import vs require with slashes

Note: I am on node 14.1注意:我在节点 14.1 上

So I'm currently trying this line:所以我目前正在尝试这条线:

import "module-alias/register";

Which gives me the error这给了我错误

Error [ERR_MODULE_NOT_FOUND]: Cannot find module

the error pathing is:错误路径是:

node_modules/module-alias/register


but if I use commonjs但是如果我使用 commonjs

require("module-alias/register");

it works.有用。

What's the difference between import and require that causes the slash to become an error? import 和 require 导致斜线变成错误的区别是什么?

1) Whenever you use to import, 1)每当你使用导入时,

import "module-alias/register";

at that time Compiler will check the relative path of your module register , in your case if you want to use import statement in NodeJs or javascript you have to specify your usable method of their module ( it is ES6 Syntax ) eg.那时编译器将检查你的模块寄存器的相对路径,在你的情况下,如果你想在 NodeJs 或 javascript 中使用 import 语句,你必须指定他们模块的可用方法(它是 ES6 语法)例如。 you have profile method in your module register then you can write something like this,你的模块寄存器中有配置文件方法然后你可以写这样的东西,

import { profile } form "module-alias/register";

2) Whenever you use require, 2) 每当你使用 require 时,

require("module-alias/register");

it is same as import feature but there is one difference you can't specify any specific method of your module register,( IT is Common JS Syntax ) as the above example, you can't specify the profile method of your module register.它与导入功能相同,但有一个区别,您不能指定模块注册的任何特定方法,( IT 是 Common JS Syntax )如上例,您不能指定模块注册的配置文件方法。

3) 3)

  1. The difference is like import is specified specific method of any module for further use in require we can't do that.不同之处在于 import 是指定任何模块的特定方法,以便在 require 中进一步使用我们不能那样做。
    1. import is asynchronous and require is synchronous. import 是异步的,require 是同步的。
    2. Import is Syntax of ES6 JS and Require is Syntax of Common JS import是ES6 JS的Syntax,Require是Common JS的Syntax

The difference between import and require is that import is ES6 syntax and that's the correct syntax for it you need to import something from Ex: express-validator. import 和 require 之间的区别在于 import 是 ES6 语法,这是正确的语法,你需要从 Ex: express-validator 导入一些东西。

import { check, validationResult } from 'express-validator';

Also import can be asynchronous.导入也可以是异步的。

register is a js file, and import requires explicit extensions thus import "module-alias/register"; register是一个 js 文件,并且 import 需要显式扩展,因此import "module-alias/register"; will not work不管用

but import "module-alias/register.js";import "module-alias/register.js"; will.将要。

import is used with ES6 and require is used with commonjs import 与 ES6 一起使用,require 与 commonjs 一起使用

  1. for using import we should use, something like this对于使用 import 我们应该使用,像这样

import myObject from './File.js';

and for exporting,和出口,

export default myObject;

  1. for using require使用要求

var myObject = require('./File.js');

and for exporting,和出口,

module.exports = myObject;

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

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