Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Kyle Knapp
Kyle Knapp
21,526 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,526 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,349 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);