如何将pm2与基于ES模块的包(类型:“模块”)结合使用,我在没有任何有用帮助的情况下研究了类似的问题(有人说它在windows上不起作用,但我使用的是linux)。
我总是收到错误:
Error [ERR_REQUIRE_ESM]: require() of ES Module /opt/app/server/lib/src/index.js not supported.
0|any| Instead change the require of index.js in null to a dynamic import() which is available in all CommonJS modules.我的ecosystem.config.js看起来像:
const os = require('os');
module.exports = {
apps: [{
port : 3000,
name : "any",
script : "lib/src/index.js",
watch : true,
instances : os.cpus().length,
exec_mode : 'fork',
env: {
NODE_ENV: "production",
}
}]
}index.js是使用“导入”语法的ES模块。我如何告诉pm2应该使用这种方式导入
发布于 2022-01-15 00:59:38
要实现这一点,您可以创建一个中间CommonJS模块,该模块从ESModule加载应用程序。可以使用公共‘s模块中的import函数。
这看起来是这样的:
ecosystem.config.jslib/src/index.cjs CommonJS入口点(用于PM2).lib/src/index.js ESModule入口点)(用于与ESM兼容的tools).lib/src/app.js应用程序代码.)
ecosystem.config.js
const os = require('os');
module.exports = {
apps: [{
port : 3000,
name : "any",
script : "lib/src/index.cjs", // CommonJS
watch : true,
instances : os.cpus().length,
exec_mode : 'fork',
env: {
NODE_ENV: "production",
}
}]
}lib/src/index.js
import {app} from './app.js'
app()lib/src/index.cjs
import('./app.js') // There is import function available in CommonJS
.then(({app}) => {
app()
})lib/src/app.js
import {hello} from './greet.js'
export function app() {
console.log(hello('World'))
}lib/src/greet.js
export function hello(name) {
return `Hello, ${name}!`
}发布于 2022-08-04 07:04:42
将ecosystem.config.js重命名为ecosystem.config.cjs为我工作
https://stackoverflow.com/questions/70450332
复制相似问题