RedwoodJS - Cannot use import statement outside a module"

Hi!

I set up a new project with some branch deploy, the deploy on itself seems to work well but the build seems to be broken.

The framework is RedwoodJS setup as a Typescript project, you will find below the address and the last deploy log.

The error I am getting seems to happen on any api call:

trace: ["Runtime.UserCodeSyntaxError: SyntaxError: Cannot use import statement outside a module",…]
0: "Runtime.UserCodeSyntaxError: SyntaxError: Cannot use import statement outside a module"
1: "    at _loadUserApp (/var/runtime/UserFunction.js:98:13)"
2: "    at Object.module.exports.load (/var/runtime/UserFunction.js:140:17)"
3: "    at Object.<anonymous> (/var/runtime/index.js:43:30)"
4: "    at Module._compile (internal/modules/cjs/loader.js:999:30)"
5: "    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)"
6: "    at Module.load (internal/modules/cjs/loader.js:863:32)"
7: "    at Function.Module._load (internal/modules/cjs/loader.js:708:14)"
8: "    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:60:12)"
9: "    at internal/main/run_main_module.js:17:47"

The react part works well.

I have seen a couple of other posts on such an error but I don’t believe it applies to this project. Here would be relevant files, also to note, I set up successfully other RedwoodJS projects, those files were accordingly corrected to no success:

# netlify.toml
[build]
  command = "yarn rw deploy netlify"
  publish = "web/dist"
  functions = "api/dist/functions"

[dev]
  command = "yarn rw dev"

[[redirects]]
  from = "/*"
  to = "/index.html"
  status = 200
# api/tsconfig.json
{
  "extends": "../tsconfig.json",
  "compilerOptions": {
    "baseUrl": ".",
    "paths": {
      "src/*": [
        "./src/*"
      ]
    },
    "types": [
      "./src/services/**/index.d.ts",
    ]
  },
  "exclude": [
    "./node_modules"
  ],
  "include": [
    "src"
  ]
}
#tsconfig.json
{
  "compilerOptions": {
    "allowJs": true,
    "allowSyntheticDefaultImports": true,
    "baseUrl": ".",
    "composite": false,
    "declarationMap": false,
    "emitDeclarationOnly": false,
    "esModuleInterop": true,
    "jsx": "preserve",
    "lib": [
      "es7",
      "es6",
      "dom",
      "ES2020"
    ],
    "noEmit": true,
    "module": "esnext",
    "moduleResolution": "node",
    "pretty": true,
    "sourceMap": false,
    "target": "esnext",
    "strict": true,
    "strictNullChecks": true,
    "typeRoots": [
      ".redwood"
    ]
  },
  "include": [
    "./api/src",
    "./web/src"
  ],
  "exclude": [
    "node_modules"
  ]
}

The url: https://brave-yalow-002a9c.netlify.app/
The log:

12:04:13 PM: Build ready to start
12:04:15 PM: build-image version: 3571f0130496395a23bffe9820bc78b4f73a6234
12:04:15 PM: build-image tag: v3.7.0
12:04:15 PM: buildbot version: b8a7cb2ab30309dc791e77b97759136f817b5561
12:04:15 PM: Fetching cached dependencies
12:04:15 PM: Starting to download cache of 295.3MB
12:04:18 PM: Finished downloading cache in 2.465825623s
12:04:18 PM: Starting to extract cache
12:04:32 PM: Finished extracting cache in 14.164088022s
12:04:32 PM: Finished fetching cache in 16.758307559s
12:04:32 PM: Starting to prepare the repo for build
12:04:32 PM: Preparing Git Reference refs/heads/preview
12:04:34 PM: Parsing package.json dependencies
12:04:35 PM: Different publish path detected, going to use the one specified in the Netlify configuration file: 'web/dist' versus '/' in the Netlify UI
12:04:35 PM: Different functions path detected, going to use the one specified in the Netlify configuration file: 'api/dist/functions' versus '' in the Netlify UI
12:04:35 PM: Different build command detected, going to use the one specified in the Netlify configuration file: 'yarn rw deploy netlify' versus '' in the Netlify UI
12:04:35 PM: Starting build script
12:04:35 PM: Installing dependencies
12:04:35 PM: Python version set to 2.7
12:04:36 PM: Started restoring cached node version
12:04:39 PM: Finished restoring cached node version
12:04:39 PM: Attempting node version 'lts/*' from .nvmrc
12:04:40 PM: v14.16.0 is already installed.
12:04:40 PM: Now using node v14.16.0 (npm v6.14.11)
12:04:40 PM: Started restoring cached build plugins
12:04:40 PM: Finished restoring cached build plugins
12:04:41 PM: Attempting ruby version 2.7.1, read from environment
12:04:42 PM: Using ruby version 2.7.1
12:04:43 PM: Using PHP version 5.6
12:04:43 PM: Started restoring cached yarn cache
12:04:43 PM: Finished restoring cached yarn cache
12:04:43 PM: Started restoring cached node modules
12:04:43 PM: Finished restoring cached node modules
12:04:43 PM: Installing NPM modules using Yarn version 1.22.4
12:04:44 PM: yarn install v1.22.4
12:04:44 PM: [1/5] Validating package.json...
12:04:44 PM: [2/5] Resolving packages...
12:04:45 PM: warning Resolution field "react@17.0.1" is incompatible with requested version "react@^16.8.3"
12:04:45 PM: warning Resolution field "react@17.0.1" is incompatible with requested version "react@^16.8.3"
12:04:45 PM: warning Resolution field "react-dom@17.0.1" is incompatible with requested version "react-dom@^16.8.3"
12:04:45 PM: warning Resolution field "react@17.0.1" is incompatible with requested version "react@^16.8.3"
12:04:45 PM: warning Resolution field "react-dom@17.0.1" is incompatible with requested version "react-dom@^16.8.3"
12:04:45 PM: [3/5] Fetching packages...
12:05:52 PM: info fsevents@2.3.2: The platform "linux" is incompatible with this module.
12:05:52 PM: info "fsevents@2.3.2" is an optional dependency and failed compatibility check. Excluding it from installation.
12:05:52 PM: info fsevents@1.2.13: The platform "linux" is incompatible with this module.
12:05:52 PM: info "fsevents@1.2.13" is an optional dependency and failed compatibility check. Excluding it from installation.
12:05:52 PM: [4/5] Linking dependencies...
12:05:52 PM: warning "@redwoodjs/core > @redwoodjs/testing > @redwoodjs/web > react-hot-toast > goober@2.0.33" has unmet peer dependency "csstype@^2.6.2".
12:05:52 PM: warning "@redwoodjs/core > @storybook/react > @storybook/addons > @storybook/api > @reach/router@1.3.4" has incorrect peer dependency "react@15.x || 16.x || 16.4.0-alpha.0911da3".
12:05:52 PM: warning "@redwoodjs/core > @storybook/react > @storybook/addons > @storybook/api > @reach/router@1.3.4" has incorrect peer dependency "react-dom@15.x || 16.x || 16.4.0-alpha.0911da3".
12:05:52 PM: warning "@redwoodjs/core > @storybook/react > @storybook/core > @storybook/ui > react-helmet-async@1.0.7" has incorrect peer dependency "react@^16.6.0".
12:05:52 PM: warning "@redwoodjs/core > @storybook/react > @storybook/core > @storybook/ui > react-helmet-async@1.0.7" has incorrect peer dependency "react-dom@^16.6.0".
12:05:52 PM: warning "@redwoodjs/core > @storybook/react > @storybook/core > @storybook/ui > react-sizeme@2.6.12" has incorrect peer dependency "react@^0.14.0 || ^15.0.0-0 || ^16.0.0".
12:05:52 PM: warning "@redwoodjs/core > @storybook/react > @storybook/core > @storybook/ui > react-sizeme@2.6.12" has incorrect peer dependency "react-dom@^0.14.0 || ^15.0.0-0 || ^16.0.0".
12:05:52 PM: warning "@redwoodjs/core > @storybook/react > @storybook/addons > @storybook/api > @reach/router > create-react-context@0.3.0" has incorrect peer dependency "react@^0.14.0 || ^15.0.0 || ^16.0.0".
12:05:52 PM: warning "@redwoodjs/core > @storybook/react > @storybook/core > @storybook/ui > @storybook/components > react-popper-tooltip@2.11.1" has incorrect peer dependency "react@^16.6.0".
12:05:52 PM: warning "@redwoodjs/core > @storybook/react > @storybook/core > @storybook/ui > @storybook/components > react-popper-tooltip@2.11.1" has incorrect peer dependency "react-dom@^16.6.0".
12:05:52 PM: warning "@redwoodjs/core > @storybook/react > @storybook/core > @storybook/ui > @storybook/components > react-textarea-autosize@7.1.2" has incorrect peer dependency "react@>=0.14.0 <17.0.0".
12:05:52 PM: warning "@redwoodjs/core > @storybook/react > @storybook/core > @storybook/ui > @storybook/components > simplebar-react@1.2.3" has incorrect peer dependency "react@^0.14.9 || ^15.3.0 || ^16.0.0-rc || ^16.0".
12:05:52 PM: warning "@redwoodjs/core > @storybook/react > @storybook/core > @storybook/ui > @storybook/components > simplebar-react@1.2.3" has incorrect peer dependency "react-dom@^0.14.9 || ^15.3.0 || ^16.0.0-rc || ^16.0".
12:05:52 PM: warning "workspace-aggregator-f985cad3-97c8-4aca-897d-a58ea537610d > web > @date-io/date-fns@1.3.13" has unmet peer dependency "date-fns@^2.0.0".
12:05:52 PM: warning "workspace-aggregator-f985cad3-97c8-4aca-897d-a58ea537610d > web > @material-ui/pickers@3.2.10" has unmet peer dependency "@date-io/core@^1.3.6".
12:05:52 PM: warning "workspace-aggregator-f985cad3-97c8-4aca-897d-a58ea537610d > web > @material-ui/pickers@3.2.10" has incorrect peer dependency "react@^16.8.4".
12:05:52 PM: warning "workspace-aggregator-f985cad3-97c8-4aca-897d-a58ea537610d > web > @material-ui/pickers@3.2.10" has incorrect peer dependency "react-dom@^16.8.4".
12:05:52 PM: warning "workspace-aggregator-f985cad3-97c8-4aca-897d-a58ea537610d > web > dexie-react-hooks@1.0.6" has unmet peer dependency "@types/react@>=16".
12:05:52 PM: warning "workspace-aggregator-f985cad3-97c8-4aca-897d-a58ea537610d > web > sass-loader@10.1.1" has unmet peer dependency "webpack@^4.36.0 || ^5.0.0".
12:05:52 PM: warning "workspace-aggregator-f985cad3-97c8-4aca-897d-a58ea537610d > web > postcss-loader@4.0.2" has unmet peer dependency "webpack@^4.0.0 || ^5.0.0".
12:05:52 PM: warning "workspace-aggregator-f985cad3-97c8-4aca-897d-a58ea537610d > web > postcss-loader@4.0.2" has unmet peer dependency "postcss@^7.0.0 || ^8.0.1".
12:05:52 PM: warning "workspace-aggregator-f985cad3-97c8-4aca-897d-a58ea537610d > web > storybook-addon-designs@5.4.5" has unmet peer dependency "@storybook/addons@^5.0.0 || ^6.0.0".
12:05:52 PM: warning "workspace-aggregator-f985cad3-97c8-4aca-897d-a58ea537610d > web > storybook-addon-designs@5.4.5" has unmet peer dependency "@storybook/components@^5.0.0 || ^6.0.0".
12:05:52 PM: warning "workspace-aggregator-f985cad3-97c8-4aca-897d-a58ea537610d > web > storybook-addon-designs@5.4.5" has unmet peer dependency "@storybook/theming@^5.0.0 || ^6.0.0".
12:05:52 PM: warning "workspace-aggregator-f985cad3-97c8-4aca-897d-a58ea537610d > web > storybook-addon-designs > react-pdf@4.2.0" has incorrect peer dependency "react@^16.3.0".
12:05:52 PM: warning "workspace-aggregator-f985cad3-97c8-4aca-897d-a58ea537610d > web > storybook-addon-designs > react-pdf@4.2.0" has incorrect peer dependency "react-dom@^16.3.0".
12:05:52 PM: warning "workspace-aggregator-f985cad3-97c8-4aca-897d-a58ea537610d > api > firebase-admin > @firebase/database > @firebase/auth-interop-types@0.1.5" has unmet peer dependency "@firebase/app-types@0.x".
12:05:52 PM: warning "workspace-aggregator-f985cad3-97c8-4aca-897d-a58ea537610d > web > storybook-addon-designs > react-pdf > pdfjs-dist@2.1.266" has unmet peer dependency "webpack@^3.0.0 || ^4.0.0-alpha.0 || ^4.0.0".
12:05:52 PM: warning "workspace-aggregator-f985cad3-97c8-4aca-897d-a58ea537610d > api > @redwoodjs/api > graphql-tools > @graphql-tools/links > apollo-upload-client@14.1.2" has unmet peer dependency "subscriptions-transport-ws@^0.9.0".
12:05:52 PM: warning "workspace-aggregator-f985cad3-97c8-4aca-897d-a58ea537610d > web > storybook-addon-designs > react-pdf > pdfjs-dist > worker-loader@2.0.0" has unmet peer dependency "webpack@^3.0.0 || ^4.0.0-alpha.0 || ^4.0.0".
12:06:35 PM: [5/5] Building fresh packages...
12:06:43 PM: Done in 119.64s.
12:06:44 PM: NPM modules installed using Yarn
12:06:44 PM: Started restoring cached go cache
12:06:44 PM: Finished restoring cached go cache
12:06:44 PM: go version go1.14.4 linux/amd64
12:06:44 PM: go version go1.14.4 linux/amd64
12:06:44 PM: Installing missing commands
12:06:44 PM: Verify run directory
12:06:46 PM: ​
12:06:46 PM: ────────────────────────────────────────────────────────────────
12:06:46 PM:   Netlify Build                                                 
12:06:46 PM: ────────────────────────────────────────────────────────────────
12:06:46 PM: ​
12:06:46 PM: ❯ Version
12:06:46 PM:   @netlify/build 9.11.4
12:06:46 PM: ​
12:06:46 PM: ❯ Flags
12:06:46 PM:   apiHost: api.netlify.com
12:06:46 PM:   cacheDir: /opt/build/cache
12:06:46 PM:   deployId: 6059cb2d30a857000796d70b
12:06:46 PM:   mode: buildbot
12:06:46 PM:   testOpts:
12:06:46 PM:     silentLingeringProcesses: ''
12:06:46 PM: ​
12:06:46 PM: ❯ Current directory
12:06:46 PM:   /opt/build/repo
12:06:46 PM: ​
12:06:46 PM: ❯ Config file
12:06:46 PM:   /opt/build/repo/netlify.toml
12:06:46 PM: ​
12:06:46 PM: ❯ Context
12:06:46 PM:   production
12:06:46 PM: ​
12:06:46 PM: ────────────────────────────────────────────────────────────────
12:06:46 PM:   1. build.command from netlify.toml                            
12:06:46 PM: ────────────────────────────────────────────────────────────────
12:06:46 PM: ​
12:06:46 PM: $ yarn rw deploy netlify
12:06:46 PM: yarn run v1.22.4
12:06:46 PM: $ /opt/build/repo/node_modules/.bin/rw deploy netlify
12:06:49 PM: $ /opt/build/repo/node_modules/.bin/rw build
12:06:51 PM: Generating the Prisma client... [started]
12:06:56 PM: Generating the Prisma client... [completed]
12:06:56 PM: [11:06:56] Cleaning "web"... (./web/dist/) [started]
12:06:56 PM: [11:06:56] Cleaning "web"... (./web/dist/) [completed]
12:06:56 PM: [11:06:56] Building "api"... [started]
12:07:01 PM: [11:07:01] Building "api"... [completed]
12:07:01 PM: [11:07:01] Building "web"... [started]
12:08:00 PM: [11:08:00] Building "web"... [completed]
12:08:00 PM: [11:08:00] Prerendering "web"... [started]
12:08:05 PM: [11:08:05] Prerendering "web"... [failed]
12:08:05 PM: [11:08:05] β†’ Command failed with exit code 1: yarn rw prerender
12:08:05 PM: Could not resolve "/opt/build/repo/web/src/pages/FatalErrorPage" in file /opt/build/repo/web/src/App.js.
12:08:05 PM: error Command failed with exit code 1.
12:08:05 PM: $ /opt/build/repo/node_modules/.bin/rw prerender
12:08:05 PM: [11:08:03] Prerendering / -> web/dist/index.html [started]
12:08:05 PM: [11:08:05] Prerendering / -> web/dist/index.html [failed]
12:08:05 PM: [11:08:05] β†’ document is not defined
12:08:05 PM: Running diagnostic checks
12:08:05 PM: βœ” Diagnostics checks passed
12:08:05 PM: Not all routes were succesfully prerendered. Run `yarn rw prerender --dry-run` for detailed logs
12:08:05 PM: We could not prerender all your pages, but your Redwood app should still work fine.
12:08:05 PM: This could mean that a library you're using does not support SSR.
12:08:05 PM: info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
12:08:05 PM: Command failed with exit code 1: yarn rw prerender
12:08:05 PM: Could not resolve "/opt/build/repo/web/src/pages/FatalErrorPage" in file /opt/build/repo/web/src/App.js.
12:08:05 PM: error Command failed with exit code 1.
12:08:05 PM: $ /opt/build/repo/node_modules/.bin/rw prerender
12:08:05 PM: [11:08:03] Prerendering / -> web/dist/index.html [started]
12:08:05 PM: [11:08:05] Prerendering / -> web/dist/index.html [failed]
12:08:05 PM: [11:08:05] β†’ document is not defined
12:08:05 PM: 
12:08:05 PM: Running diagnostic checks
12:08:05 PM: βœ” Diagnostics checks passed 
12:08:05 PM: 
12:08:05 PM: Not all routes were succesfully prerendered. Run `yarn rw prerender --dry-run` for detailed logs
12:08:05 PM: We could not prerender all your pages, but your Redwood app should still work fine.
12:08:05 PM: This could mean that a library you're using does not support SSR.
12:08:05 PM: info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
12:08:05 PM: error Command failed with exit code 1.
12:08:05 PM: info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
12:08:05 PM: Done in 79.29s.
12:08:05 PM: ​
12:08:05 PM: (build.command completed in 1m 19.5s)
12:08:05 PM: ​
12:08:05 PM: ────────────────────────────────────────────────────────────────
12:08:05 PM:   2. Functions bundling                                         
12:08:05 PM: ────────────────────────────────────────────────────────────────
12:08:05 PM: ​
12:08:05 PM: Packaging Functions from api/dist/functions directory:
12:08:05 PM:  - graphql.js
12:08:05 PM: ​
12:08:47 PM: ​
12:08:47 PM: (Functions bundling completed in 41.6s)
12:08:47 PM: ​
12:08:47 PM: ────────────────────────────────────────────────────────────────
12:08:47 PM:   3. Deploy site                                                
12:08:47 PM: ────────────────────────────────────────────────────────────────
12:08:47 PM: ​
12:08:47 PM: Starting to deploy site from 'web/dist'
12:08:47 PM: Creating deploy tree 
12:08:47 PM: Creating deploy upload records
12:08:47 PM: 22 new files to upload
12:08:47 PM: 1 new functions to upload
12:08:54 PM: Site deploy was successfully initiated
12:08:54 PM: ​
12:08:54 PM: (Deploy site completed in 6.6s)
12:08:54 PM: ​
12:08:54 PM: ────────────────────────────────────────────────────────────────
12:08:54 PM:   Netlify Build Complete                                        
12:08:54 PM: ────────────────────────────────────────────────────────────────
12:08:54 PM: ​
12:08:54 PM: (Netlify Build completed in 2m 7.8s)
12:08:54 PM: Starting post processing
12:08:54 PM: Caching artifacts
12:08:54 PM: Started saving node modules
12:08:54 PM: Finished saving node modules
12:08:54 PM: Started saving build plugins
12:08:54 PM: Finished saving build plugins
12:08:54 PM: Started saving yarn cache
12:08:54 PM: Finished saving yarn cache
12:08:54 PM: Started saving pip cache
12:08:54 PM: Post processing - HTML
12:08:54 PM: Finished saving pip cache
12:08:54 PM: Started saving emacs cask dependencies
12:08:54 PM: Post processing - header rules
12:08:54 PM: Post processing - redirect rules
12:08:54 PM: Finished saving emacs cask dependencies
12:08:54 PM: Started saving maven dependencies
12:08:54 PM: Finished saving maven dependencies
12:08:54 PM: Started saving boot dependencies
12:08:54 PM: Finished saving boot dependencies
12:08:54 PM: Started saving rust rustup cache
12:08:54 PM: Finished saving rust rustup cache
12:08:54 PM: Started saving go dependencies
12:08:54 PM: Finished saving go dependencies
12:08:54 PM: Build script success
12:08:54 PM: Post processing done
12:08:54 PM: Site is live ✨
12:09:55 PM: Finished processing build request in 5m40.469657435s

Update:
I’ve checked the api/dist for import ... statements and found nothing.
I’ve built the app with the netlify cli and checked the zipped code for such statements, still nothing. The app builds properly while I’m developing so I can only see this as a Netlify issue.

Found it

I had some imports which included the workspace api
import {createAccount} from 'api/src/services/accounts/accounts'
They were working on local and I had no warning of any kind, i think they might have been a misconfiguration of my IDE at some point.

It only revealed itself on deploy.

Removing the workspace made the import work again.
import {createAccount} from 'src/services/accounts/accounts'

1 Like

Hey there, @noire.munich :wave:

Thank you so much for sharing your solution and thought process here! It will be very helpful for future Forums members who encounter something similar.