-
Notifications
You must be signed in to change notification settings - Fork 509
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Node cant find package.json file #573
Comments
Can you provide exact steps or script to reproduce this? |
Thanks for the attention. node --version => v8.2.1
THEN
|
I have this problem |
It appears you are using Webpack. Are you trying to use bcrypt in the frontend? It won't work there. bcrypt is a native module for nodejs. |
Im not using it in frontend, i havent frontend its a code to learn, im learning typescript/nodejs and my webpack.config.js has target: "node". There is a bundler comparison here: |
@Mark086 I understand your use-case. However, bundling your code will break native modules like this one. As a workaround, you can exclude some dependencies using the NodeJS native modules contains DLLs and glue code and the module initialization code loads them into the NodeJS process. For this we need the full path to the native module. We use a library called bindings to find the correct path to the native DLL. Webpack while bundling changes those expectations (namely, the initialization code and package.json are located in the directory) I'll take a look at the module loading code and see if it can be fixed. |
Thanks a lot for the attention, i will freeze this part of my exercise and will keep learning others, and thanks for try fix it too. |
I'm getting this error as well. I'm using Webpack to bundle my server side code though. |
@nukeop, As I wrote above, we need the package.json to locate the native counterpart to this extension. So any bundler will break this functionality. If you are using webpack, ignore this module using IgnorePlugin or Externals. I recommend the later. |
Yep, you are 100% correct. I made the comment above so that I'll be able to find this thread the next day and share my solution to this problem for anyone visiting it in the future. Here's the simplest solution if you want to bundle your server side code using webpack and use native modules:
This solves the problem. If you find that there are modules that this solution excludes that should be bundled, add them to the whitelist of nodeExternals. For example lodash requires this. |
Hi @agathver, ignoring bcrypt (or all node_modules) was not a solution when you need to bundle your backend code. So it was a showstoper for me too. I switched to https://github.com/dcodeIO/bcrypt.js#readme to solve the bundle problem. |
Hi everyone, my use case involve an offline installation with an RPM package, so bundling If you have a recipe that can bundle bcrypt without hassle, I'll be glad to read it :) |
@Glandos If your deployment is an RPM, just copy the In your CI or build machine, perform a clean install using |
I always appreciate advice, but this is not applicable to me. I have 200 hundreds servers, some of them behind a small DSL line. Transferring half of a gigabyte of data for just 8MiB webpacked script is a complete waste of resource. |
By the way, I tried ncc (https://github.com/vercel/ncc) with great success: it detects the pattern of |
Im very new to Node and is possible its just my lack of knowledge, in that case Im sorry, I opened the issue nodejs/node#18267 thinking it was a node bug but the issue grpc/grpc#13049 is telling: "The __dirname variable is explicitly documented as referring to the full path of the directory containing the current module file."
There is a comment of me after this block with a script to reproduce the project.
Below the detail steps to reproduce the problem:
__dirname => "/"
var binding_path = binary.find(path.resolve(path.join(__dirname, './package.json'))); /* node_modules/bcrypt/bcrypt.js [line:0005] */
/* INNER path.join(__dirname, './package.json') RETURNS '/package.json' */
/* <node_internals>/path.js [ {function=path.resolve} line:1174] */
resolvedPath => ""
path => "/package.json"
resolvedPath => ""
resolvedAbsolute => false
resolvedPath = path + '/' + resolvedPath; /* <node_internals>/path.js [line:1174] */
resolvedPath => "/package.json/"
path => "/package.json"
resolvedPath => "/package.json/"
resolvedAbsolute = path.charCodeAt(0) === 47/*/*/; /* <node_internals>/path.js [line:1175] */
resolvedAbsolute => true
resolvedPath = normalizeStringPosix(resolvedPath, !resolvedAbsolute); /* <node_internals>/path.js [line:1182] */
resolvedPath => "package.json"
"/package.json" <= return '/' + resolvedPath; /* <node_internals>/path.js [line:1186] */
/* node_modules/bcrypt/node_modules/node-pre-gyp/lib/pre-binding.js [ {function=binary.find} line:0015] /
/ binary.find execute against "/package.json" and throw error: "package.json does not exist at /package.json"
/* the problem looks happens because of lack of calling "process.cwd()" inside [ {function=path.resolve} line:1174] on the flow */
The text was updated successfully, but these errors were encountered: