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}]'
]
Related
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.
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).
Hi everyone I'm beginner in nodejs .I want to test some basic functionality in my code.I m using JEST testing framework.In the command prompt I used npm test I could not test my code it shows
npm ERR! Test failed. Can anyone solve this issue? Thanks in advance...
lib.test.js:
const lib=require('../lib.js');
describe('absolute',()=>{
it('It should return positive number if given number is positive',()=>{
const result=lib.absolute(1);
expect(result).toBe(1);
});
it('It should return positive number if given number is negative',()=>{
const result=lib.absolute(-1);
expect(result).toBe(1);
})
it('It should return zero if given number is zero',()=>{
const result=lib.absolute(0);
expect(result).toBe(1);
});
});
lib.js:
// Testing numbers
module.exports.absolute = function(number) {
if (number > 0) return number;
if (number < 0) return -number;
return 0;
}
package.json:
{
"name": "testing-demo",
"version": "1.0.0",
"description": "",
"main": "db.js",
"directories": {
"test": "tests"
},
"dependencies": {},
"devDependencies": {
"jest": "^22.2.2"
},
"scripts": {
"test": "jest"
},
"keywords": [],
"author": "",
"license": "ISC"
}
I'm guessing the error you are seeing is
SecurityError: localStorage is not available for opaque origins
The default test environment for Jest is a browser-like environment provided by jsdom, which is required for testing code like React applications that are designed to run in the browser.
If you are going to use jsdom as the test environment then you need to set testURL in the Jest configuration, otherwise you will get the error seen above.
If your code is designed to run on node and you don't need a browser-like environment then you can set your test environment to be node.
The easiest way to do that in your case is to pass it as a command line argument to jest in your package.json:
"scripts": {
"test": "jest --env=node"
},
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