I’m having issues getting npm modules to work with require in my Node.js project. I’ve installed Node.js (v0.6.6) and npm (v1.1.15) on Windows 7 64-bit. I’ve also installed express using npm, both locally and globally.
I’ve set up a simple app like this:
const server = require('framework').newServer();
server.get('/', (request, response) => {
response.send('Welcome to my app');
});
server.listen(5000);
When I run it, I’m getting an error: ‘Cannot find module “framework”’. It seems like Node.js isn’t checking the right folder for modules. How can I specify that it should look in ‘C:\Users\MyName\node_modules’?
I’ve tried a few online solutions, but nothing has worked so far. Any advice is greatly appreciated!
hey mate, have u tried using the full path when requiring? like this:
const framework = require(‘C:\Users\MyName\node_modules\framework’);
if that dont work, maybe check ur PATH environment variable. sometimes windows can be a pain with that stuff.
I encountered a similar issue when I first started with Node.js. The problem likely stems from using an outdated version of Node.js and npm. Version 0.6.6 is quite old and may not support the current module resolution system.
I’d strongly recommend upgrading to the latest LTS version of Node.js, which includes a more recent npm version. This should resolve most module-related issues out of the box.
If you must stick with the older version, try adding the following to your script:
const path = require('path');
require.paths.push(path.join(__dirname, 'node_modules'));
This explicitly adds the local node_modules directory to the module search path. However, this approach is deprecated in newer Node.js versions, so upgrading remains the best solution.
I’ve been down this road before, and it can be frustrating. Your issue likely stems from using an older Node.js version, so I strongly suggest upgrading to the latest LTS release—it will save you many headaches. If you must stick with v0.6.6, consider creating a package.json file in your project root if you haven’t already, then add a dependencies section with your required packages and run npm install in your project directory. This should install the packages locally so require() can find them. Also, double-check that you are using the correct package name, as ‘framework’ is not standard. Keeping your development environment up-to-date is crucial for a smoother experience.