Closed its-dibo closed 3 years ago
If you're using webpack, please try adding sharp to externals
- see https://webpack.js.org/configuration/externals/
I tried config.externals.push("sharp");
, but got ReferenceError: sharp is not defined
I already use webpack-node-externals to add all node_modules to webpack.externals
also I see a .node
file have been created in dist folder
@lovell
You'll probably need to remove the use of node-loader
when using externals
.
after removing node-loader
I got this error
D:\Downloads\__projects\dev\NodeJs\@eng-dibo\ngx\dist\cms\core\server\express.js:149906
throw new Error(error);
^
Error:
Something went wrong installing the "sharp" module
Module parse failed: Unexpected character '�' (1:2)
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
(Source code omitted for this binary file)
- Remove the "node_modules/sharp" directory then run
"npm install --ignore-scripts=false --verbose" and look for errors
- Consult the installation documentation at https://sharp.pixelplumbing.com/install
- Search for this error at https://github.com/lovell/sharp/issues
at Object.EmBj (D:\Downloads\__projects\dev\NodeJs\@eng-dibo\ngx\dist\cms\core\server\express.js:149906:9)
at __webpack_require__ (D:\Downloads\__projects\dev\NodeJs\@eng-dibo\ngx\dist\cms\core\server\express.js:27:30)
at Object.Tbcg (D:\Downloads\__projects\dev\NodeJs\@eng-dibo\ngx\dist\cms\core\server\express.js:251757:15)
at __webpack_require__ (D:\Downloads\__projects\dev\NodeJs\@eng-dibo\ngx\dist\cms\core\server\express.js:27:30)
at Module.dMKD (D:\Downloads\__projects\dev\NodeJs\@eng-dibo\ngx\dist\cms\core\server\express.js:324399:63)
at __webpack_require__ (D:\Downloads\__projects\dev\NodeJs\@eng-dibo\ngx\dist\cms\core\server\express.js:27:30)
at Module.mWHM (D:\Downloads\__projects\dev\NodeJs\@eng-dibo\ngx\dist\cms\core\server\express.js:405599:77)
at __webpack_require__ (D:\Downloads\__projects\dev\NodeJs\@eng-dibo\ngx\dist\cms\core\server\express.js:27:30)
at Module.WXK/ (D:\Downloads\__projects\dev\NodeJs\@eng-dibo\ngx\dist\cms\core\server\express.js:271382:65)
at __webpack_require__ (D:\Downloads\__projects\dev\NodeJs\@eng-dibo\ngx\dist\cms\core\server\express.js:27:30)
at D:\Downloads\__projects\dev\NodeJs\@eng-dibo\ngx\dist\cms\core\server\express.js:118:18
at Object.<anonymous> (D:\Downloads\__projects\dev\NodeJs\@eng-dibo\ngx\dist\cms\core\server\express.js:121:10)
←[90m at Module._compile (internal/modules/cjs/loader.js:1156:30)←[39m
←[90m at Object.Module._extensions..js (internal/modules/cjs/loader.js:1176:10)←[39m
←[90m at Module.load (internal/modules/cjs/loader.js:1000:32)←[39m
←[90m at Function.Module._load (internal/modules/cjs/loader.js:899:14)←[39m
←[90m at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12)←[39m
←[90m at internal/main/run_main_module.js:18:47←[39m
It doesn't look like externals
has been configured correctly.
There are plenty of examples of others who have got sharp working with webpack, and most relate to incorrect webpack configuration.
https://github.com/lovell/sharp/search?q=webpack+externals&type=issues
I don't have problems with externals, it already execluse node_modules (including sharp) and it generates a .node
file in the dist folder, so webpack does it's job.
I think the problem is with process.dlopen
in the output file:
process.dlopen(module, __dirname + "/" + __webpack_require__.p + "sharp.node");
and sharp.node
exists in the same output dir
OS: windows 8.1
dependency walker result for the output file sharp.node https://www.mediafire.com/file/osekegnuq9cuzk4/sharp.dwi/file
The initial error message suggests webpack is looking for a file named 33c1e48f049ce3285905e286d212a201.node
rather than sharp.node
.
D:\Downloads__projects\dev\NodeJs\@eng-dibo\ngx\dist\cms\core\server/33c1e48f049ce3285905e286d212a201.node
Please provide a complete, minimal repo with a package.json
and webpack configuration that enables someone else to reproduce this problem.
yes, but this file exists.
for clarification: node-loader plugin chnges file name to [hash].[ext]
and the output file searches for it, soit is not the problem @lovell
Please provide a complete, minimal repo with a package.json
and webpack configuration that uses externals
but does not use node-loader
to enable someone else to reproduce this problem.
repo: https://github.com/eng-dibo/ngx
the base webpack config file is located in the workspace's root.
the issue occures when you build:server of the project 'cms' to do that, run the following command from the workspace's root.
npm run build:server:dev --prefix projects/cms
additional info: I installed sharp on windows 8 with this cmd:
npm i sharp
I will try the following, may this help
npm install --arch=x64 --platform=linux sharp
It looks like node-loader
is still present - https://github.com/eng-dibo/ngx/blob/master/webpack.config.js#L43 - please remove this.
additional info:
I installed sharp on windows 8.1 using the following cmd:
npm i sharp
I will try reinstall using
rmdir /s /q node_modules/sharp
npm install --arch=x64 --platform=linux sharp
may it help.
source: https://sharp.pixelplumbing.com/install (windows)
removing node-loader
causes the error already mentioned above.
https://github.com/lovell/sharp/issues/2517#issuecomment-754901799
I will try reinstall using
rmdir /s /q node_modules/sharp npm install --arch=x64 --platform=linux sharp
may it help.
this didn't help. error: sharp.node is not a valid Win32 application
also npm install --arch=x32 --platform=linux sharp
and npm install --arch=x32 sharp
didn't help.
Are you using Windows x64 at runtime, or just install time? There are a lot of moving parts here so I will reiterate my plea: please provide a complete, minimal repo with a package.json
and webpack configuration that uses externals
but does not use node-loader
to enable someone else to reproduce this problem.
I added webpack config may it help. working in creating a minimal repo with angular project.
this issue is specially occures with angular, I just created a brand new angular project and enabled universal app.
minimal reproduction: https://github.com/eng-dibo/ng-sharp-issue
steps to run this reproduction:
1- clone this repo https://github.com/eng-dibo/ng-sharp-issue
2- inatsll packages npm i
3- build the server npm run build:server:dev
-> {optional} to build the whole project (browser & server) and serve it npm run start
-> the script build:ssr
has been renamed to build:server
or build:server:dev
for clarification.
this generates dist
dir contains main.js
and sharp-*.node
files
4- run the server node dist/main
error:
Error:
Something went wrong installing the "sharp" module
node-loader:
Error: The specified module could not be found.
//sharp-ac5709806d227d1d966f7f0b76814d78.node
if we removed node-loader
from webpack config, we will get error: Unexpected character '�' (1:2)
as mentioned in the detailed "steps of creating this repo" step-by-step below.
steps of creating this reproduction: (for reference)
1- create new angular project:
ng new ngx
ng serve -o //<-- success
2- enable universal app
ng add @nguniversal/express-engine
npm run build:ssr & npm run serve <--success
3- add sharp to server.ts
//server.ts
import * as sharp from "sharp"
sharp('./image.jpg')
npm run build:ssr <-- faild, error: Unexpected character '�' (1:2)
4- use node-loader to load .node file (inside sharp package), we need to use custom webpack to modify webpack configuration in Angular
npm i -D @angular-builders/custom-webpack node-loader ts-loader
//angular.json
{
...
server:{
"builder": "@angular-builders/custom-webpack:server",
"options": {
"customWebpackConfig": {
"path": "./webpack.server.config.js"
}
}
}
}
//webpack.server.config.js (simplified)
module.exports = (config, options) => {
//remove webpack config from Angular, except 'resolve', and add rules (ts-loader, node-loader)
config = {
mode: "development",
resolve: { extensions: [".ts"] },
entry: { main: ["./server.ts"] },
target: "node",
stats: { colors: false },
module: {
rules: [
{ test: /\.ts$/, loader: "ts-loader" },
{
test: /\.node$/,
loader: "node-loader",
options: { name: "[name]-[contenthash].[ext]" }
}
]
}
};
return config;
}
npm run build:ssr <-- faild
This sample appears to use node-loader
rather than externals
. It looks node-loader
only copies and renames the node_modules/sharp/build/Release/sharp.node
file, ignoring all the other files in node_modules/sharp
.
Please can you try using externals
as requested.
I already use externals
, in addition, I already tested building the server with and without node-loader
as mentioned above in detailed step-by-step explanation.
both cases cause errors
with node-loader:
build command runs successfully, and it generates dist
folder contains main.js
and sharp-*.node
files
but the error occures when running node dist/main
, because process.dlopen()
fails.
without node-loader:
build command fails with error: Unexpected character '�' (1:2)
I already use externals
https://github.com/eng-dibo/ng-sharp-issue/search?q=externals returns zero results. Am I missing something?
Applying the following diff appears to work:
--- a/webpack.server.config.js
+++ b/webpack.server.config.js
@@ -7,14 +7,12 @@ module.exports = (config, options) => {
},
target: "node",
stats: { colors: false },
+ externals: {
+ 'sharp': 'commonjs sharp',
+ },
module: {
rules: [
{ test: /\.ts$/, loader: "ts-loader" },
- {
- test: /\.node$/,
- loader: "node-loader",
- options: { name: "[name]-[contenthash].[ext]" }
- }
]
}
};
the issue is probably caused by webpack-node-externals
.
it faild to add node modules to webpack.resolve.externals
, because it resolves the path to 'package.json' file (or node_module) wrongly in case of building the webpack using npm --prefix
flag.
I created an issue to mention this problem https://github.com/liady/webpack-node-externals/issues/96
Glad you worked it out, I've added a small section to the docs about the recommend approach that most people use - see https://github.com/lovell/sharp/commit/0dc325daa4a5b15bdbd1d6ff2375e0d18b4380c7
I'm not using webpack, and I'm still having this problem. I have: sharp: 0.26.2 OS: macOS 11.0.1 (Not M1) Node: 14.15.5 npm: 7.5.4
Something went wrong installing the "sharp" module
Cannot find module '../build/Release/sharp.node'
@nph278 Please open a new installation issue and answer all of the questions - https://github.com/lovell/sharp/issues/new?labels=installation&template=installation.md
What fixed it for me was upgrading to sharp
version 0.30.1.
To help other people figure this out. In webpack.config.js
externals: ['node_modules', 'sharp']
This will not work, and you will get error:
ReferenceError: sharp is not defined
Only this will help (at least in my case)
externals: ['node_modules', {
'sharp': 'commonjs sharp'
}]
I hope I was helpful for someone
update: the issue is probably caused by webpack-node-externals. https://github.com/lovell/sharp/issues/2517#issuecomment-772827876
I use
sharp
but I got this error:minimal reproduction: https://github.com/lovell/sharp/issues/2517#issuecomment-767703678
I removed
sharp
from 'node_modules` then reinstalled it, but got the same errorthe verbose output of installing sharp is:
I added
node-loader
to webpack:webpack config:
all node_modules are added to webpack.externals OS: windows 8.1