Directory Structure
The following is the content of hogan.html
var Hogan = require('hogan.js'); mytools = { renderHtml: function(htmlTemplate, data) { var template = Hogan.compile(htmlTemplate); var result = template.render(data); return result; } } var htmlTpl = '{{name}}
'; var data = '张三'; mytools.renderHtml(htmlTpl ,data);
I am npm i --save hogan.js to install the js module.
require('hogan.js') in html
will report an error require is not defined.
Usually used directly in webpack.
How to use require??
This means you don’t have this in your
node_modulesdirectory. Check where you installed itI just didn’t see clearly that you wrote
requireinhtml, which only supportsrequireinjsSo have you solved it? Do you want to
If you want to importrequireinsidejsorrequireinsidehtml?jsnormallyrequireandhtml, just go to thenode_modulesfolder and find the path tohongan.jsrequireis a built-in method ofnode.js. When you run Webpack,npm run buildactually uses node to run the webpack compilation script, so require is supported. But now that you have written it natively, you are out of touch withnode.js, which does not natively supportrequire()loading packages.The correct way is to add files directly using
andtags:You should write the absolute path to Hogan .js