简体   繁体   English

为什么我的代码适用于 require 但不适用于 import Node JS

[英]Why my code work with require but don't work with import Node JS

I am beginner in Node JS.I have questions... I want to export multiple modules is node with module.exports when i use requre my code work but don't work with import {} from.I know Node js doesen't provide ES6.But if i change my package.json and write type:module it can work.With export default my app work with import but with module.exports it can't work.我是 Node JS 的初学者。我有问题...我想导出多个模块是 node 与 module.exports 当我使用要求我的代码工作但不使用 import {} from.我知道 Node js 没有提供 ES6。但是如果我更改我的 package.json 并写入 type:module 它可以工作。默认导出我的应用程序可以使用 import 但使用 module.exports 它不能工作。 I asked because in my last interview i got this question.And in my fitback there was this:Exporting multiple entities with <exports.[name-of-entity]>我问是因为在我上次的采访中我得到了这个问题。在我的 fitback 中有这样的:Exporting multiple entity with <exports.[name-of-entity]>

Please help rosolve it))请帮助解决它))

import lib from "./index2.js"

lib.double()
function double(){
    return console.log('Hello double')
}

function trouble(){
    return console.log('Hello trouble!')
}

module.exports = {
    double,
    trouble
}

Import and require are not the same. Importrequire是不一样的。

This link is informative此链接提供信息

The difference between "require(x)" and "import x" “require(x)”和“import x”的区别

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

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