I have a project (node.js 14 + npm 6) that has a dependency let's call it A.
A also has a dependency called B.
After I run npm install the package lock file has B under A as a transient dependency to my project:
{
"name": "test",
"version": "1.0.0",
"lockfileVersion": 1,
"dependencies": {
"A": {
...
"dependencies": {
"B": {
...
},
}
},
}
}
However, if I delete the node_modules folder and run npm install lock file will change to this:
{
"name": "project",
"version": "1.0.0",
"lockfileVersion": 1,
"dependencies": {
"A": {
...
},
"B": {
...
},
}
}
Both A and B are on the same level in the tree structure. Nothing has been changed in the package.json and all other files remained the same between the two installs.
My questions are:
Why is it happening?
Is it a normal thing to happen?
Doesn't this makes collaboration cumbersome? Every time someone touches npm i the packages lock file changes.
Related
I am trying to pass a variable in npm per Solution 1 here How to use npm config variables of package.json in npm scripts but cannot seem to get it right. I have tried several variations of the below code to no avail. Can someone tell me the right way to do it?
UPDATE:
I had actually started from a point closer to that mentioned by xehpuk (below) but I cannot retrieve the variable from there either. I have updated the code below to reflect that
SOLUTION:
This works!
test.js
const main = function () {
console.log(process.argv)
}
main()
package.json
{
"name": "test",
"version": "0.1.0",
"description": "testing",
"dependencies": {
},
"config": {
"params": "{\"height\": 33, \"width\": 22}]"
},
"license": "",
"scripts": {
"return": "node test.js --params=$npm_package_config_params"
}
}
result
[
'/home/dcode/.nvm/versions/node/v18.3.0/bin/node',
'/mnt/d/bin/tools/test.js',
'--params={"height":',
'33,',
'"width":',
'22}]'
]
A working solution is posted above!
You can access it via process.env.npm_package_config_params.
package.json > config | npm Docs
process.argv works for me with:
{
"config": {
"params": "{\"height\": 33, \"width\": 22}]"
},
"scripts": {
"return": "node test.js \"$npm_package_config_params\""
}
}
Result:
$ npm run return
> test#0.1.0 return
> node test.js "$npm_package_config_params"
[
'/usr/bin/node',
'/home/xehpuk/test.js',
'{"height": 33, "width": 22}]'
]
I’m looking for a way to use a local package (named bs-package) from my Reason React app (named ApplicationA).
bs-package has a single file within src folder called ModuleA.re :
let greet_me = (me) => Js.log("Hello, " ++ me ++ "!");
In ApplicationA I did :
npm install /path/to/bs-package
In node_modules bs-package now appears as symbolic link.
Then added bs-package to bs-dependencies in bsconfig
"bs-dependencies": [
"reason-react",
"bs-package",
],
Then run make world in ApplicationA
bsb -make-world
Now in my Reason React app
ModuleA.greet_me("John");
Gives me
unbound module ModuleA
What am I missing ?
EDIT
Here is the config file of bs-package
It was created with
bsb -init bs-package -theme basic-reson
bsconfig.json
{
"name": "bs-pacakge",
"version": "0.1.0",
"sources": {
"dir" : "src",
"subdirs" : true
},
"package-specs": {
"module": "commonjs",
"in-source": true
},
"suffix": ".bs.js",
"bs-dependencies": [
],
"warnings": {
"error" : "+101"
},
"namespace": true,
"refmt": 3
}
package.json
{
"name": "bs-pacakge",
"version": "0.1.0",
"scripts": {
"build": "bsb -make-world",
"start": "bsb -make-world -w",
"clean": "bsb -clean-world"
},
"keywords": [
"BuckleScript"
],
"author": "",
"license": "MIT",
"devDependencies": {
"bs-platform": "^5.0.6"
}
}
As suggested, bs-package is namespaced so this should work
BsPackage.ModuleA.greet_me("name")
But the same error appears
unbound module BsPackage
Apparently the bug came from my bs-platform version. I've just switch from version 6.2.1 to version 7.0.1 and everything works fine now (I'm on Windows).
I am developing a module that I try to make as generic as possible (let's say BaseModule), and I want to extend it to feet my current purposes in another module (let's say ChildModule).
So ChildModule begins with:
require('BaseModule');
So I added BaseModule as a dependency of ChildModule. But when I want to use both of them on my real project, I cannot install both of them. Installing BaseModule removes ChildModule, and installing ChildModule moves BaseModule, in a way it is unreachable in my code afterward.
Both the module are on a git repository only (ie not published yet).
What is funny is that mongoose is also a dependency of ChildModule, and I don't have this issue. So I think I forgot something in BaseModule, but I don't know what...
It is a bit similar to this issue NPM dependencies shared by dependencies, which is supposed to be fixed on my NPM version (5.6). This issue is also similar, but as I said before I don't think it is a bug.
Is there any way to have nested dependency? If no, what is the standard way to do this kind of extension?
Here are the different package.json:
BaseModule's package.json
{
"name": "BaseModule",
"main": "index.js",
"repository": {
"type": "git",
"url": "git+ssh://.../BaseModule.git"
},
"bundledDependencies": false,
}
ChildModule:
{
"name": "ChildModule",
"main": "index.js",
"repository": {
"type": "git",
"url": "git+ssh://.../ChildModule.git"
},
"bundledDependencies": false,
"dependencies": {
"mongoose": "^4.13.9",
"BaseModule": ".../BaseModule"
}
}
and the project itself:
{
"name": "Project",
"dependencies": {
"mongoose": "^4.13.9",
"ChildModule": ".../ChildModule",
"BaseModule": ".../BaseModule",
"andALotMore": "*"
}
}
... stands for the correct address of my repositories.
Use of the npm config section is simple, and cool, but I've run into one restriction with it: a config entry does not get expanded, so one cannot chain them, nor even access non-config values s.a. package version within the config.
Sample:
{
"name": "myproj",
"version": "0.1.2",
"//": "Here, '$npm_package_version' is not expanded",
"config": {
"target": "dist/myproj-$npm_package_version.js"
},
"scripts": {
"echo": "echo $npm_package_config_target",
}
}
This gives:
dist/myproj-$npm_package_version.js
instead of:
dist/myproj-0.1.2.js
Is there anything I can do about it? Chaining values like this is a useful feature - I'm surprised nom doesn't do it. Is there a reason not to?
References:
Node.js: How to setup different variables for prod and staging
How to use npm as a build tool
There's a few of ways this could be handled.
The simplest way is probably just to eval the string
{
"name": "myproj",
"version": "0.1.2",
"config": {
"target": "dist/myproj-$npm_package_version.js"
},
"scripts": {
"echo": "eval echo $npm_package_config_target"
}
}
If you're not comfortable with eval then you could either manually concat the string within the script:
{
"name": "myproj",
"version": "0.1.2",
"config": {
"target_prefix": "dist/myproj-",
"target_suffix": ".js"
},
"scripts": {
"echo": "echo $npm_package_config_target_prefix$npm_package_version$npm_package_config_target_suffix",
}
}
Or you could do away with using npms config and write a quick script instead, which can be composed with others:
{
"name": "myproj",
"version": "0.1.2",
"scripts": {
"config:target": "echo dist/myproj-$npm_package_version.js",
"echo": "echo $(npm run config:target)"
}
}
I have created a npm package, Here is the package.json:
{
"name": "docxgen",
"version": "1.0.5",
"author": "Hans Thunam <hans.thunam#gmail.com>",
"description": "A docX Generator",
"contributors": [
{
"name": "",
"email": ""
}
],
"bin":
{
"docxgen":"./docxgenNode/bin/docxgen"
},
"keywords": [
"DocX",
"templates",
"Generator"
],
"dependencies" : {
"xmldom" : "0.1.x"
},
"devDependencies": {
"jasmine-node":"1.10.x"
},
"license": "MIT",
"engines": {
"node": ">=0.8"
}
}
Then I did sudo npm install -g in the folder of package.json, the installation worked correctly (no error nor warnings).
Howewer, when I then try to execute docxgen file.docx file.json, I get the response: File or command not found. What is strange about this is that it works on Windows 7.
Is there anything different to do for Ubuntu ?
The issue was about the character encoding of the ./docxgenNode/bin/docxgen.
The shell system didn't read the first line correctly.
I started from an existing working binary file that can be found here: https://github.com/LearnBoost/stylus/blob/master/bin/stylus