I keep encountering an issue in my project when attempting to load a module. Whenever I try to import it from the node_modules folder, I receive the following error:
Uncaught Error: Module not found: "./MyLibrary". Parent module folder was: "/"
However, if I manually copy the MyLibrary.js
file to the same directory, it imports without any issues.
What could be causing this problem?
Below is the code snippet:
Not working:
const MyLibrary = require("MyLibrary");
Working:
const MyLibrary = require("./MyLibrary").MyLibrary;
Package.json:
{
"name": "project",
"version": "1.0.0",
"description": "",
"main": "main.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"MyLibrary": "^1.4.8"
}
}
UPDATE:
When I provide the full path to the library, it does work, but then it raises an error with one of its dependencies.
const Sval = require('/node_modules/MyLibrary/dist/MyLibrary');
Error:
Uncaught Error: Module not found: "MyLibraryDependen". Parent module folder was: "/node_modules/MyLibrary/dist".
UPDATE 2:
It seems like the environment I'm using does not handle module resolution the same way as nodejs does.
...you would need to use something like webpack to be able to require() stuff from node_modules.