I'm trying to create a docker image that would compile and run an application from my monorepo. I'm using ESBuild to bundle and compile all the dependencies in a single file, this process works fine when I run the bundle command manually but it fails when it's ran by Docker.
The reported error is about the other workspaces the application depends on, this is an example:
#10 1.443 ✘ [ERROR] Could not resolve "#platform/bar"
#10 1.443
#10 1.443 src/app/logger.ts:1:29:
#10 1.443 1 │ import { createBar } from '#platform/bar'
#10 1.443 ╵ ~~~~~~~~~~~~~~~
#10 1.443
#10 1.443 You can mark the path "#platform/bar" as external to exclude it from the bundle, which will remove this error.
Dockerfile
FROM node:18
ENV NODE_ENV=production
COPY . /app
WORKDIR /app
RUN yarn install
RUN yarn workspace #platform/foo bundle
WORKDIR /platform/packages/apps/foo/dist
EXPOSE 8181
CMD ["node", "app.js"]
package.json
{
"name": "#platform/foo",
"scripts": {
"bundle": "node bundle.mjs"
},
"dependencies": {
"#platform/bar": "workspace:*",
"#platform/xyz": "workspace:*"
}
}
bundle.mjs
import * as esbuild from 'esbuild'
import { copy } from 'esbuild-plugin-copy'
await esbuild.build({
entryPoints: ['src/main.ts'],
bundle: true,
outfile: 'dist/app.js',
plugins: [
copy({
resolveFrom: 'cwd',
verbose: true,
assets: [
{
from: ['../../../node_modules/couchbase/build/**/*.node'],
to: ['./dist'],
},
],
}),
],
platform: 'node',
target: 'node18',
loader: {
'.node': 'copy',
},
})
Related
Current Behavior
When I run lerna publish, it gets stuck after packaging the first package. This is where it gets stuck:
lerna WARN ENOLICENSE One way to fix this is to add a LICENSE.md file to the root of this repository.
lerna WARN ENOLICENSE See https://choosealicense.com for additional guidance.
(#########⠂⠂⠂⠂⠂⠂⠂⠂⠂) ⠏ publish: verb packed packages/react
This line particularly:
(#########⠂⠂⠂⠂⠂⠂⠂⠂⠂) ⠏ publish: verb packed packages/react
Expected Behavior
It should publish to NPM successfully
Failure Logs / Configuration
lerna.json
{
"packages": [
"packages/*",
"docs"
],
"version": "independent",
"stream": true,
"hoist": true,
"command": {
"bootstrap": {
"npmClientArgs": ["--no-package-lock"]
},
"publish": {
"ignoreChanges": ["**/stories/**", "**/tests/**"]
}
}
}
Environment
System:
OS: macOS 12.3
CPU: (8) arm64 Apple M1 Pro
Binaries:
Node: 14.21.2 - ~/.nvm/versions/node/v14.21.2/bin/node
npm: 6.14.17 - ~/.nvm/versions/node/v14.21.2/bin/npm
Utilities:
Git: 2.32.1 - /usr/bin/git
npmPackages:
lerna: ^5.6.2 => 5.6.2
After upgrading Angular version of my app to 12, I decided to upgrade other libraries like jest as well:
"jest": "^27.4.6", (from 24.1.0)
"jest-canvas-mock": "^2.2.0",
"jest-junit": "^13.0.0", (from ^9.0.0)
"jest-preset-angular": "11.0.1",
"jest-resolve": "^27.4.6", (added this one)
"ts-jest": "27.1.2" (from 24.0.0)
This is how my jest.config.js looks like:
const jestPresetAngularSerializers = require('jest-preset-angular/build/serializers');
module.exports = {
name: 'sam-web',
preset: '../../jest.config.js',
coverageDirectory: '../../coverage/apps/sam-web',
reporters: [
'default',
[
'jest-junit',
{
outputDirectory: 'reports',
outputName: 'junit-sam-web.xml',
},
],
],
snapshotSerializers: [jestPresetAngularSerializers, 'jest-preset-angular/build/serializers/html-comment'],
transformIgnorePatterns: ['/node_modules/(?!lodash-es).+\\.js$'],
collectCoverageFrom: ['src/app/**/*.ts', '!src/app/**/*.module.ts'],
cacheDirectory: '../../.jest-cache',
};
I'm also using import 'jest-preset-angular/setup-jest'; instead of import 'jest-preset-angular';
This is my app things and their versions:
Angular CLI: 12.2.14
Node: 14.15.0
Package Manager: yarn 1.22.17
OS: win32 x64
Package Version
------------------------------------------------------------
#angular-devkit/architect 0.801.1
#angular-devkit/build-angular 12.2.14
#angular-devkit/core 8.1.1
#angular-devkit/schematics 8.1.1
#angular/cdk 12.2.13
#angular/cli 12.2.14
#angular/material 12.2.13
#angular/material-moment-adapter 12.2.13
#schematics/angular 12.2.14
rxjs 7.4.0
typescript 4.3.5
And this is the error what I get when I'm trying to run the tests:
An unhandled exception occurred: Package subpath './build/defaultResolver' is not defined by "exports" in C:\Users\Dominik\Desktop\Work\web_gui\node_modules\jest-resolve\package.json
See "C:\Users\Dominik\AppData\Local\Temp\ng-9Gl1h1\angular-errors.log" for further details.
error Command failed with exit code 127.
I have tried to remove node_module and yarn.lock, clean cache, install everything one more time but every time is the same. I found a similar issue on some page where the solution was to run npm update but similar command for yarn doesn't help me. Maybe someone of you can help me?
i have to create a module to use on my application, but when I import that in my major project i have this error
Error: Cannot find module '.../node_modules/Table/dist/index.js'. Please verify that the package.json has a valid "main" entry
I have not the folder dist/index.js, in the guide I followed I did not see that it was necessary to make the package, also in another project i did not have these problems. My version of node is v12.16.2
I update my rollup to build with npm run dev
Here there is my new rollup
import svelte from 'rollup-plugin-svelte';
import resolve from 'rollup-plugin-node-resolve';
const pkg = require('./package.json');
export default {
input: 'src/Table.svelte',
output: {
file: `dist/bundle.js`,
format: "iife"
},
plugins: [
svelte(),
resolve({ preferBuiltins: true, mainFields: ['browser'] })
],
external: ['uuid', 'object-exporter']
};
But i have again this error
internal/modules/cjs/loader.js:327
throw err;
^
Error: Cannot find module '/../../node_modules/Table/dist/index.js'. Please verify that the package.json has a valid "main" entry
Your rollup config build your package in dist/bundle.js:
output: {
file: `dist/bundle.js`,
format: "iife"
},
But you probably defined in your package.json:
"main": "dist/index.js",
But dist/index.js doesn't exits. Just replace it with "main": "dist/bundle.js".
I'm making an NPM package in TypeScript, and wanted to know how I can make it available for ES and Node modules.
I've set it up with Rollup and a few configs:
rollup.config.js
export default {
input: 'build/kimp.js', // built from TS
output: [
{
file: 'dist/main/kimp.js',
format: 'es',
strict: false,
name: 'module',
banner: `#! /usr/bin/env node - Copyright 2020 Herbie Vine - Updated: ${new Date()}`
},
{
file: 'dist/module/kimp.js',
format: 'umd',
strict: false,
name: 'common',
banner: `#! /usr/bin/env node - Copyright 2020 Herbie Vine - Updated: ${new Date()}`
}
],
plugins: [
terser(),
resolve(),
json(),
commonjs({
include: 'node_modules/**'
})
],
external: [
'crypto'
]
};
package.json
{
"name": "kimp",
"version": "1.0.0",
"description": "Lightweight ID generator",
"sideEffects": false,
"main": "dist/main/kimp.js", // import() - es6
"module": "dist/module/kimp.js", // require() - node
"scripts": {
"build": "tsc -p ./src/tsconfig.json",
"rollup": "rollup -c"
},
"publishConfig": {
"registry": "https://npm.pkg.github.com/"
},
"keywords": [...],
"repository": {...},
"author": "Herbie Vine",
"license": "MIT",
"bugs": {...},
"homepage": "https://github.com/herbievine/kimp#readme",
"devDependencies": {...}
}
I tried using it in an express app, but I get an error:
const { kimp } = require('kimp');
console.log(kimp)
------
C:\Users\**\kimp-ts\dist\main\kimp.js:3484
export { kimp };
^^^^^^
This is coming from the built version for es modules
basic gist on github
Am I wrong to believe that when node requires a package, it looks at the module key in package.json. Anyways I've been at it for hours, any help would mean a lot cheers 👍
Using rollup you have compiled in to ESModules as you have specified format: 'es' in your rollup.config.js. Nodejs uses commonjs modules and require is supposed to import the commonjs module which it couldn't find there and hence you are getting error. Nodejs started shipping experimental support for ES modules starting node version 10.
If you have greater than version node 10 you can just update your express server start script in package.json to allow the experimental modules support for instance: "start": "node --experimental-modules server.js".
Other approaches that can work depending on your liking or requirements:
Use the third party #std/esm to compile and use es modules as commonjs modules
Compile your library in commonjs modules via this rollup plugin
Edit: It seems the above code in question had issue in config and which fixed the issue, main and module entries in package.json were declared other way around and had to swap the entries to set them up correctly. Main should actually point to umd and module should point to es modules.
npm i -g phantom
/
weak#0.4.1 install C:\Users\muzo0\AppData\Roaming\npm\node_modules\phantom\node_modules\dnode\node_modules\weak
node-gyp rebuild
C:\Users\muzo0\AppData\Roaming\npm\node_modules\phantom\node_modules\dnode\node_modules\weak>if
not defined npm_config_n ode_gyp (node
"C:\DevLibs\nodejs\node_modules\npm\bin\node-gyp-bin\....\node_modules\node-gyp\bin\node-gyp.js"
rebuild ) else (rebuild) Building the projects in this solution one
at a time. To enable parallel build, please add the "/m" switch.
weakref.cc
Creating library C:\Users\muzo0\AppData\Roaming\npm\node_modules\phantom\node_modules\dnode\node_modules\weak\buil
d\Release\weakref.lib and object
C:\Users\muzo0\AppData\Roaming\npm\node_modules\phantom\node_modules\dnode\node_modu
les\weak\build\Release\weakref.exp Generating code Finished
generating code weakref.vcxproj ->
C:\Users\muzo0\AppData\Roaming\npm\node_modules\phantom\node_modules\dnode\node_modules\weak\build
\Release\weakref.node phantom#0.7.2
C:\Users\muzo0\AppData\Roaming\npm\node_modules\phantom ├──
win-spawn#2.0.0 ├── traverse#0.6.6 ├── shoe#0.0.15 (sockjs#0.3.7) └──
dnode#1.2.1 (jsonify#0.0.0, dnode-protocol#0.2.2, weak#0.4.1)
webpack.config.js
module.exports = {
entry: './src/main.js',
output: {
filename: 'dist.js'
},
module: {
loaders: [
{ test: /\.js$/, loader: 'babel', exclude: /node_modules/ },
{ test: /\.json$/, loader: 'json'}
]
},
target: "node"
};
webpack --hot
Hash: d94ab9a8045078efe92c Version: webpack 1.10.0 Time: 2807ms
Asset Size Chunks Chunk Names dist.js 1.28 MB
0 [emitted] main
+ 180 hidden modules
WARNING in ./~/phantom/~/dnode/~/weak/~/bindings/bindings.js Critical
dependencies: 76:22-40 the request of a dependency is an expression
76:43-53 the request of a dependency is an expression #
./~/phantom/~/dnode/~/weak/~/bindings/bindings.js 76:22-40 76:43-53
WARNING in ./~/phantom/~/shoe/~/sockjs/lib/utils.js Module not found:
Error: Cannot resolve module 'rbytes' in
c:\Users\muzo0\Desktop\new_tele_bot\node_modules\phantom\node
_modules\shoe\node_modules\sockjs\lib # ./~/phantom/~/shoe/~/sockjs/lib/utils.js 7:13-30
WARNING in ./~/phantom/~/dnode/~/weak/~/bindings/README.md Module
parse failed:
c:\Users\muzo0\Desktop\new_tele_bot\node_modules\phantom\node_modules\dnode\node_modules\weak\node_
modules\bindings\README.md Line 2: Unexpected token === You may need
an appropriate loader to handle this file type. | node-bindings |
============= | ### Helper module for loading your native module's .node file | # ./~/phantom/~/dnode/~/weak/~/bindings ^./.*$
node dest.js
module.js:338
throw err;
^ Error: Cannot find module 'c:\Users\muzo0\Desktop\new_tele_bot\dest.js'
at Function.Module._resolveFilename (module.js:336:15)
at Function.Module._load (module.js:278:25)
at Function.Module.runMain (module.js:501:10)
at startup (node.js:129:16)
at node.js:814:3
c:\Users\muzo0\Desktop\new_tele_bot>phantomjs
'phantomjs' is not recognized as an internal or external command,
operable program or batch file.
main.js
import phantom from 'phantom';
OS: win 10
npm: 2.7.4