how to install new nodejs modules to expressjs project?

243 views Asked by At

Currently I am working on online yaml code editor. I use expressjs framework for it. I want to install yamljs module to it. How do I install it? Because following code line cause to an error.

var YAML = require('yamljs');

This is the code segment

var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');


var routes = require('./routes/index');
var users = require('./routes/users');

var YAML = require('yamljs');

var app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');

This is the error

Error: Cannot find module 'yamljs'
    at Function.Module._resolveFilename (module.js:338:15)
    at Function.Module._load (module.js:280:25)
    at Module.require (module.js:364:17)
    at require (module.js:380:17)
    at Object.<anonymous> (/home/tharindu/Documents/projects/Group project2/CuubezFinal/app.js:12:12)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Module.require (module.js:364:17)

I use webstorm for coding. I have installed yamljs module using

npm install yamljs
This library is perfectly using if I run my code manually from the ubuntu terminal. But When I run the expressjs project, It gives above error.

2

There are 2 answers

3
Andreas On

could you provide more information like the error text?

Have you tried

npm install yamljs

to make the module accessible to the node environment?

0
Hyo Byun On

This library is perfectly using if I run my code manually from the ubuntu terminal. But When I run the expressjs project, It gives above error.

Make sure your node_modules folder is available in the same or a more outer directory than the folder of your executing script.