Webpack can not resolve baseUrl in node modules package - node.js

So I have a node package I am developing with typescript and react where I set the baseUrl in tsconfig.json to the src folder so I can use absolute paths like src/module. The application using this node package (also react) uses webpack and also has its own tsconfig so when I import this module and run rpm run webpack, I get this error
Module not found: Error: Can't resolve src/module in /node_modules/path_to_module
I have tried to use path aliases and combine with tsconfig-paths-webpack-plugin but this packages seems to only work if the path configuration (tsconfig) and webpack config is within the project.
I understand the issue here is since the tsconfig in the node package is not same as tsconfig webpack would use when running, it won't point to the baseUrl in the package
So, is it possible for me to set baseUrl in tsconfig of a node package and run rpm run webpack in another project using the node package that webpack knows to use the baseUrl in packages in node_modules

Related

npx MODULE_NOT_FOUND when hardcoding dependency name

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?

google-auth-library has missing dependencies when installed

I am trying to use google-auth-library with a react app but am getting various errors which seem to indicate missing dependencies. The errors are all in a format similar to this:
Module not found: Error: Can't resolve 'buffer' in '/private/tmp/minimal-example/node_modules/buffer-equal-constant-time'
The error messages include details on how to add a polyfill for the missing dependencies, but even after going through and adding all these polyfills I am unable to use the library. The logs for the development server still show errors for missing files, and the browser console has the following error:
GET http://metadata.google.internal./computeMetadata/v1/instance net::ERR_NAME_NOT_RESOLVED
Steps to reproduce:
Install Node 16.13.2
Create a react app npx create-react-app minimal-example
Enter directory for the created project cd minimal-example
Install google-auth-library npm install google-auth-library --save
Add the following code to ./src/App.js
const {GoogleAuth} = require('google-auth-library');
Start the development server npm start
Observe errors
What am I doing wrong?
I Have the same problem with same google-auth-library package!
This is a problem with using webpack 5 on React scripts=> v5
Have three solutions to resolve:
downgrade react-scripts to version 4.0.3. on package.json file.
Overwrite the config webpack config, but, i will need install react-app-rewired to do this, because react app not have webpack.config on tree app files.
you can eject the script to root folder executing npm run eject, but caution, this action is permanent, more here: https://stackoverflow.com/a/48395890
Some people answered this problem in How to Polyfill node core modules in webpack 5
and https://github.com/facebook/create-react-app/issues/12072

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.

How to have environment-specific npm load paths?

I have some node.js libraries that I package as modules and install by npm install --save. This is a pain to debug, because I have to publish a module, then update to use the newer version.
I see on this SO question: how to specify local modules as npm package dependencies that I can specify my dependencies as paths, which is much better.
Is there a way I could create a debug environment that uses paths for mudule-loading, and then a release environment that will use the modules from npm?
In this case the 'release' is created by the command pkg -t win index.js. i.e.
I'd like to develop with relative paths to modules
I'd like to 'switch' to using modules from npm without changing strings anywhere.
Then. If the above is possible, I would like to NOT import the module file to node_modules, but run it in place (I'm trying to take advantage of VS Code debugging)

Typescript Definitions from Node Modules

I am trying to configure typescript with my tsconfig.json so it does find definitions inside node_modules.
Ive read that it should work if i set the module to commonjs despite excluding node_modules.
I tried to remove the exclude. Tried to include manually but nothing works.
How to configure typescript to find definition files installed with the source npm package?
Typescript#2.0 (Recommended)
You may use npm install. Example:
npm install --save #types/<your package name>
Typescript#1.x
Use tsd package: https://github.com/DefinitelyTyped/tsd
I also have node.js + typescript#2.0 + mongodb + passportjs example right there: https://github.com/thanhtruong0315/typescript-express-passportjs.

Resources