Error: Cannot import module '@nuxt/builder'を解決するまでの道のり

#はじめに
Error: Cannot import module '@nuxt/builder'を解決するまでの道のり。

# 最終更新日
2022年2月1日

# 環境の情報
OS
Ubuntu2004

# 注意
最新の状態を確認して下さい。

#　公式はこちら
https://nuxtjs.org/docs/get-started/installation
https://cli.vuejs.org/


# 参考さま
https://qiita.com/kmifa/items/c320575b65ac35a9e7f1
https://stackoverflow.com/questions/67134676/how-to-resolve-cannot-import-module-nuxt-builder


# 上記を参考に進めていたのだが、


@nuxt/builderないって言われる。


```
test$ npm run generate

> nuxtcontent@1.0.0 generate /なんか置いてるところ/test
> nuxt generate


 WARN  When using nuxt generate, you should set target: 'static' in your nuxt.config
       👉 Learn more about it on https://go.nuxtjs.dev/static-target


 FATAL  Cannot import module '@nuxt/builder'                          22:22:02

  at Function.Module._resolveFilename (internal/modules/cjs/loader.js:902:15)
  at Function.Module._load (internal/modules/cjs/loader.js:746:27)
  at Module.require (internal/modules/cjs/loader.js:974:19)
  at require (internal/modules/cjs/helpers.js:93:18)
  at Object.<anonymous> (node_modules/webpack-dev-middleware/dist/utils/setupHooks.js:8:39)
  at Module._compile (internal/modules/cjs/loader.js:1085:14)
  at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)
  at Module.load (internal/modules/cjs/loader.js:950:32)
  at Function.Module._load (internal/modules/cjs/loader.js:790:12)
  at Module.require (internal/modules/cjs/loader.js:974:19)


   ╭─────────────────────────────────────────────────╮
   │                                                 │
   │   ✖ Nuxt Fatal Error                            │
   │                                                 │
   │   Error: Cannot import module '@nuxt/builder'   │
   │                                                 │
   ╰─────────────────────────────────────────────────╯

npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! nuxtcontent@1.0.0 generate: `nuxt generate`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the nuxtcontent@1.0.0 generate script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /home/user/.npm/_logs/2022-02-01T13_22_02_852Z-debug.log
```

# 調べてみたら

https://stackoverflow.com/questions/67134676/how-to-resolve-cannot-import-module-nuxt-builder

```
npm install
npm run build
```

したら直ったよ！という方が。

#やってみる

```
npm install
> core-js@3.20.3 postinstall /置いてるところ/test/node_modules/core-js
> node -e "try{require('./postinstall')}catch(e){}"

Thank you for using core-js ( https://github.com/zloirock/core-js ) for polyfilling JavaScript standard library!

The project needs your help! Please consider supporting of core-js:
> https://opencollective.com/core-js 
> https://patreon.com/zloirock 
> https://paypal.me/zloirock 
> bitcoin: bc1qlea7544qtsmj2rayg0lthvza9fau63ux0fstcz 

Also, the author of core-js ( https://github.com/zloirock ) is looking for a good job -)


> nuxt@2.15.8 postinstall /置いてるところ/test/node_modules/nuxt
> opencollective || exit 0

                                                             
                                     :-:                     
                                   .==-+:                    
                                  .==. :+- .-=-              
                                 .==.   :==++-+=.            
                                :==.     -**: :+=.           
                               :+-      :*+++. .++.          
                              :+-      -*= .++: .=+.         
                             -+:      =*-   .+*: .=+:        
                            -+:     .=*-     .=*-  =+:       
                          .==:     .+*:        -*-  -+-      
                         .=+:.....:+*-.........:=*=..=*-     
                         .-=------=++============++====:     
                                         
                         Thanks for installing nuxtjs 🙏
                 Please consider donating to our open collective
                        to help us maintain this package.
                                         
                            Number of contributors: 0
                              Number of backers: 460
                              Annual budget: $69,853
                             Current balance: $7,656
                                         
           👉  Donate: https://opencollective.com/nuxtjs/donate
                                         
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@2.3.2 (node_modules/fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@2.3.2: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@1.2.13 (node_modules/watchpack-chokidar2/node_modules/fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.2.13: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})

added 27 packages from 47 contributors, removed 2 packages, updated 6 packages and audited 1217 packages in 7.498s

103 packages are looking for funding
  run `npm fund` for details

found 13 vulnerabilities (10 moderate, 3 high)
  run `npm audit fix` to fix them, or `npm audit` for details
```

```
test$ npm run build

> nuxtcontent@1.0.0 build /置いてるところ/test
> nuxt build

ℹ Production build                                                    22:23:41
ℹ Bundling for server and client side                                 22:23:41
ℹ Target: server                                                      22:23:41
ℹ Using components loader to optimize imports                         22:23:41
ℹ Discovered Components: .nuxt/components/readme.md                   22:23:41
✔ Builder initialized                                                 22:23:41
✔ Nuxt files generated                                                22:23:41

✔ Client
  Compiled successfully in 7.90s

✔ Server
  Compiled successfully in 642.98ms


Hash: fc8487275c13747d2171
Version: webpack 4.46.0
Time: 7902ms
Built at: 2022/02/01 22:23:49
                         Asset        Size  Chunks                         Chunk Names
../server/client.manifest.json    11.6 KiB          [emitted]              
                    019ec79.js    6.63 KiB       4  [emitted] [immutable]  components/tutorial
                    20ddc1a.js    62.7 KiB       7  [emitted] [immutable]  vendors/pages/index
                    257ac55.js    16.7 KiB       2  [emitted] [immutable]  commons/pages/index
                    442bd9a.js  1000 bytes       5  [emitted] [immutable]  pages/index
                    4d80b6d.js    52.8 KiB       0  [emitted] [immutable]  app
                    5683452.js    2.34 KiB       6  [emitted] [immutable]  runtime
                    9489330.js     191 KiB       1  [emitted] [immutable]  commons/app
                      LICENSES   407 bytes          [emitted]              
                    ee8da59.js    1.85 KiB       3  [emitted] [immutable]  components/nuxt-logo
 + 2 hidden assets
Entrypoint app = 5683452.js 9489330.js 4d80b6d.js

Hash: 2c52acbf4e3df4fe5bcf
Version: webpack 4.46.0
Time: 644ms
Built at: 2022/02/01 22:23:50
                  Asset       Size  Chunks             Chunk Names
components/nuxt-logo.js   5.89 KiB       1  [emitted]  components/nuxt-logo
 components/tutorial.js   7.81 KiB       2  [emitted]  components/tutorial
         pages/index.js   3.09 KiB       3  [emitted]  pages/index
              server.js   79.3 KiB       0  [emitted]  app
   server.manifest.json  443 bytes          [emitted]  
 + 4 hidden assets
Entrypoint app = server.js server.js.map
ℹ Ready to run nuxt start                                             22:23:50
```

# できた

```
test$ npm run generate

> test@1.0.0 generate /置いてるところ/test
> nuxt generate


 WARN  When using nuxt generate, you should set target: 'static' in your nuxt.config
       👉 Learn more about it on https://go.nuxtjs.dev/static-target


ℹ Production build                                                    22:24:04
ℹ Bundling for server and client side                                 22:24:04
ℹ Target: static                                                      22:24:04
ℹ Using components loader to optimize imports                         22:24:04
ℹ Discovered Components: .nuxt/components/readme.md                   22:24:04
✔ Builder initialized                                                 22:24:04
✔ Nuxt files generated                                                22:24:04

✔ Client
  Compiled successfully in 7.58s

✔ Server
  Compiled successfully in 579.69ms


Hash: dd57b181b94f59f9ffd3
Version: webpack 4.46.0
Time: 7583ms
Built at: 2022/02/01 22:24:12
                         Asset        Size  Chunks                         Chunk Names
../server/client.manifest.json    11.6 KiB          [emitted]              
                    019ec79.js    6.63 KiB       4  [emitted] [immutable]  components/tutorial
                    20ddc1a.js    62.7 KiB       7  [emitted] [immutable]  vendors/pages/index
                    257ac55.js    16.7 KiB       2  [emitted] [immutable]  commons/pages/index
                    342c568.js    53.1 KiB       0  [emitted] [immutable]  app
                    442bd9a.js  1000 bytes       5  [emitted] [immutable]  pages/index
                    5683452.js    2.34 KiB       6  [emitted] [immutable]  runtime
                    9489330.js     191 KiB       1  [emitted] [immutable]  commons/app
                      LICENSES   407 bytes          [emitted]              
                    ee8da59.js    1.85 KiB       3  [emitted] [immutable]  components/nuxt-logo
 + 2 hidden assets
Entrypoint app = 5683452.js 9489330.js 342c568.js

Hash: 33066ac4740ba628e49c
Version: webpack 4.46.0
Time: 581ms
Built at: 2022/02/01 22:24:13
                  Asset       Size  Chunks             Chunk Names
components/nuxt-logo.js   5.89 KiB       1  [emitted]  components/nuxt-logo
 components/tutorial.js   7.81 KiB       2  [emitted]  components/tutorial
         pages/index.js   3.09 KiB       3  [emitted]  pages/index
              server.js   79.4 KiB       0  [emitted]  app
   server.manifest.json  443 bytes          [emitted]  
 + 4 hidden assets
Entrypoint app = server.js server.js.map
ℹ Generating output directory: dist/                                  22:24:13
ℹ Generating pages                                                    22:24:13
✔ Generated route "/"                                                 22:24:13
✔ Client-side fallback created: 200.html                              22:24:13
```

```
test$ npm run dev 
> nuxtcontent@1.0.0 dev /置いてるところ/test
> nuxt


   ╭───────────────────────────────────────╮
   │                                       │
   │   Nuxt @ v2.15.8                      │
   │                                       │
   │   ▸ Environment: development          │
   │   ▸ Rendering:   server-side          │
   │   ▸ Target:      server               │
   │                                       │
   │   Listening: http://localhost:3000/   │
   │                                       │
   ╰───────────────────────────────────────╯
```

#　こちら
![Screenshot from 2022-02-01 22-25-11.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/148394/972edd18-c491-0211-30d8-00fbd8941dfb.png)