[英]How do I require a node module in my client side javascript?
I am trying to build a natural language processing bot using javascript and jQuery for the logic and node and express as the framework. 我正在尝试使用javascript和jQuery构建逻辑和节点的自然语言处理机器人,并表示为框架。 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. 我发现了一种自然语言处理工具,这对我的项目https://github.com/NaturalNode/natural非常有用,不幸的是文档很少,而且我只使用node几周了。
This is the code I have on my server side app.js 这是我在服务器端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' ]
The method 'tokenizer' is undefined. 方法“ tokenizer”未定义。 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. 我对此进行了大量研究,并研究了如何使用module.export并将变量通过app.get函数传递给我正在使用的index.ejs页面,但是到目前为止,我一直没有成功。
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. 注意:我尝试使用这些方法的javascript文件位于public / javascript目录中,而app.js文件位于主项目目录中。 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. 我试图直接在我尝试使用方法的javascript文件中要求“自然”包,但抛出了一个错误,说require是未定义的方法。
here is the package.json file: 这是package.json文件:
{
"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模块。
Converting my comment into an answer... 将我的评论转换为答案...
The natural module says it's for running in the node.js environment. 自然模块说它是用于在node.js环境中运行的。 Unless it has a specific version that runs in a browser, you won't be able to run this in the browser. 除非它具有在浏览器中运行的特定版本,否则您将无法在浏览器中运行它。
have you install that module with global mode and have defined that in package.json? 您是否以全局模式安装了该模块,并在package.json中定义了该模块? Otherwise, try this: 否则,请尝试以下操作:
npm install -g natural
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.