Note: I am on node 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
require("module-alias/register");
it works.
What's the difference between import and require that causes the slash to become an error?
1) Whenever you use to import,
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. 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,
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.
3)
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 { 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";
will not work
but import "module-alias/register.js";
will.
import is used with ES6 and require is used with commonjs
import myObject from './File.js';
and for exporting,
export default myObject;
var myObject = require('./File.js');
and for exporting,
module.exports = myObject;
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.