nodejs
坑一:模塊require
在html引入的js文件里require的時候是基于該html文件的。
目錄結(jié)構(gòu)
src/
index.html
js/
index.js
test.js
src/index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<script src="./js/index.js"></script>
</body>
</html>
src/js/index.js
const test = require('./test');
test();
src/js/test.js
module.exports = function(){
console.log('you are require src/js/test.js');
}
用nodejs運行上面的代碼新症,打開index.html袋哼,會報找不到./test模塊的錯誤涌献,將index.js的代碼改成下面的就行了。
src/js/index.js
const test = require('./js/test'); //基于index.html所在目錄下。
test();