Module installed from npm can't find it's local files - node.js

I published a small package to npm which consists of several files.
The index.js of the package just imports and re exports the public classes and APIs/
When I install the package from NPM I get the following error when I try to use the custom package:
Error: Cannot find module './openapi'
Require stack:
- .../node_modules/.../dist/index.js
I checked dist folder in the module dir in node_modules and the files are there.
I assume there is an issue with the relative path inside the node_modules
One option to fix this is to bundle everything into a single file, but I would prefer to keep multiple files.
What options are there to fix this ?

.gitignore files are ignored. There were after all some ignored local files that needed a to be pushed.

Related

Nest.js error with ENOENT: no such file or directory, open './push-service-account.json'

I tried to find a solution to the problem, I don’t think that the option to remove package-lock.json is suitable for me, since I install dependencies from it using npm ci.
In project i have Node: 12.22.12
npm: 6.14.16
enter image description here
Looks like in your PushService you are trying to read ./push-service-account.json. This may look fine in your src directory, but when you compile, Typescript won't move the json file for you. You either need to use a post-build step that moves all .json files you plan to read to the relative dist location, or you should put all of these .json files in a common directory relative to your project root so that you can use fs.readFile(path.join(process.cwd(), 'common', fileName))

Why is my Node.js package missing the main file when installed globally?

This feels like an embarrassing question to ask but having recently published a Node package to the NPM registry, I now find it doesn't work.
The issue seems to be that my main file, ./src/index.js, isn't being included in the global install.
I know this because when I call the package from the command line it
runs ./bin/cli.js in the package as expected, but then throws:
Error: Cannot find module '../src/index.js'
Require stack:
- /usr/lib/node_modules/diffcraft/bin/cli.js
The error even references the line in ./bin/cli.js where the index
file is required, so that's definitely where the problem is.
I also know this because I checked the folder where the module is
installed globally and while the bin folder is there, the src
folder isn't. So the main code for my package just isn't there.
After discovering this, I even patched package.json to ensure that ./src/index.js was explicitly whitelisted in the files array. I hadn't done this before as NPM guidance states that whichever file is listed under main is also automatically whitelisted. But even including the file in files explicitly hasn't worked.
For reference, I don't have an .npmignore file.
I've got a horrible feeling I'm missing something simple and basic... Any ideas why my main file might be being skipped?
The package is diffcraft.
It works if you omit the ./ in front of the files (tested with npm 6.14.4 on Windows):
"files": [
"bin/cli.js",
"src/index.js"
],
This might be a bug in npm.
You can check this without publishing by running npm pack and checking the archive file.
Alternative is using an .npmignore file.

NodeJS: installing dependencies from package.json located one level above the working folder

The project I need to work upon is structured in a way that the package.json file is one level abov the code folder. The code is located at "D:\MyNodeProj\Source\"
All the files and code is located under "Source" project including "node_modules" folder. Package.json file is located out of "Source" folder. It is located at "D:\MyNodeProj\package.json" When I try to run the project it gives an error: Cannot find module 'winston-logzio'
I know this has something to do with package.json's location as winston-logzio's entry is there in package.json
I tried npm install but it is also throwing error. It used to work before when package.json and node_modules folder were on same level. Now that package.json is one level up we are haviing all sorts of trouble.
What is the fix for this? Please note, we can not change the location of package.json now Don't know for what reason but the architect want it this way.
If you use prefix for the parent directory, you will be able to use npm install --prefix=./../ in the project folder (D:\MyNodeProj\Source\), this command will install node_modules in the parent directory(D:\MyNodeProj\). But in order to make the node_module visible to your project you can set NODE_PATH to the parent directory and then run the project.

how to make webpack use shared node modules in the parent directory

I want to use shared node modules which is kept in the parent directory. I have the following folder structure:
parent directory
node_modules
company
serverless.yml
handler.js
webpack.config
I have to use webpack to create packages and upload into aws-lamda. But the webpack is looking for node modules. But I am getting the following error:
Overriding the webpack directory in serverless-webpack since it uses v1 version of webpack...
cp: cannot stat 'node_modules/webpack': No such file or directory
How can I make webpack.config point to the node_modules in the parent directory?
if you read the NodeJS require documentation you are highly encouraged to use local node_modules folders even if your packages require have the same dependencies.
https://nodejs.org/api/modules.html#modules_loading_from_node_modules_folders
Alternatively your can work with NODE_PATH , notice that require will first look into local node_modules then start looking into other folders so try to delete local node_modules
If the NODE_PATH environment variable is set to a colon-delimited list of absolute paths, then Node.js will search those paths for modules if they are not found elsewhere. (Note: On Windows, NODE_PATH is delimited by semicolons instead of colons.)
Since webpack exposes many module resolving options https://webpack.github.io/docs/resolving.html
Resolving a module path
For resolving a module Resolve first gathers all search directories for modules from Webpack’s context directory. This process is similar to the node.js resolving process, but the search directories are configurable with the configuration option resolve.modulesDirectories. In addition to this the directories in the configuration option resolve.root are prepended, and directories in the configuration option resolve.fallback are appended.

npm install "no such file or directory '.../package.json'" for custom modules

I have some folders symlinked to node_modules in order to be able to import them as modules. For example, I have src/client/apps/admin directory which is symlinked to node_modules/#admin. But npm gives me these warnings when I'm trying to install or remove any modules
$ npm i -S connect-roles
...
npm WARN ENOENT ENOENT: no such file or directory, open '/Users/Admin/Code/www/learn/src/client/apps/admin/assets/package.json'
npm WARN ENOENT ENOENT: no such file or directory, open '/Users/Admin/Code/www/learn/src/client/apps/admin/components/package.json'
npm WARN ENOENT ENOENT: no such file or directory, open '/Users/Admin/Code/www/learn/src/client/apps/admin/reducers/package.json'
npm WARN ENOENT ENOENT: no such file or directory, open '/Users/Admin/Code/www/learn/src/client/apps/admin/package.json'
Of course, I can just add package.json to every such folder but I don't want to. There has to be another better way to do that. Is there?
PS: npm v3.5.3
You are trying to use nodes module require/import in a way that is bound to cause you problems later on. It would be much better to structure your project in a way that does away with these symlinks.
When importing a module you can specify a core module, a module installed inside your projects node_modules, or a relative path:
Core modules (eg require('http'))
Core modules are included as part of node and can be imported simply by name.
Installed modules (eg require ('bluebird'))
Modules that have been installed via npm, and are inside your projects _node_modules_ directory, can be installed using the name declared in their package.json.
Relative paths (eg require('../settings/menu'))
Modules (that could be single files with no package definition) can be imported using a path relative from the importing file.
Because you are not providing a package.json, npm is failing because it's unable to lookup the name property of each package. You will be much better off if you simply require these files using a relative path.
For instance, if your project looks something like:
apps
│
└───admin
│ assets.js
│ components.js
| reducers.js
│
├───subfolder
│ │ thing.js
You can import one file to another with a relative path.
Eg, in components.js:
require ('./assets')
Or in thing.js:
require ('../assets')
If you have a lot of very nested paths, this can become a little cumbersome, but there are a number of suggested ways this can be dealt with. One method is to use path.resolve() which will resolve the relative path, given a path from the project route.
For example:
require ('../assets')
could be re-written as
require (path.resolve('app/admin/assets'))
This has the nice result of all your require paths being 'absolute' from your project root.
You can read more suggested ways to deal with this in Better local require() paths for Node js
None of the answers helped me to solve the problem so I created my own package module-alias, which is highly inspired by app-module-path. This package allows registering aliases of directories for further usage with require/import.
Example:
const moduleAlias = require('module-alias')
moduleAlias.addAliases({
'#root' : __dirname,
'#server': __dirname + '/src/server'
})
const someModule = require('#server/some-module')
Read the readme for more examples
Try this command:
npm install -g npm-autoinit

Resources