Reputation: 3050
I've got continuous deployment setup with Netlify and a git repo but so far no matter what I do, Netlify isn't npm-install
ing anything. There's no node_modules folder when I download a zip of the deploy, my site can't access node_modules, it's just not there. I've started with a random npm package (lodash) to try to get it to install but I've got nothing so far.
Netlify says that it automatically runs npm install
. I've tried having no build commands and I've tried adding npm install
as a build command with no results from either.
package.json:
{
"name": "netlify-test",
"version": "1.0.0",
"description": "stuff",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/electrovir/netlify-test.git"
},
"author": "electrovir",
"license": "MIT",
"bugs": {
"url": "https://github.com/electrovir/netlify-test/issues"
},
"homepage": "https://github.com/electrovir/netlify-test#readme",
"dependencies": {
"lodash": "^4.17.11"
}
}
HTML:
<!doctype html>
<html>
<head>
<title>
hi
</title>
<script src="node_modules/lodash/_apply.js"></script>
</head>
<body>
Why can't this find node_modules??
</body>
</html>
Upvotes: 7
Views: 7213
Reputation: 3050
It took a while to figure this out but I discovered that Netlify does npm install
into your repo root or Base directory when you have a package.json
. This can be seen by changing the Build command to something like ls
and reading the deploy console output (there will be a node_modules
folder listed).
However, at some point after running the Build command and before deploying, this node_modules
is deleted. Hence, if you use something similar to the following as a Build command, node_modules
can be copied to your Publish directory:
rm -rf dist && mkdir dist && rsync -rv * dist --exclude ./dist
This copies the Base directory contents into dist
, which I have set as my Publish directory, ignoring ./dist
itself (it can't be copied into itself). It'd be even better if this were added as an npm script in package.json
with npm run <script name>
for your Build command.
Note that Base directory, Build command, and Publish directory are all under Deploys > Deploy settings or Settings > Build & deploy in Netlify.
Thanks to lastmjs for help with figuring this out.
Upvotes: 7
Reputation: 3189
It would be odd if node_modules
WERE there. Netlify's continuous deployment does run npm install for you, if and only if there is a package.json in the root of your repository (or instead in the base
directory if you set one in netlify.toml. However, it also uses a custom npm directory that is outside of the deployment directory (see how it is setup here: https://github.com/netlify/build-image/blob/master/run-build-functions.sh#L34), since deploying your node_modules
shouldn't be needed for a static site at browse time - only at build time.
The intended path for a Netlify deploy is:
base
directory is set in the UI or in the toml file) or in the base
directory if set. This could be any/all of Gemfile.lock
, package.json
, yarn.lock
, or requirements.txt
as explained in this article about the build settings at Netlifynode_modules
in the repo. Many of those will be specific to the architecture of the build environment - and your local machine is almost certainly different than Netlify's. You'll want those generated at build time..env
file while building, or you use gulp-cli to process your Gulpfile.js
You could have a situation that requires some/one of them - e.g. a function or you could need a file from one of them even on your static site - and that's fine, feel free to copy it into the publish directory explicitly. But that is not the norm :)
Upvotes: 3