NUXT SSR aws pipeline Build Issue

Viewed 262

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.

2 Answers

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/**/*"

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?

Related