Coding⏱️ 2 min read📅 2026-06-03

How to Fix: How do I import global modules in Node? I get "Error: Cannot find module <module>"?

Importing global modules in Node.js on Mac OSX Lion.

Quick Answer: Check that the module path is correct and try using absolute paths or requiring the module with a relative path.

The issue you're facing is due to the way Node.js searches for modules. When you run `node -e require.paths`, it shows the paths where Node looks for modules, but it doesn't guarantee that the module is actually installed in those locations.

✅ Best Solutions to Fix It

Method 1: Update the `PATH` Environment Variable

  1. Step 1: Open your terminal and run the command `export PATH=$PATH:/usr/local/lib/node_modules
  2. Step 2: Restart your terminal or run a new instance of Node.js to apply the changes.

Method 2: Use `require.resolve()`

  1. Step 1: Import the module using `const module = require.resolve('module-name');
  2. Step 2: Use the resolved path to load the module, e.g., `require.resolve()`
    1. Step 1: Load the module using its resolved path.

💡 Conclusion

By applying these solutions, you should be able to import global modules in Node.js without encountering the `Cannot find module` error.

Did this fix your problem?

If not, try searching for specific error codes.

🔍 Search Error Database

❓ Frequently Asked Questions