Prepublish not working as expected - node.js

I am testing on npm scripts to build my project dependency.
My idea comes from https://github.com/ParsePlatform/parse-server which impressed me by code in repository doesn't mean code in node_modules after npm install.
Below is my testmodule structure
src/index.js
package.json
and this is my package.json content
{
"name": "testmodule",
"version": "1.0.0",
"description": "",
"main": "lib/index.js",
"scripts": {
"build": "babel src/ -d lib/",
"prepublish": "npm run build"
},
"devDependencies": {
"babel-cli": "^6.18.0",
"babel-core": "^6.18.2"
}
}
and this is structure I expect after run npm install testmodule
node_modules/testmodule/lib/index.js
node_modules/testmodule/package.json
which is src folder should not be here.
But after I run npm install, it is exactly the same as when I push to my git repository.
Please take note that I am using GitLab in my own server.
So my questions are:
Is there anything that i'm missing to make prepublish run?
Which part of parse-server code makes the src folder and other files not there after install?

How are you running npm install?
According to the documentation on npm scripts, the prepublish script is run "BEFORE the package is published. (Also run on local npm install without any arguments.)". It seems clear that the prepublish script is only run on npm publish or npm install <local directory>.
If you are trying to install directly from your local gitlab server via a URL, this will not work - the script will not be run. The solution would be to install locally unless you're willing to open source your package & push it to the npm repository or pay for a private npm repository. This is what I have done during development of packages before they're ready to be made public.

Related

NPM 7 workspaces - how to install new package in workspace?

If I have a NPM 7 workspace like this:
root
- submodule0
- submodule1
- submodule2
and I navigate to the submodule0 directory and run npm i somepackage it seems to "break" the workspace by creating a new package-lock.json in the submodule0 directory and installing all the dependencies there. In other words, it just does the old behavior that existed before I created the workspace.
I was hoping for a command similar to lerna where I can install a new package in submodule0 from the root. Something like:
npm i somepackage --scope submodule0
So far, the only workaround I can find is to edit the submodule0 package.json and add the somepackage manually. Then run npm i from the root. Obviously this is not ideal because I need to look up the #latest version, navigate to the subdirectory, open the package.json, etc. etc. as opposed to just typing one line in the root.
Workspace support for npm install and npm uninstall was added in npm v7.14.0. You can now just do:
npm i somepackage --workspace=submodule0
Uninstalling modules has been the biggest pain, so this is really exciting. The npm team seems to be slowly adding support to commands one by one. Follow updates here: https://github.com/npm/cli/blob/latest/CHANGELOG.md.
I'm also baffled with why npm workspaces has been released without this functionality.
My current workaround uses the add-dependencies package, which adds dependencies to a declared package.json file, whilst skipping the installation process.
npm i add-dependencies -g
Then, from top level of the monorepo, you can run:
npx add-dependencies ./submodule0/package.json somepackage && npm i
Hopefully a --workspace argument will be added to npm i soon to avoid this faff.
Please refer to the answer of mattwad above if you have NPM v7.14.0 or above
Original answer
I wasn't quite happy with the suggestions, but combined all of them to use it in a npm script without any dependencies:
{
"add": "npm install --package-lock-only --no-package-lock --prefix",
"postadd": "npm install"
}
This can be used like following: npm run add -- submodule0 somepackage
Add only into package.json
U can use this to install package only into package.json ( you don't need external dependencies )
npm i --prefix packages/test --save --package-lock-only --no-package-lock express
followed by npm i to install specified dependency into mono repository root node_modules
Lerna
Also can use lerna to use workspace name to install dependency into
package.json
{
"name": "mono",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"wsi": "function workspaceinstall() { ( scope=$1; shift; lerna exec --scope \"$scope\" -- npm install --package-lock-only --no-package-lock \"$#\") }; workspaceinstall"
},
"author": "",
"license": "ISC",
"workspaces": {
"packages": [
"packages/**"
]
}
}
lerna.json
{
"version": "1.0.0",
"npmClient": "npm",
"packages": ["packages/**"]
}
npm run wsi [workspace name] [dependency name to install]
npm run wsi #workspace/test express
npm run wsi #workspace/test express --save-prod
npm run wsi #workspace/test #types/express --save-dev
wsi script only modify package.json for provided workspace name, to actually install dependencies u have to run npm i
In my case, which is similar to yours, I deleted all dependencies from all the inner projects, deleted also the package-lock.json, and installed everything in the root.
/
node_modules
package.json >> all dependencies
package-lock.json >> the only lock file that exists in the repo
/packages
/A
package.json >> no dependencies
-- no package-lock.json
/B
package.json >> no dependencies
-- no package-lock.json
/C
package.json >> no dependencies
-- no package-lock.json
This way, the node_modules folder ONLY resides on the root, and also the package-lock.json file is in the root.
If I allowed to have each project it's own package-lock.json I started seeing installation and runtime errors (because each project could have its own node_modules and its own version of a dependency).
This is the best way I see it works.
After trying to use the npm install with the --prefix --save --package-lock-only --no-package-lock options, npm always give the the error E404 - Not Found for my own packages of the monorepo that are not yet published to a registry. So even when trying to install external packages it fails because of my current dependencies in the package.json.
To workaround this issue I ended up with a mix of the previous suggestions:
"scripts": {
"add": "add-dependencies $npm_config_scope/package.json",
"postadd": "npm i",
},
"devDependencies": {
"add-dependencies": "^1.1.0"
},
Then I can do:
npm run add --scope=packages/app express
npm run add --scope=packages/core eslint jest -D
This works fine for installing external packages. To install my own packages that lives inside the monorepo, I still have to manually edit the package.json, otherwise I get the package not found error.

Npm install from repo not running `prepare`

I have an npm package for common components hosted on an internal git server. For some reason when I call npm install in another project I want to consume this in it will not run the prepare hook. Obviously, this does not work since the npm package needs a /dist folder in node_modules to be able to consume the package.
I have already tried things such as using the deprecated prepublish hook and even that does not get called. I also tried to do postinstall to see if I could build after install, while that hook did get called it failed because the devDependencies were not installed
package.json
{
"name": "common-components",
"version": "0.1.0",
"scripts": {
"prepare": "npm run build",
"build": "ng build",
...
},
"private": true,
"dependencies": {
...
},
"devDependencies": {
...
},
}
command being used for install
npm install --save git+ssh://{URL-to-common-components-repo}}
I have read through the npm-scripts documentation https://docs.npmjs.com/misc/scripts thoroughly and it seems like they insist that prepare hook should always be called for this exact use-case
Updated 5/6/2019
Just as a note I found this bug on NPM community https://npm.community/t/using-npm-ci-does-not-run-prepare-script-for-git-modules/632/4.
I am using npm 6.4.1 which should work according to the bug
One thing to check that hit me on a package recently - if there is a .gitignore and not .npmignore npm may be ignoring your /dist folder. Adding an empty .npmignore worked in this case.
"If there’s no .npmignore file, but there is a .gitignore file, then
npm will ignore the stuff matched by the .gitignore file. If you want
to include something that is excluded by your .gitignore file, you can
create an empty .npmignore file to override it."
from https://docs.npmjs.com/misc/developers
For those that are wondering the status of this. I was unable to ever get it to work. What I ended up doing was hosting the components on a private npm registry and that works fine since the npm publish command will do the build and only publish the dist folder
If adding an empty .npmignore does not help, you can try specifying all files in dist explicitly in package.json#files. If this works you might want to consider using a wildcard pattern that matches the files in dist to simplify the package.json.
package.json
...
"files": [
"source",
"dist/cjs/main.js",
"dist/es/main.js"
]
}
see this comment to a similar issue in the npm/cli repository https://github.com/npm/cli/issues/1287#issuecomment-635021757
It's very likely that your dist/ folder is in your .gitignore file. According to this response from an npm-cli maintainer:
In order to be able to properly prepare a git repo npm will run the
extracted files through npm-packlist in order to get the expected
files that are going to be placed in your node_modules folder.
Further checking the documentation of npm-packlist, we find that npm-packlist will respect the .gitignore file if it has nothing else to go off of:
If there's no package.json with a files list, and there's no
.npmignore file, but there is a .gitignore file, then ignore all the
files in the .gitignore file.
This article further expands on the idea.
It seems to me that the best fix is to explicitly declare the files that your package needs (including dist/) in the files section of your package.json file. Then you have complete control over what's included and the package size is minimized.
If you are using root user to npm install the package then the prepare script might not be triggered. The reason was the prepare child process has no permission to run (user account was set to default of 'nobody' when using npm with root). You can read more here: https://github.com/npm/npm/issues/17346
To fix this, in the lib package, create an .npmrc file and add:
unsafe-perm: true
Adding main in the package.json fixed this issue for me.
"main": "./dist/index.js",
"scripts": {
"build": "babel src --out-dir dist",
"prepare": "npm run build",
"lint": "eslint ."
},
node v14.15.4 npm 6.14.11

NPM install doesn't trigger babel build if dependencies not coming from NPM

For example, if in my package.json, i have this:
"dependencies": {
"cacheman": "2.1.0" }
it works and it will trigger the building script inside cacheman when I do npm install.
however, if i do this:
"dependencies": {
"cacheman": "https://github.com/cayasso/cacheman.git" }
it won't work. npm install will not trigger the build process for cacheman.
why is that?
The script you are referring is pre-publish script which runs before publishing the npm module to npm registry. Check here package.json#L9
Extract shown here
"scripts": {
"test": "make test",
"prepublish": "make"
}
When you install it from github there is no publish step so the script is not run.
If you want to install from github only and have the script run, you can add it as postinstall script of cacheman (you will have to fork the repo to make changes if you are not owner of cacheman).
"scripts": {
"test": "make test",
"prepublish": "make",
"postinstall": "make"//Added postinstall
}
Check examples in npm scripts documentation for more details.

NPM: After "npm link" module is not found

I'm developing two modules for NodeJS, first one named aligator and second one aligator-methods. Second one depends on first one to work. I'm developing these two modules at the same time and I want to global link aligator so I can use it like it is on npm registry and I just installed it globally. To do this NPM documentation says that I need to use npm link but it's not working.
File package.json of module aligator:
{
"name": "aligator",
"version": "0.0.1",
"description": "",
"main": "index.js",
"private": true,
"directories": {
"doc": "docs",
"example": "examples",
"test": "spec"
},
"scripts": {
"test": "gulp jasmine"
},
"license": "MIT",
"devDependencies": {
"gulp": "^3.6.2",
"gulp-jasmine": "^0.2.0",
"gulp-jshint": "^1.6.1",
"gulp-rename": "^1.2.0",
"jasmine-node": "^1.14.3"
},
"dependencies": {
"bluebird": "^1.2.4",
"lodash": "^2.4.1",
"mathjs": "^0.22.0"
}
}
File package.json of module aligator-methods:
{
"name": "aligator-methods",
"version": "0.0.1",
"description": "",
"main": "index.js",
"private": true,
"directories": {
"doc": "docs",
"example": "examples",
"test": "jasmine"
},
"scripts": {
"test": "gulp jasmine"
},
"author": "",
"license": "MIT",
"devDependencies": {
"gulp": "^3.6.2",
"gulp-jasmine": "^0.2.0",
"gulp-jshint": "^1.6.1",
"gulp-rename": "^1.2.0",
"jasmine-node": "^1.14.3"
},
"dependencies": {
"lodash": "^2.4.1",
"mathjs": "^0.22.0",
"aligator": "^0.0.1"
}
}
First of all I linked the module globally:
$ cd ~/aligator
$ npm link
/usr/local/lib/node_modules/aligator -> /Users/roc/aligator
This if I'm not mistaken has created a global reference of my module aligator and now I can use this module from everywhere I want in the computer.
Then I went to the other module and tried to install the dependency but it gave me this output:
$ cd ~/aligator-methods
$ npm install
npm ERR! 404 404 Not Found: aligator
npm ERR! 404
npm ERR! 404 'aligator' is not in the npm registry.
npm ERR! 404 You should bug the author to publish it
npm ERR! 404 It was specified as a dependency of 'aligator-methods'
npm ERR! 404
npm ERR! 404 Note that you can also install from a
npm ERR! 404 tarball, folder, or http url, or git url.
npm ERR! System Darwin 13.2.0
npm ERR! command "node" "/usr/local/bin/npm" "install"
npm ERR! cwd /Users/roc/aligator-methods
npm ERR! node -v v0.10.28
npm ERR! npm -v 1.4.16
npm ERR! code E404
npm ERR!
npm ERR! Additional logging details can be found in:
npm ERR! /Users/roc/aligator-methods/npm-debug.log
npm ERR! not ok code 0
I even tried to link it directly with:
$ cd ~/aligator-methods
$ npm link aligator
/Users/roc/aligator-methods/node_modules/aligator -> /usr/local/lib/node_modules/aligator -> /Users/roc/aligator
But it didn't work either.
Any thoughts on what it is that could be happening? I read somewhere that maybe it had something to do with my installation of node and npm because it was made by Homebrew and so sometimes I need to use sudo, it seemed unlikely but I tried what they proposed and It didn't work either.
The problem was that the main property of package.json was pointing to a non-existing file. It seems that the problem can happen due to multiple reasons so be sure to take a look at other answers.
I ran into this issue because of NVM, I was running one version of node for the dependency and another for the dependant.
Deleting package-lock.json then running npm install again resolved the issue for me.
When you first run npm link from the aligator directory, you create a link from your global node_modules directory to aligator. Then when you run the npm link aligator from the aligator-methods directory, you link aligator from your locally installed node_modules to the original source (as the output shows in your example above). Once this is done, there shouldn't be a need to install anymore since it's already "installed". What errors are you seeing after you run the npm link aligator command?
If you just want to install a dependency from a local directory, you might just try using npm install instead. For example:
$ cd ~/aligator-methods
$ npm install ../aligator
My issue ended up being that repo A was using npm and repo B was using yarn, so I needed to run yarn link in repo B in order to pull it in via npm link package-name into repo A.
What worked for me was to:
Delete the node_modules in both the dependency and the consumer module.
Run npm unlink --no-save [dependency-module]
re-link with the 2-link commands as per npm-link
Now I am able to fully test my unpublished module locally.
Additionally, there is an npm pack command which can help you test your unpublished modules, although not quite as robust.
npm-pack
Fix for my version of this issue; in npm v5.3.0, I removed node_modules from repo I was linking into another project.
I found out that after npm v3 they try to put all node_modules dependencies into one node_modules directory (one in your project) to flatten the structure as much as possible (http://codetunnel.io/npm-5-changes-to-npm-link/).
Be sure to check the main in package.json.
This serves as the entry of your package. This is a slight detail that took me a long time.
For me this happened when I decreased the version number of my local package from 0.1.0 to 0.0.1. And in the projects where I linked to this package I was still using the higher version number. Updating dependencies in package.json fixed it.
Check tsconfig moduleResolution
If like me, you happened to change the tsconfig module from es5 to esnext or something, then the moduleResolution default may have changed.
Without moduleResolution being set to "node", typescript will not resolve node_modules packages.
You can read on the Compiler Options page about how the default value depends on the value of module, whose default in turn depends on target — but probably set it to "node" explicitly.
I had a similar issue, and I had to perform the following steps to solve it:
In the library:
Setup the libraries that are generating issues as peerDependencies in the package.json instead of dependencies or devDependencies, e.g. in my case react:
"peerDependencies": {
"react": "^16.8.6",
...
}
run npm install
build the library (in my case, with a rollup -c npm script)
In my main app:
change the version of my library to point to my local project with a relative path in package.json, e.g.
"dependencies": {
"my-library": "file:../../libraries/my-library",
...
}
Add resolve.symlinks = false to my main app's webpack configuration
Add --preserve-symlinks-main and --preserve-symlinks to my package.json start script, e.g:
"scripts": {
"build": "set WEBPACK_CONFIG_FILE=all&& webpack",
"start": "set WEBPACK_CONFIG_FILE=all&& webpack && node --preserve-symlinks-main --preserve-symlinks dist/server.js",
}
run npm install
run npm run start
May be trivial, but worth mentioning:
npm link <module-name> must be executed after npm install (if needed) was executed in the <module-name> folder.
That is, unless the linked module is already present in <module-name> folder's package.json, in which case the order won't matter, because it means that the linked module is actually installed solely by npm install (as demonstrated here), and there's no need for linking using npm link - which is not the subject of this question.
The reason is that npm link <module-name> simply creates a symlink (or a folder shortcut, in Windows) to the linked package, so that executing npm install afterwards just deletes it.
To summarize, this is the order of execution:
Replacing the OP aligator with exporter and aligator-methods with importer for easier grasp
⚡ cd exporter
⚡ npm install <-- if needed, execute here, though it can also be executed after `npm link`
⚡ npm link
⚡ cd importer
⚡ npm install <-- if needed, must be executed here
⚡ npm link exporter
Bonus: A full minimal ES modules example of exporter and importer can be found here.
I know this is an old post, but in my case the issue was that I had renamed my package directory name, but the package.json "name" was still set to the old name.
for example, my directory name was package-name but the actual "name" found in package.json was package-name-b".
running yarn link would create a link called "package-name-b".
I then tried to run yarn link package-name since I was using the directory name. When I switched it to yarn link package-name-b, it worked.
I ran npm run build on the local package (dependency) and that worked for me
I was unable to import from my linked package because I simply forgot to prepend ./ to my module exports in my top level index.ts file:
export * from './utilities'
Took a while to figure that one out.
When using peerDependency
I'm developing two packages, stejs, and stejs-loader. stejs-loader has stejs as a peerDependency. When I ran npm link stejs-loader and npm link stejs in my project I was getting an error that stejs-loader couldn't find stejs. I got it fixed by running npm link stejs in the directory of stejs-loader.

How can I invoke npm on heroku command line (to install bower components)?

Bower is for client side Javascript what npm is for the server side and reads a component.json file to recognize dependencies that should be fetched at deploy time so I'd be happy it heroku would run it at slug compilation time.
Unfortunately I can not invoke npm or bower from a heroku console or one-off command (heroku run "npm help") (heroku run bash -> npm help) as it's possible with ruby's rake. I've put npm and node (latest/x versions) in my package.json but in the engines section, not the dependencies.
I think this could be solved by customizing the node buildpack but I consider this a little too heavy task just for activating something so obvious.
You can also setup a postintall command, something like this in your package.json
"dependencies": {
"bower": "0.6.x"
},
"scripts": {
"postinstall": "./node_modules/bower/bin/bower install"
}
Then npm install will also install bower dependencies.
Pros : one command to rule them all.
Cons : you unnecessarily embed bower as a dependency.
You can use run like this:
heroku run npm install git://github.com/webjay/kaiseki
You should declare NPM dependencies in the package.json file
All you install from shell will be deleted on exit shell. You are in a cloned instance.
You can use bower directly like this
"dependencies": {
"bower": "^1.7.9"
},
"scripts": {
"postinstall": "sudo bower install --allow-root "
}

Resources