npx MODULE_NOT_FOUND when hardcoding dependency name - node.js

I have a dependency that I don't import directly in my node application. I pass the dependency name as a string to another dependency through my code. It works locally, but I get a MODULE_NOT_FOUND when it's packaged up and called using npx <NODE_APPLICATION> .... The dependency is included in my package.json and it's found if I import it directly in my code, but I assume the path of the dependencies change when packaged up. I publish the package using yarn lerna publish from-package. Is there a way I can reference/find the dependency's path through my code?

Related

How to include polyfills in self-published npm package so that it does not cause errors when used with Webpack v5.x?

Hi this is my first npm package (it is a React component library) that Ive published and I realized that while it works perfectly fine when used with Webpack v4.x, the issue is that anyone who uses Webpack v5.x or uses npx create-react-app command to create a React app and tries to use my npm package will encounter a bunch of errors due to Webpack v5.x no longer including polyfills.
Thus the person will have to install a number of npm packages and then configure their Webpack.config.js file to include a number of fallbacks.
For example, the person will get 15-20 error messages when they try to use my npm package such as cannot resolve crypto or cannot resolve http, etc. This is due to one of the dependencies for my npm package being the npm package ‘ws’.
Is there anything I can do on my end when publishing my npm package so that these additional steps for users of my npm package is not necessary and they won’t have to install a bunch of npm packages and then edit their Webpack.config.js file just to able to use my npm package?

npm binary using ts-node causes errors when invoked using npm i -g or npx

I have a module which exposes a bin in package.json which points to a TypeScript file. The .ts file has a ts-node shebang and is executable and works well.
However, when trying to use this binary from another package, I get errors:
Using npm i -g to install globally, when the binary is called which in turn calls ts-node I get errors for all the missing types declared in devDependencies of the module containing the binary, since devDependencies aren't installed when installing the module from another module. I have to manually install all the devDependencies such as `npm i -g #types/lodash" which makes no sense to do.
Using npx causes module related errors since I'm trying to use import syntax etc from a stand-alone ts-node call:
(node:67861) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
(Use `node --trace-warnings ...` to show where the warning was created)
SyntaxError: Cannot use import statement outside a module
Is there any way to build a module which exposes a binary which is a wrapper around a ts-node script, and where that binary is actually executable through npm i -g or npx?
Move the "missing" package references into "dependencies".
NPM says that "devDependencies" is for "Packages that are only needed for local development and testing" while "dependencies" is for "Packages required by your application in production". So what is "production"? In the case of modules like this, "production" means use in other packages!
In other words, since the type-only packages are still truly required for use by a consumer, they don't belong in "devDependencies" but in "dependencies". This wouldn't be the case if you were pre-compiling into plain JS, but since you're not, the type packages simply need to be there in "production".
Note that this won't necessarily increase your ultimate bundle size in e.g. a web app. If your consumer package lists your tool package in "devDependencies", then ultimately the types packages will not be considered necessary for "production" for the consumer package.

using lovell/sharp with angular project

I'm trying to use lovell/sharp with Angular universal project, but I have an issue.
I build the server part via webpack and I added sharp to webpack's externals, but I got this error Unexpected character '�' (1:2)
so, I added node-loader to webpack, it builds the project and generates dist folder that contains main.js and sharp-*.node files
but when I run node dist/main I got this error:
Error:
Something went wrong installing the "sharp" module
node-loader:
Error: The specified module could not be found.
//sharp-ac5709806d227d1d966f7f0b76814d78.node
minimal reproduction:
https://github.com/eng-dibo/ng-sharp-issue
1- install dependencies npm install
2- build the server npm run build:server:dev
3- serve node dist/main
to check the webpack configurations, check this file './webpack.server.config.js`
issue solved
the problem is that the webpack plugin webpack-node-externals cannot work with monorepos, in this case it fails to get the correct path of package.json file, results in node modules don't be excluded.

Loading module installed directly from github repo with npm

I installed a public github repo using the syntax provided in the repository's readme:
npm install https://github.com/user_name/proj_name.git
However, I'm not sure how to load this module into my js code. I've tried:
const my_lib = 'proj_name';
Unfortunately, this is not working for me. How can I load a module that was installed directly from a github repository?
Currently you're just defining a constant as a string with value 'proj_name'.
To load a module from node_modules, you have to do the following:
npm install <package_name> --save, where --save write the package and version in your dependencies of package.json. You can also use --savedev to write the package in your devDependencies (both optional).
Use const packageName = require('packageName'); in your e.g. app.js to use the package in your code.
See here for more details about npm install in general, specifying-dependencies and here for ecma script require and import.

Parent React App linting transpiled dependency code that is connected via `npm link`

I have a React app that has a dependency on another React component from a library that I created.
When I run the build script on the Parent, and the dependency is installed via npm install, the dependency is not linted in the Parent App.
But when I run npm link so I can develop the component locally, and have my Parent App run it's build script, it fails due to the fact it is linting the transpiled code of the dependency.
Any ideas why this is happening?
UPDATE
It seems to have something to do with the name of the directory where the final bundle ends up. I named it bin. Reading through the npm docs it seems that npm link creates a symlink for the directory of the module, as well as for the bin directory, if there is one. I simply changed the name to dist and it resolved the issue. Not the best answer, but hopefully it can help someone in my same situation.

Resources