javascript - require('...') module not found!!
PHP中文网
PHP中文网 2017-06-26 10:50:33
0
3
960

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中文网

认证0级讲师

reply all(3)
迷茫

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

女神的闺蜜爱上我

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/..." />
洪涛

You should write the absolute path to Hogan .js

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template