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 trialKyle Knapp
21,526 PointsCannot 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
Dale Severude
Full Stack JavaScript Techdegree Graduate 71,350 PointsHi Kyle,
Your code path needs to move up a few directories in order to find the requireMe.js file.
const foo = require('../../requireMe.js');
Dale Severude
Full Stack JavaScript Techdegree Graduate 71,350 PointsThere 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);
Kyle Knapp
21,526 PointsThank You!
Kyle Knapp
21,526 PointsKyle Knapp
21,526 PointsThank 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')
?