3

I have a very simple aws code pipeline to build Nuxt for SSR on eleastic beanstalk.

version: 0.2

phases:
  install:
    runtime-versions:
      nodejs: 14
  pre_build:
    commands:
      - yarn
  build:
    commands:
      - yarn build --max-old-space-size=2048

artifacts:
  base-directory: ./
  files:
    - '**/*'

cache:
  paths:

It fails on Elastic Beanstalk with this error:

web: > nuxt start
web: internal/modules/cjs/loader.js:905
web: throw err;
web: ^
web: Error: Cannot find module '../package.json'
web: Require stack:
web: - /var/app/current/node_modules/.bin/nuxt
web: at Function.Module._resolveFilename (internal/modules/cjs/loader.js:902:15)
web: at Function.Module._load (internal/modules/cjs/loader.js:746:27)
web: at Module.require (internal/modules/cjs/loader.js:974:19)
web: at require (internal/modules/cjs/helpers.js:93:18)
web: at Object.<anonymous> (/var/app/current/node_modules/.bin/nuxt:5:16)
web: at Module._compile (internal/modules/cjs/loader.js:1085:14)
web: at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)
web: at Module.load (internal/modules/cjs/loader.js:950:32)
web: at Function.Module._load (internal/modules/cjs/loader.js:790:12)
web: at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:76:12) {
web: code: 'MODULE_NOT_FOUND',
web: requireStack: [ '/var/app/current/node_modules/.bin/nuxt' ]
web: }

If I download the application to my local I get the same problem. BUT if I rerun

yarn install

it starts working.

it looks like it is this line of node_modules/.bin/nuxt

const suffix = require('../package.json')

Because it is in the .bin folder there isn't any package.json, not should how this ever works.

So I am trying to figure out how the yarn install is different on aws pipeline vs local. Same yarn versions on pipeline and local 1.22.1

What breaks if you move your Nuxt Application from one server/location to another? I can build it local and upload it to Elastic Beanstalk and that works.

Any suggestions would be a big help.

Jimmar
  • 4,194
  • 2
  • 28
  • 43
Patrick_Finucane
  • 725
  • 1
  • 7
  • 24

2 Answers2

0

you will need to run yarn install before the yarn build or just run

yarn install && yarn build --max-old-space-size=2048

you can probably also have your prebuild stage do yarn install but I never tried that.

I've recently wrote a tutorial for deploying a nuxt app with SSR on elasticBeanstalk which you can check the last part that includes code pipeline buildspec.yml file and the necessary artifacts.
it uses npm but you can easily change it to use yarn

version: 0.2

phases:
  build:
    commands:
      - npm install && npm run build

artifacts:
  files:
    - "package.json"
    - "nuxt.config.js"
    - ".nuxt/**/*"
    - "static/**/*"
    - ".platform/**/*"
    - ".ebextensions/**/*"
Jimmar
  • 4,194
  • 2
  • 28
  • 43
  • I found your tutorial, googling around. It was very helpful, it amazing how few SSR tutorials are out there for nuxt.js. So good job writing it up. Unfortunately that didn't solve the problem. The "yarn" in the pre command is the same as running "yarn install". – Patrick_Finucane Jan 22 '22 at 23:51
  • @Patrick_Finucane have you tried having them together under build commands like how I wrote it ? that's how I had it set up and it worked for me with `npm` – Jimmar Jan 22 '22 at 23:55
  • I think what's wrong is the start command I was using in my package.json. – Patrick_Finucane Jan 23 '22 at 00:05
  • @Patrick_Finucane wait, I just realized that the error you are getting is on Elastic Beanstalk, that means the issue could be with your artifacts not being copied correctly, did you try to ssh into your ebs instance and going to `/var/app/` and checking the staging directory ? – Jimmar Jan 23 '22 at 00:12
0

I updated two things and it started working.

On my buildspec I removed the following from the artifacts: section

  base-directory: ./

And in my package.json file I updated the start command to

"start": "node_modules/nuxt/bin/nuxt.js start",

Note that I have found tutorials with the start location as

"start": "node_modules/.bin/nuxt"

That did NOT work for me. I wish the Nuxt docs were a little more clear about the best practice of running SSR in a CI/CD environment. Like after you build do you still need the node_modules folder?

Patrick_Finucane
  • 725
  • 1
  • 7
  • 24
  • After you build, you do need the `node_modules` directory but elastic beanstalk would install your modules from your `package.json` so that would be enough. you'd also need the `.nuxt` directory and your `nuxt.config.js`, those are the minimum requirements for things to work properly – Jimmar Jan 23 '22 at 00:14
  • also you actually don't need to change the default `start` command which is `nuxt start` since nuxt would be installed before trying to run it. – Jimmar Jan 23 '22 at 00:18