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.
According to the documentation, it is possible to launch a program before debugging:
To launch a task before the start of each debug session, set the preLaunchTask to the name of one of the tasks specified in tasks.json.
I've not seen example syntax of a "named" task, but the schema documentation reveals a property called taskName. I tried using that to link my launch.json preLaunchTasks to the task, but it didn't work. When I launched my program, Visual Studio Code reported this error:
Could not find a unique task 'launch-core'. Make sure the task exists and that it has a unique name.
My custom "named" task looked something like this:
{
"taskName": "launch-core",
"version": "0.1.0",
"command": "C:\\utils\\mystuff.exe",
// The command is a shell script
"isShellCommand": true,
// Show the output window only if unrecognized errors occur.
"showOutput": "silent",
}
I then tried changing the property name from taskName to just name, based on this link. That also didn't work.
Intellisense gives no suggestions of how to name a task.
Does anybody know how to uniquely name a task in the tasks.json file? What is the syntax? What is the property name?
Ultimately I'd like to execute two or three node.js processes before my own node.js app is launched. For example, I'd like to have the following three apps launched before my app is launched into the debugger:
sh -c 'cd ./manager/ && node manager.js'
sh -c 'cd ./adapter/ && node adapter.js'
sh -c 'cd ./core/ && node core.js'
If I'm working on a Windows box, my task might look something like this:
{
"taskName": "core-launch",
"version": "0.1.0",
// The command is tsc. Assumes that tsc has been installed using npm install -g typescript
"command": "start",
// The command is a shell script
"isShellCommand": true,
// Show the output window only if unrecognized errors occur.
"showOutput": "silent",
// args is the HelloWorld program to compile.
"args": [
"ACD-Manager",
"/B",
"/D",
"./manager/",
"node",
"manager.js"
]
}
The above task using using the cmd start capability. I'm not sure yet how to make several node tasks launch instead of one, but I can't even get one task to launch because of this task-naming issue.
How do I name a task in the tasks.json file?
FWIW, I'm using VS Code 1.20.1 and here's how I got my preLaunchTask to work:
In launch.json:
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
...
"preLaunchTask": "npm: build",
}
]
}
In my package.json:
{
...
"scripts": {
"build": "tsc"
...
}
}
So, if it's still relevant, or if someone finds this thread with the same problem, I've just figured it out how it works:
In the tasks.json, you need to create a 'tasks' array - code hint will help you with that - which holds an array of objects. Inside an object, you can have the 'taskName' key-value pair.
Example:
{
"version": "0.1.0",
"command": "npm",
"isShellCommand": true,
"args": ["run-script", "webpack"],
"showOutput": "always",
"tasks": [
{
"taskName": "runwebpack",
"suppressTaskName": true
}
]
}
In my case, I had to run the npm run-script webpack command before running my project.
In the launch.json file, the "preLaunchTask": "runwebpack" will work now.
Note: the suppressTaskName is true in my example. Omitting it, or setting it to false will result in VS Code appending the taskName after the command.
A more general approach would be something like this:
{
"version": "0.1.0",
"command": "npm",
"isShellCommand": true,
"args": ["run-script"],
"showOutput": "always",
"tasks": [
{ "taskName": "webpack" }
]
}
With the latter example, you can extend the tasks array with other scripts to be run also.
Hint for my usage: npm run-script fetches what to do from the package.json file's scripts object.
Edit: this works with VS Code 1.3.1
For version 2.0.0 configuration you now use label instead of taskName.
package.json:
...
"scripts": {
"tsc": "tsc",
...
}
...
launch.json (My source is in the src directory and tsc compiles to the dist directory):
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"preLaunchTask": "Compile",
"name": "Launch Program",
"program": "${workspaceFolder}/src/index.ts",
"outFiles": [
"${workspaceFolder}/dist/**/*.js"
],
"protocol": "inspector",
"sourceMaps": true
}
]
}
tasks.json:
{
"version": "2.0.0",
"tasks": [
{
"label": "Compile",
"type": "npm",
"script": "tsc",
"problemMatcher": []
}
]
}
For vscode 1.36.1 (1.36.1):
tasks.json:
{
"version": "2.0.0",
"tasks": [
{
"label": "build:tsc",
"type": "npm",
"script": "build:tsc"
},
{
"label": "clean",
"type": "npm",
"script": "clean"
},
{
"label": "build",
"dependsOrder": "sequence",
"dependsOn": ["clean", "build:tsc"]
}
]
}
launch.json:
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"program": "${workspaceFolder}/dist/main.js",
"preLaunchTask": "build",
"outFiles": ["${workspaceFolder}/dist/**/*.js"],
"console": "integratedTerminal"
}
]
}
Before running node ${workspaceFolder}/dist/main.js, the preLaunchTask will run build task firstly which includes two subtasks: clean and build. It will run clean task firstly, then run build task.
You can specify the label of a task to preLaunchTask option.
The question title is:
"Using “preLaunchTasks” and Naming a Task in Visual Studio Code
I needed to define preLaunchTask***s***.
You can config multiple tasks using the dependsOn property described here
For example, a compound task in your tasks.json:
{
"version": "2.0.0",
"tasks": [
{
"label": "Client Build",
"command": "gulp",
"args": ["build"],
"options": {
"cwd": "${workspaceRoot}/client"
}
},
{
"label": "Server Build",
"command": "gulp",
"args": ["build"],
"options": {
"cwd": "${workspaceRoot}/server"
}
},
{
"label": "Build",
"dependsOn": ["Client Build", "Server Build"]
}
]
}
You can find more information about naming tasks here.
I've only really seen the taskName used in relation to Gulp; I'm sure there are others but nothing that I have much insight into. Perhaps this can get you off to a start with what you already have?
Run a pre-launch task in VSCODE
On the Site bower.io,it says "There must be a valid manifest JSON in the current working directory." What does that mean? What is a manifest JSON?
This is referring to the bower.json file. This is a a sample of that:
{
"name": "my-project",
"version": "1.0.0",
"main": "path/to/main.css",
"ignore": [
".jshintrc",
"**/*.txt"
],
"dependencies": {
"<name>": "<version>",
"<name>": "<folder>",
"<name>": "<package>"
},
"devDependencies": {
"<test-framework-name>": "<version>"
}
}