我正在制作一個 Next Js 應用程式,它在本地 npm 包中包含貓鼬模型,因此可以與后端的其他部分共享它們。但我收到這些錯誤:
Module not found: Can't resolve 'supports-color' in '/Users/akopyl/Projects/my-project/shared-stuff/node_modules/debug/src'
../shared-stuff/node_modules/mongodb/lib/bson.js
Module not found: Can't resolve 'bson-ext' in '/Users/akopyl/Projects/my-project/shared-stuff/node_modules/mongodb/lib'
../shared-stuff/node_modules/mongodb/lib/deps.js
Module not found: Can't resolve 'kerberos' in '/Users/akopyl/Projects/my-project/shared-stuff/node_modules/mongodb/lib'
../shared-stuff/node_modules/mongodb/lib/deps.js
Module not found: Can't resolve 'snappy' in '/Users/akopyl/Projects/my-project/shared-stuff/node_modules/mongodb/lib'
../shared-stuff/node_modules/mongodb/lib/deps.js
Module not found: Can't resolve 'snappy/package.json' in '/Users/akopyl/Projects/my-project/shared-stuff/node_modules/mongodb/lib'
../shared-stuff/node_modules/mongodb/lib/deps.js
Module not found: Can't resolve 'aws4' in '/Users/akopyl/Projects/my-project/shared-stuff/node_modules/mongodb/lib'
../shared-stuff/node_modules/mongodb/lib/encrypter.js
Module not found: Can't resolve 'mongodb-client-encryption' in '/Users/akopyl/Projects/my-project/shared-stuff/node_modules/mongodb/lib'
我的猜測是,這是由于 webpack 試圖在目錄中而不是目錄中查找shared-stuff包的依賴項引起的。my-project/appmy-project/shared-stuff
專案檔案結構:
my-project
├── app (This is the Next Js app that has errors)
│ ├── lib
│ ├── next.config.js
│ ├── node_modules
│ ├── package-lock.json
│ ├── package.json
│ ├── pages
│ ├── public
│ └── styles
├── gather
│ ├── ecosystem.config.js
│ ├── node_modules
│ ├── package-lock.json
│ ├── package.json
│ └── src
└── shared-stuff
├── index.js
├── models
├── node_modules
├── package-lock.json
└── package.json
app/package.json檔案:
{
"private": true,
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start"
},
"dependencies": {
"mongoose": "^6.2.6",
"next": "latest",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"sass": "^1.49.9",
"shared-stuff": "file:../shared-stuff"
},
"devDependencies": {
"eslint": "^8.11.0",
"eslint-config-next": "^12.1.0"
}
}
shared-stuff/package.json檔案:
{
"name": "shared-stuff",
"version": "1.0.0",
"main": "index.js",
"dependencies": {
"mongoose": "^6.2.6"
}
}
我注意到的一件有趣的事情是,雖然這兩個包都只是mongoose一個依賴項,但該shared-stuff/node_modules檔案夾包含一個mongodb檔案夾,但app/node_modules沒有。
目錄中的另一個包gather也有shared-stuff依賴項,但它按預期運行,因為它不使用 webpack。
我已經運行npm ci了 3 個專案中的每一個,嘗試禁用 eslint,嘗試使用webpack.IgnorePluginin next.config.jsto ignore shared-stuff,但沒??有成功。
uj5u.com熱心網友回復:
所以我找到的解決方案是將父目錄初始化my-project為 npm 專案本身,并將子檔案夾定義為其作業空間。
my-project $ npm init -y
my-project $ npm install
然后將以下行添加到my-project/package.json:
"workspaces": ["./app", "./gather", "./shared-stuff"]
在這些步驟之后,本地檔案夾包的依賴關系被正確決議。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/444958.html
標籤:javascript npm 网页包 下一个.js
下一篇:3rd方庫的Webpack問題
