简体   繁体   English

浏览器验证后,客户端节点模块不起作用

[英]client-side node module doesn't work after browserify

I'm trying to develop an app under node.js. 我正在尝试在node.js下开发一个应用程序。 I need a module named autocomplete so I did 我需要一个名为自动完成的模块,所以我做了

npm install -S autocomplete

and that worked fine. 而且效果很好。 packages.json was updated and everything. packages.json已更新,一切packages.json更新。

But I need the functionality of autocomplete on the client side. 但是我需要客户端自动完成功能。 The most popular solution to this problem seems to be to use browserify, so I installed that globally and it seems to work. 解决此问题的最流行的方法似乎是使用browserify,因此我在全球范围内安装了该工具,并且似乎可以正常工作。

Now according to everything I've read, I should be able to: 现在,根据我阅读的所有内容,我应该能够:

cd node_modules
browserify autocomplete/index.js > bundle.js
mv bundle.js ../public/lib/js/

and then in views/index.html I should be able to have 然后在views / index.html中,我应该可以

<script src="lib/js/bundle.js">

and finally I should be able to say 最后我应该可以说

var auto = new Autocomplete();

because the Autocomplete object is defined in the autocomplete module and that's how the instructions say to instantiate it. 因为自动完成对象是在自动完成模块中定义的,这就是说明如何实例化它的方式。

But unfortunately my browser says Autocomplete is not defined so it's clearly not getting the message. 但不幸的是,我的浏览器显示“ Autocomplete is not defined因此显然没有收到消息。

What is wrong with the above? 上面有什么问题?

When you run Browserify, create it as a standalone module: 运行Browserify时,将其创建为standalone模块:

browserify autocomplete/index.js --standalone Autocomplete > autocomplete.js

I'm only changing the name here for simplicity - call it whatever you want. 我在这里只是为了简单起见更改名称-随便叫什么。

Then, when you run this in the browser as you have it currently, new Autocomplete() should be available. 然后,当您在浏览器中以当前方式运行它时, new Autocomplete()应该可用。

Edit While this is doable, the docs for this module are missing, and the module you are reading the docs for seems missing. 编辑尽管这是可行的,但缺少该模块的文档,并且您正在阅读文档的模块似乎缺失。

There isn't a need to go to this trouble when you can use something like typeahead which is already built for the browser. 当您可以使用已经为浏览器内置的诸如typeahead东西时,就不必麻烦了。

So it turns out because Autocomplete is not exported to the global scope. 事实证明,因为“自动完成”未导出到全局范围。 Try this in the file where you use Autocomplete, for example in the app.js: 在使用自动完成功能的文件中尝试此操作,例如在app.js中:

var Autocomplete = require('autocomplete');
var auto = new Autocomplete();

Then 然后

browserify app.js > bundle.js
mv bundle.js ../public/lib/js/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM