Using a peer dependency in npm package - javascript

I have momentjs listed as a peer dependency in a npm package full of utility functions. One of these functions uses moment.
"peerDependencies": {
"moment": "2.x.x"
},
Here's a simplified look at where moment is being used:
import moment from 'moment'
export default function formatDate(someDate) {
return moment(someDate).format('MM/DD/YYYY')
}
My package.json looks like this:
"main": "dist/index.js",
"types": "dist/index.d.ts",
"files": [
"dist"
],
"scripts": {
"build": "tsc",
"publish-free": "npm publish --access public"
},
When I run "npm run build" I get an error in the terminal saying "Cannot find module 'moment'".
I am currently testing the package by using npm link to connect it to my main project where I have moment installed but I can't get past the build stage.

Related

Call JS package as npm script into another package

I have a package Package2 that depends on Package1.
Package 1:
consists of the following files (for this example)
package.json
index.js
/bin/package1 (no extension)
Package 1 package.json:
...
{
"name": "package1",
"version": "0.0.1",
"description": "a JavaScript library that should be called by other stuff",
"main": "index.js",
"scripts": {
"bin": "cli.js"
},
...
Package 1 /bin/package1 script:
#!/usr/bin/env node
const package1 = require('../');
// just import the package1 module an it should run without function call
Package 2 is a package that depends on package1.
Package 2 package.json:
...
"name": "package2",
"version": "1.0.0",
"description": "a JavaScript library that depends on package1",
"main": "index.js",
"scripts": {
"serve" : "package1 ."
},
...
But I'm getting an error when I run yarn serve:
yarn run v1.22.17
$ package1 .
'package1' is not recognized as an internal or external command,
operable program or batch file.
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
I've tried a lol of different things, but I can't get it to work.
You will need to add this after the "scripts" tag into package 1's package.json:
...
"scripts": {....},
"bin": {
"package1": "bin/package1"
},
...
and that's it

How to make npx work with my custom npm pakage?

I've published a new package which aims to generate a very small boilerplate for a node open source project via cli. For now, it's just a combination of few npx commands and requires other npm packages like gitignore, license to work. I want to execute the build script in package.json with the following command.
npx get-set-node-oss build youremailaddress#xyz.com
Link to the npm package: get-set-node-oss. I know the name is a bit too long.
{
"name": "get-set-node-oss",
"version": "1.0.1",
"description": "One command setup for your Node OSS project",
"scripts": {
"build": "npx license mit > LICENSE && npx gitignore node && npx covgen"
},
"author": "Harshit Juneja",
"license": "MIT",
"repository": {
"type": "git",
"url": "git+https://github.com/harshitjuneja/get-set-node-oss.git"
},
"keywords": [
"node",
"boilerplate","gitignore","MIT","OSS"
],
"bugs": {
"url": "https://github.com/harshitjuneja/get-set-node-oss/issues"
},
"homepage": "https://github.com/harshitjuneja/get-set-node-oss#readme"
}
I expect the user to make a new folder and cd into the folder and do
npx get-set-node-oss build emailstring
and get the resulting boilerplate files.
For npx to work you need to specify what the get-set-node-oss command means. Thankfully, this can be accomplished by using the bin field in your package.json file. More information from NPM documentation for your reference:
To use this, supply a bin field in your package.json which is a map of command name to local file name. On install, npm will symlink that file into prefix/bin for global installs, or ./node_modules/.bin/ for local installs.
An example of it below.
"bin": {
"get-set-node-oss": //script you like to run
},
Hope it works out for you. I have understood bin as scripts in package.json. Where we can either do npm run foo or npx foo both yielding the same results. If you want an example, here you go https://github.com/vipulgupta2048/balenaclone

npm ERR Refusing to install package with name <packageName> under a package also called <packageName>

I am trying to use the jsonfile package in my project, but I get the following errors:
Refusing to install package with name <packagename> under a package also called <packagename>. (Note that none of the directory or filename is same as package name)
Cannot find module <packagename>.
The problem is caused when the name of project in package.json is the same as the module you're trying to install.
To solve this problem, please change the project name in package.json to something else. For example, "jsonfile-test":
{
"name": "jsonfile-test",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
}
}
I think it should be:
npm install --save json-file
not
npm install --save jsonfile
ref: https://www.npmjs.com/package/json-file
If that does not work please try enabling permissions install - e.g. (on mac- but will be equivalent on windows, check: https://helpdeskgeek.com/free-tools-review/5-windows-alternatives-linux-sudo-command/) run:
sudo npm install --save json-file

Module 'jest-junit' in the testResultsProcessor option was not found

I have setup jest and jest-junit as the reporter and followed the simple instructions given by jest-junit.
This includes npm install jest --save-dev and npm install jest-junit --save-dev
My package.json looks like this (excerpt):
"devDependencies": {
"jest": "^22.4.4",
"jest-junit": "^4.0.0",
},
"scripts": {
"test": "jest --ci --testResultsProcessor='jest-junit'"
},
"jest": {
"verbose": true,
"testResultsProcessor": "jest-junit"
},
"jest-junit": {
"suiteName": "Test Suite",
"output": "./junit.xml"
}
When running npm run test on my machine (OSX), it works well. When running it as part of the CI build process or on another Windows machine, I am getting the following error:
Module 'jest-junit' in the testResultsProcessor option was not found.
Maybe you just need to install the missing module to the other machine:
npm install jest-junit
Found the solution and it was the removal of inverted commas.
"test": "jest --ci --testResultsProcessor='jest-junit'"
should become
"test": "jest --ci --testResultsProcessor=jest-junit"

How to run a script before installing any npm module?

I am trying to make a test for npm packages for my project such that every time I try to install a module i.e run npm install <module> a script must be run before that module is installed. The preinstall script only works with npm install and not with npm install <module>.
eg :- If run npm install request . It should run a script which shows me all the dependencies of the request module before installing the module. Thanks in advance.
Add "install": "[Your Command]" in the script part of your package.json
Example:
{
"name": "test",
"version": "1.0.0",
"description": "A sample test",
"main": "index.js",
"scripts": {
"install": "echo Test"
}
}
You can also use a pre hook with "preinstall": "[Your Command]"

Categories

Resources