search

Home  >  Q&A  >  body text

javascript - require('...') module not found!!

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 = '<p>{{name}}</p>';
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??

PHP中文网PHP中文网2777 days ago962

reply all(3)I'll reply

  • 迷茫

    迷茫2017-06-26 10:52:33

    This means you don’t have this in your node_modules directory. Check where you installed it

    I just didn’t see clearly that you wrote require in html, which only supports require in js

    So have you solved it? Do you want to require inside js or require inside html?

    If you want to import

    js normally require and html, just go to the node_modules folder and find the path to hongan.js

    reply
    0
  • 女神的闺蜜爱上我

    女神的闺蜜爱上我2017-06-26 10:52:33

    require is a built-in method of node.js. When you run Webpack, npm run build actually 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 with node.js, which does not natively support require()loading packages.

    The correct way is to add files directly using <script> and <link> tags:

    <script src="./node_modules/..."></script>
    <link href="./node_modules/..." />

    reply
    0
  • PHP中文网

    PHP中文网2017-06-26 10:52:33

    You should write the absolute path to Hogan .js

    reply
    0
  • Cancelreply