node.bcrypt.js: Node cant find package.json file
Im very new to Node and is possible its just my lack of knowledge, in that case Im sorry, I opened the issue https://github.com/nodejs/node/issues/18267 thinking it was a node bug but the issue https://github.com/grpc/grpc/issues/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 */
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Comments: 15
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:
target: 'node'externals: [nodeExternals()]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.
@Glandos If your deployment is an RPM, just copy the
node_modulesfolder verbatim. You should not bundle NodeJS applications using something like webpack is an invitation to trouble. Not just bcrypt, bundling is not compatible with any module that uses native code, likepgormongodbIn your CI or build machine, perform a clean install using
npm ciand then copy the resultingnode_modulestogether with the code. I use this method to run code using bcrypt and other native modules in production where our standard deployment is with RPM for a fleet of VMs.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.