search

Home  >  Q&A  >  body text

node.js - 关于import React from 'react';的一点问题

问题一:
我是使用webpack打包的
为什么我的react模块放在node_modules中也只需要写成
import React from 'react';
就可以在另一个文件夹里引入react了?我并没有看到哪里设置了路径给react阿

问题2:
我想问一下用webpack的时候使用import和export,是相当于使用ES6的呢?还是node里的内置对象呢?

天蓬老师天蓬老师2779 days ago701

reply all(2)I'll reply

  • 巴扎黑

    巴扎黑2017-04-17 16:21:26

    Answer 1: If you write import React from 'react';, webpack will give priority to see if you have defined resolve.alias. code>react, if not, look for it in node_modules. import React from 'react';,webpack会优先看看你有没有在resolve.alias里看看你有没有定义react,如果没有的话,就在node_modules里找。

    答问二:这import和export都是ES6的语法,并不是什么内置对象,你看webpack1就不支持这import/export的语法,需要用babel转换成commonjs的写法require

    Answer 2: Both import and export are ES6 syntax and are not built-in objects. You see, webpack1 does not support this import/export syntax. You need to use babel to convert it to commonjs writing require . 🎜

    reply
    0
  • 伊谢尔伦

    伊谢尔伦2017-04-17 16:21:26

    Question 1: It is determined by the module loading mechanism of nodejs. You can read the official documentation. If you are not familiar with NODE, there is nothing you can do.
    Question 2: There is no conflict between the built-in objects of ES6 and NODE. Node relies on the V8 platform, so whether ES6 syntax is supported or not depends on the V8 platform, and V8 currently partially supports ES6.
    However, since react needs to run on the browser side, in order to ensure browser compatibility, babel is generally required to translate it into standard ES5 syntax. This actually has nothing to do with node itself, but to be compatible with different versions of browsers. .

    reply
    0
  • Cancelreply