Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript

Kyle Knapp
Kyle Knapp
21,525 Points

Cannot find module node.js

Workspace snapshot: https://w.trhou.se/lzs4xyte5q

Running node _misc/tests/requireTest.js in the console returns Cannot find module '../requireMe.js'

I have also tried require('./requireMe.js') and require('requireMe.js') with the same error.

Is it not possible to require a module from a parent folder? Or is the path name wrong?

Thanks!

2 Answers

Kyle Knapp
Kyle Knapp
21,525 Points

Thank You Dale,

Is there a way to path directly to the root project folder instead of moving up each directory one at a time?

For example, lets say that I have a file nested 5 directories down from root. Is require('../../../../../requireMe.js') the only way to find the module in the root folder? Is there a shortcut, like an asterisk or another notation, to go directly to the project's root folder, like this: require('**/require.js') ?

Dale Severude
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Dale Severude
Full Stack JavaScript Techdegree Graduate 71,324 Points

There is an npm module that will do this.

https://www.npmjs.com/package/app-root-dir

// get the application's root directory
var appRootDir = require('app-root-dir').get();

// set the application's root directory
// (this will set a global so that no matter
// how many instances of app-root-dir module are
// installed, they will all return the same
// directory)
require('app-root-dir').set(__dirname);