我如何在客户端JavaScript中要求节点模块? [英] How do I require a node module in my client side javascript?
问题描述
我正在尝试使用javascript和jQuery构建逻辑和节点的自然语言处理机器人,并表示为框架.我发现了一种自然语言处理工具,对于我的项目 https://github.com/NaturalNode/natural 不幸的是,文档很少,而且我仅使用node已有两周了.
I am trying to build a natural language processing bot using javascript and jQuery for the logic and node and express as the framework. I discovered a natural language processing facility that would be extremely useful for my project https://github.com/NaturalNode/natural unfortunately the documentation is sparse and I have only been using node for a couple of weeks.
这是我在服务器端app.js上的代码
This is the code I have on my server side app.js
var natural = require('natural'),
tokenizer = new natural.WordTokenizer();
stemmer.attach();
var express = require("express");
app = express();
app.set("view engine", "ejs");
app.use(express.static(__dirname + '/public'));
我在这里需要自然"模块,但无法使用文档中概述的方法.
I am requiring the 'natural' module here but I am unable to use the methods that are outlined in the documentation.
var natural = require('natural'),
tokenizer = new natural.WordTokenizer();
console.log(tokenizer.tokenize("your dog has fleas."));
// [ 'your', 'dog', 'has', 'fleas' ]
方法'tokenizer'未定义.我对此进行了大量研究,并研究了如何使用module.export并通过app.get函数将变量传递给我正在使用的index.ejs页面,但到目前为止仍未成功.
The method 'tokenizer' is undefined. I have done quite a bit of research on this and looked into using module.export and passing variables through the app.get function for the index.ejs page I am using but I have been unsuccessful so far.
注意:我尝试使用这些方法的javascript文件位于public/javascript目录中,而app.js文件位于主项目目录中.我试图直接在我要使用方法的javascript文件中要求自然"包,但抛出了一个错误,说require是未定义的方法.
NOTE: The javascript file I am trying to use these methods in is located in the public/javascript directory while the app.js file is located in the main project directory. I have tried to require the 'natural' package directly in the javascript file I am trying to use the methods in but an error was thrown saying require is an undefined method.
这是package.json文件:
here is the package.json file:
{
"name": "JSAIN",
"version": "1.0.0",
"description": "",
"main": "app.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"ejs": "^2.3.1",
"express": "^4.12.3",
"natural": "^0.2.1",
"underscore": "^1.8.2"
}
}
推荐答案
也许您可以尝试 browserify 允许您在浏览器中使用一些npm模块.
maybe you can try browserify which allow you to use some npm modules in browser.
这篇关于我如何在客户端JavaScript中要求节点模块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!