Creating an NPM package often involves using the following:
npm link
This allows for making modifications to a package called <myPackage>
during development without constantly having to publish and unpublish! Developers can make changes locally and immediately see their effects.
To install it in a project, use:
npm link <myPackage>
While this method is beneficial, there can be an issue if <myPackgage>
requires a path.
In such cases, it will reference the true location of <myPackage>
as __dirname instead of the expected symlink location, which should be local to the project like a typical node_module.
So far, I have come up with a solution that works well for my specific scenario:
module.exports = {
loadImage: function (filename) {
var img
if (typeof window !== 'undefined' && ({}).toString.call(window) === '[object Window]') {
try {
img = require('../../src/images/' + filename)
} catch (e) {
// For development purposes only
img = require('./template/src/images/' + filename)
}
} else {
img = '/assets/images/' + filename
}
return img
}
}
However, this approach may result in warning messages in the browser.
Although I understand the cause of the problem, ideally, I would prefer to suppress the error message.