简体   繁体   中英

Including other libraries / files / plugins with nodejs and NPM

I'm using OSX 10.5 and playing around with Nodejs. I've managed to install npm and used it to install a couple of plugins. Well at least the installation says it has worked fine but when I try and load them in I get an error

$npm install htmlparser

Installs ok, then I create a file called test with

var htmlparser = require("node-htmlparser");

and run

$node test.js

I get

var htmlparser = require("node-htmlparser"); node.js:275 throw new Error("Cannot find module '" + request + "'");

I have this in my .bash_profile file:

export NODE_PATH="/usr/local/lib/node"

If I clone the GIT repos at this page and move the file lib/node-htmlparser.js to ~/.node_libraries then it works fine.

What is the point of using npm to install anything if I have to move the lib file like this? Am I missing something ?

var htmlparser = require('htmlparser')应该可以正常工作,因为它们从名称中var htmlparser = require('htmlparser')了“节点”。

Use the exact name that you use to install the module via NPM.

If you do
npm install htmlparser

Then your syntax for using the module should be
var htmlparser = require("htmlparser");

If that doesn't work, I'd check your npm and/or node install, but with all the details you listed above, that should work fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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