简体   繁体   中英

Next.js - `npm run build` failed

Trying to build my app with target: 'serverless' in next.config.js (In-order to deploy on AWS Lambda). When running npm run build I'm getting the following output:

Warning: Built-in CSS support is being disabled due to custom CSS configuration being detected.
See here for more info: https://err.sh/next.js/built-in-css-disabled  

info  - Using external babel configuration from C:\Users\User\Desktop\mysite\.babelrc
info  - Creating an optimized production build  
warn  - Compiled with warnings

./node_modules/require_optional/index.js
Critical dependency: the request of a dependency is an expression     

./node_modules/node-pre-gyp/lib/pre-binding.js
Critical dependency: the request of a dependency is an expression     

./node_modules/node-pre-gyp/lib/util/versioning.js
Critical dependency: the request of a dependency is an expression     

./node_modules/node-fetch/lib/index.js
Module not found: Can't resolve 'encoding' in 'C:\Users\User\Desktop\mysite\node_modules\node-fetch\lib'

./node_modules/mongodb/lib/operations/connect.js
Module not found: Can't resolve 'mongodb-client-encryption' in 'C:\Users\User\Desktop\mysite\node_modules\mongodb\lib\operations'

./node_modules/node-pre-gyp/lib/util/compile.js
Module not found: Can't resolve 'node-gyp' in 'C:\Users\User\Desktop\mysite\node_modules\node-pre-gyp\lib\util'

./node_modules/node-pre-gyp/lib/util/compile.js
Module not found: Can't resolve 'npm' in 'C:\Users\User\Desktop\mysite\node_modules\node-pre-gyp\lib\util'

./node_modules/node-pre-gyp/lib/util/nw-pre-gyp/index.html 1:0        
Module parse failed: Unexpected token (1:0)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
> <!doctype html>
| <html>
| <head>


> Build error occurred
Error: package.json does not exist at C:\package.json
    at Object.module.exports.wr/F.exports.find (C:\Users\User\Desktop\mysite\.next\serverless\pages\_error.js:188263:15)
    at Object.<anonymous> (C:\Users\User\Desktop\mysite\.next\serverless\pages\_error.js:154078:28)
    at Object.paWE (C:\Users\User\Desktop\mysite\.next\serverless\pages\_error.js:154141:30)
    at __webpack_require__ (C:\Users\User\Desktop\mysite\.next\serverless\pages\_error.js:23:31)
    at Object.xDbK (C:\Users\User\Desktop\mysite\.next\serverless\pages\_error.js:189175:64)
    at __webpack_require__ (C:\Users\User\Desktop\mysite\.next\serverless\pages\_error.js:23:31)
    at Object.gL7F (C:\Users\User\Desktop\mysite\.next\serverless\pages\_error.js:123042:67)
    at __webpack_require__ (C:\Users\User\Desktop\mysite\.next\serverless\pages\_error.js:23:31)
    at Module.IlR1 (C:\Users\User\Desktop\mysite\.next\serverless\pages\_error.js:53618:12)
    at __webpack_require__ (C:\Users\User\Desktop\mysite\.next\serverless\pages\_error.js:23:31)
    at Module.ScIc (C:\Users\User\Desktop\mysite\.next\serverless\pages\_error.js:81256:17)
    at __webpack_require__ (C:\Users\User\Desktop\mysite\.next\serverless\pages\_error.js:23:31)
    at C:\Users\User\Desktop\mysite\.next\serverless\pages\_error.js:91:18
    at Object.<anonymous> (C:\Users\User\Desktop\mysite\.next\serverless\pages\_error.js:94:10)
    at Module._compile (internal/modules/cjs/loader.js:1133:30)       
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1153:10)
info  - Collecting page data .npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! client@0.1.0 build: `next build`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the client@0.1.0 build 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!     C:\Users\User\AppData\Roaming\npm-cache\_logs\2020-09-12T19_38_11_418Z-debug.log

What is the meaning of this error?

Error: package.json does not exist at C:\package.json

And what about all the Module not found: Can't resolve errors?

EDIT:
I have package.json in project folder.
when I run npm run dev it works just fine.
I don't know why he is complaining about C:\\package.json
(the project is not under C:\\ , but C:\\Users\\User\\Desktop\\mysite )

When you npm run it will run whatever it is inside the script section of package.json in the directory but for some reason, it is invoked in here. I think there are 2 possibilities.

1/Either your current package.json somehow looks for C:\\package.json and the error occurs

2/Your npm cache is including the C:\\package.json for some reason try npm cache clean or npm cache clean --force

I think when you read this:

npm ERR! This is probably not a problem with npm.

Probably don`t depend from Npm or nodejs. I supppose some modules are missing in this folder:

./node_modules/node-fetch/lib/index.js
Module not found: Can't resolve 'encoding' in 
'C:\Users\User\Desktop\mysite\node_modules\node-fetch\lib'

./node_modules/mongodb/lib/operations/connect.js
Module not found: Can't resolve 'mongodb-client-encryption' in 
'C:\Users\User\Desktop\mysite\node_modules\mongodb\lib\operations'

./node_modules/node-pre-gyp/lib/util/compile.js
Module not found: Can't resolve 'node-gyp' in 
'C:\Users\User\Desktop\mysite\node_modules\node-pre-gyp\lib\util'

./node_modules/node-pre-gyp/lib/util/compile.js
Module not found: Can't resolve 'npm' in 
'C:\Users\User\Desktop\mysite\node_modules\node-pre-gyp\lib\util'

I suggest to write in console:

npm install encoding -g
npm install mongodb-client-encryption -g
npm install node-gyp -g
npm install npm@latest -g

and change folder into "C:\\Users\\User\\Desktop\\mysite" then type:

npm install encoding --save
npm install mongodb-client-encryption --save
npm install node-gyp --save
npm install npm@latest --save

And another thing, did you do "npm init"? To initialize your folder then edit it.

I had this error, my solution was:

First, in the packege.json file, in the section scripts, we must modify the property “build”: "next build && next export", and add a new property "export": "next export"

Second, we must establish the component and data folders in the root of the project.

Third: use the command: npm dev build. That gives to you a folder with the name "out".

package.json out folder

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM