import 时候的路径问题(新手容易碰到)

开始玩的时候,总是出现 cannot find module 问题,原来 在 import 的时候 如果不使用相对路径或者绝对路径,node默认会去node_modules/文件夹下去找,例如:

1 import * as obj from 'exports'
2 // node 会试着去寻找 node_modules/exports.js 模块
3 
4 // 正确写法
5 import * as obj from './exports'

 

关于 import * as obj from 'xx'  这种写法是把所有的输出包裹到obj对象里

对了,还有模块的继承写法:

复制代码
 1 // circle.js
 2 export var a = 1;
 3 
 4 // circleplus.js 当前模块继承了 circle 模块的所有输出
 5 // 此处只是继承了输出,并不能直接使用
 6 
 7 export * from 'circle';
 8 export var e = 2.71828182846;
 9 export default function(x) {
10   return Math.exp(x);
11 }
12 
13 // 继承之后,circleplus.js 相当于下面代码
14 export var a = 1;
15 export var e = 2.71828182846;
16 export default function(x) {
17   return Math.exp(x);
18 }
复制代码

 

代码改变世界,我的《源代码》,我的世界!
posted on 2019-01-14 15:26  yellwonfin  阅读(112)  评论(0)    收藏  举报