V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
ghostgril
V2EX  ›  Node.js

es6 模块和 node 模块不兼容指的是?

  •  
  •   ghostgril · Mar 9, 2018 · 5955 views
    This topic created in 2983 days ago, the information mentioned may be changed or developed.
    在学习 es6 的模块加载中,看到说 node 的模块和 es6 的模块是不兼容的?但是我在 nodejs 创建的 vue 应用里面使用 es6 的模块加载却没有问题。。。那么 node 和 es6 的不兼容指的是什么呢
    9 replies    2018-03-09 17:16:20 +08:00
    VDimos
        1
    VDimos  
       Mar 9, 2018 via Android
    nodejs 以前是使用的 commonjs 作为自己的模块儿机制,es6 提出了 import 的方法,这两者不兼容。vue 使用 es6 模块是因为它用了 webpack 打包,或者说你的目标浏览器目前已经支持了 es6 语法
    ghostgril
        2
    ghostgril  
    OP
       Mar 9, 2018
    @VDimos 是不是在使用了 import 或者 export 的文件里面就不能再使用 require 和 module.export 呢
    zhengxiaowai
        3
    zhengxiaowai  
       Mar 9, 2018
    模块机制不兼容
    VDimos
        4
    VDimos  
       Mar 9, 2018 via Android
    @ghostgril 并不是,在 node 里面,目前已经支持了 import 语法,但是为了向后兼容,依然支持 require,可以一起使用。
    chairuosen
        5
    chairuosen  
       Mar 9, 2018
    export default 暴露的东西 require 时候要加.default
    raawaa
        6
    raawaa  
       Mar 9, 2018
    用了 webpack 了吧,webpack 会把 import 语句( es6 )转化成 require 语句( commonjs )
    2Go
        7
    2Go  
       Mar 9, 2018 via Android
    「不兼容」就是「不兹瓷」。如果你非要问我为啥不兹瓷,我的回答是无可奉告
    ghostgril
        8
    ghostgril  
    OP
       Mar 9, 2018
    @raawaa 原来如此
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   4620 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 36ms · UTC 10:05 · PVG 18:05 · LAX 03:05 · JFK 06:05
    ♥ Do have faith in what you're doing.