0

My application is Node.JS, using .ejs to render views.

I'm trying to get require a dependency folder from node_modules, instead of having to place that node_module module(jquery, angular, moment, jquery-ui, kendo-ui-core, etc.) physically inside my 'public' folder for grabbing assets for my application or having to refer to a CDN in a script tag in my .ejs(html)file. I've tried adding:

   var moment = require('moment');

in my server.js(or what you would call 'app.js' or 'index.js' file) at the root of my application, but this doesn't seem to grab this folder. I've also tried to place

   var moment = require('moment');

within a javascript(.js) file, at the very top and was receiving an error stating: 'require isn't defined'. I'm assuming that NodeJs should know which modules are within its Node_modules folder so I'm thinking that maybe in my server.js file, node_modules isn't set to be used like my public folder is being used.

app.use(express.static(__dirname + '/public'));

using the app.use function. Most tutorials and even docs I've looked through just give the simple instruction: 'Just add var moduleName = require('module');' but they don't really elaborate in which file I do this, or if there is anything else to do to set this up outside of declaring a variable to a require function. I don't use a module loader so this MAY be the reason I'm getting the require is not defined. Also, I'm thinking maybe the installation of these modules either being installed globally(-g) or locally could possibly be a reason. But yea, totally lost.

In Summary: How do I use the node_modules in just one of my javascript files and how would I use this within my .ejs file without moving the js file outside of node_modules?

Jake_West
  • 111
  • 1
  • 7
  • RequireJS, SystemJS, Webpack, Browserify, Rollup, etc. etc. – Aluan Haddad Jan 16 '18 at 20:32
  • I don't currently use a module loader, clearly, and not really trying to avoid it, but I did want to see if there was an alternative to this, which would require a lot of modifications to tons of files correct? I'm assuming these module loaders are best for setting up right after you create your project. Or is this not as complicated as I think it is? – Jake_West Jan 17 '18 at 14:07

1 Answers1

0

there is a certain process to be followed when running a Node.js application. Below I will enumerate the steps to take when using npm which is available with Node.js:

  1. Install Node.js on your environment
  2. When Node is installed you will have access to the npm functionality in your terminal
  3. In the folder - which is referred as repository - that you want to place your app open your terminal and run npm init. This will create a file called package.json where all the dependencies of the application will be listed along with other data for the app
  4. To install any dependency run npm install appname (eg. npm install jquery)
  5. All your dependencies will be available in the node_modules folder that will be created when you first run npm install and will be also listed in package.json
  6. Create an index.js file in your application's folder (repository) with any require statements you need
  7. Run node index.js in your termimal, this will start node and any require statement should work fine, as long as you have npm install it

I hope this helps. Just keep in mind that node runs javascript in an environment outside the browser, in our case in your local machine.

One last thing, you refer to app.use(express.static(__dirname + '/public')); that is actually a middleware and is used when your app acts like a server, it has no correlation with require whatsoever.

jioakim
  • 1
  • 1
  • Hey, thanks, jioakim for the intro, but my issue is after these steps. I asked how do I use the node_modules folder? I've used var module_name = require(); in my .ejs file(view) and within javascript files, both don't read the files, and I have to manually copy the module out of the node_modules folder, instead of leaving it in there and having NodeJS find a path to this file. Not sure where in the application this happens. So that's pretty much my question. Where do you set up the path to the node_modules folder dependencies? Am I forced to use some module loader like Webpack or RequireJS? – Jake_West Jan 17 '18 at 14:03
  • Hello Jake, just to clarify the reason that you don't see the node_modules files in your client side is because they are available only to the server, for example in app.js. Also please take note that the `require` statement will not work on client side javascript, it only works on the server side. You can take a look at [this post as well](https://stackoverflow.com/questions/14264429/how-to-use-jquery-installed-with-npm-in-express-app). Hope this helps. – jioakim Jan 17 '18 at 21:29
  • Module loaders can be used but there is the extra complexity of setting them up. If you have just a few dependencies and a simple app in my opinion you can add the dependencies directly to the client. – jioakim Jan 17 '18 at 21:54
  • hey didnt know that about the node_modules(used only client-side), kind of jump into node without a proper intro into its limitations I guess. Awesome advice about if I should add the dependencies directly. I don't feel so bad about adding these modules manually now. lol. – Jake_West Jan 18 '18 at 21:29