Javascript SyntaxError:无法在heroku上的模块外部使用导入语句
我使用babel捆绑文件,并运行捆绑文件以运行服务器。但是,我在heroku上遇到一个错误“SyntaxError:无法在模块外使用导入语句”。我正在使用Javascript SyntaxError:无法在heroku上的模块外部使用导入语句,javascript,node.js,heroku,babeljs,Javascript,Node.js,Heroku,Babeljs,我使用babel捆绑文件,并运行捆绑文件以运行服务器。但是,我在heroku上遇到一个错误“SyntaxError:无法在模块外使用导入语句”。我正在使用@babel/cli,@babel/core,@babel/node,@babel/preset env和babel插件模块解析器。我的babelrc如下所示 { "presets": [ "@babel/preset-env" ], "plugins": [
@babel/cli
,@babel/core
,@babel/node
,@babel/preset env
和babel插件模块解析器
。我的babelrc如下所示
{
"presets": [
"@babel/preset-env"
],
"plugins": [
"graphql-tag"
]
}
这是我在package.json上的脚本
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"dev": "env-cmd -f ./dev/.env nodemon --exec babel-node app/server.js",
"syncGraphql": "rsync -a --prune-empty-dirs --include 'app/graphql/*' --include '*.graphql' 'app/graphql' 'dist'",
"build": "babel app/ -d dist && npm run syncGraphql",
"staging": "env-cmd -f ./staging/.env nodemon --exec babel-node app/server.js",
"start:dev": "cross-env NODE_ENV=production nodemon --exec babel-node app/server.js",
"start": "cross-env NODE_ENV=production node dist/server.js"
},
"engines": {
"node": "14.x"
},
{
"name": "api-server",
"version": "0.0.1",
"description": "Description",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"clean": "rm -rf dist && mkdir dist",
"dev": "env-cmd -f ./dev/.env nodemon --exec babel-node app/server.js",
"syncGraphql": "rsync -a --prune-empty-dirs --include 'app/graphql/*' --include '*.graphql' 'app/graphql' 'dist'",
"build": "babel app/ -d dist && npm run syncGraphql",
"staging": "env-cmd -f ./staging/.env nodemon --exec babel-node app/server.js",
"start:dev": "cross-env NODE_ENV=production nodemon --exec babel-node app/server.js",
"start": "node dist/server.js"
},
"engines": {
"node": "14.x"
},
"dependencies": {
"apollo-errors": "^1.9.0",
"apollo-server-express": "^2.24.1",
"bcryptjs": "^2.4.3",
"better-module-alias": "^1.1.0",
"body-parser": "^1.19.0",
"cors": "^2.8.5",
"cross-env": "^7.0.3",
"dataloader": "^2.0.0",
"env-cmd": "^10.1.0",
"express": "^4.17.1",
"express-session": "^1.17.2",
"github-graphql-client": "^1.0.0",
"graphql": "^15.5.0",
"graphql-tools": "^7.0.5",
"helmet": "^4.6.0",
"http-status": "^1.5.0",
"isomorphic-git": "^1.8.2",
"jsonwebtoken": "^8.5.1",
"lodash": "^4.17.21",
"merge-graphql-schemas": "^1.7.8",
"mongoose": "^5.12.10",
"node-fetch": "^2.6.1",
"passport": "^0.4.1",
"passport-github2": "^0.1.12",
"passport-gitlab2": "^5.0.0"
},
"devDependencies": {
"@babel/cli": "^7.14.3",
"@babel/core": "^7.14.3",
"@babel/node": "^7.14.2",
"@babel/preset-env": "^7.14.2",
"axios": "^0.21.1",
"babel-plugin-graphql-tag": "^3.3.0",
"babel-plugin-module-resolver": "^4.1.0",
"eslint": "^7.26.0",
"eslint-config-airbnb-base": "^14.2.1",
"eslint-plugin-import": "^2.23.2",
"fs-extra": "^10.0.0",
"husky": "^6.0.0",
"jest": "^26.6.3",
"nodemon": "^2.0.7",
"xmlhttprequest": "^1.8.0"
}
}
我用两种方法发现了类似的问题。一个使用与我相同的babelrc,另一个使用package.json中的type:module
。但这样我就得到了其他问题,比如
internal/process/esm_loader.js:74
internalBinding('errors').triggerUncaughtException(
^
Error [ERR_MODULE_NOT_FOUND]: Cannot find module (.....)
运行脚本时没有问题开始:dev
。但是,我在heroku上运行捆绑文件时遇到了问题
这是package.json
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"dev": "env-cmd -f ./dev/.env nodemon --exec babel-node app/server.js",
"syncGraphql": "rsync -a --prune-empty-dirs --include 'app/graphql/*' --include '*.graphql' 'app/graphql' 'dist'",
"build": "babel app/ -d dist && npm run syncGraphql",
"staging": "env-cmd -f ./staging/.env nodemon --exec babel-node app/server.js",
"start:dev": "cross-env NODE_ENV=production nodemon --exec babel-node app/server.js",
"start": "cross-env NODE_ENV=production node dist/server.js"
},
"engines": {
"node": "14.x"
},
{
"name": "api-server",
"version": "0.0.1",
"description": "Description",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"clean": "rm -rf dist && mkdir dist",
"dev": "env-cmd -f ./dev/.env nodemon --exec babel-node app/server.js",
"syncGraphql": "rsync -a --prune-empty-dirs --include 'app/graphql/*' --include '*.graphql' 'app/graphql' 'dist'",
"build": "babel app/ -d dist && npm run syncGraphql",
"staging": "env-cmd -f ./staging/.env nodemon --exec babel-node app/server.js",
"start:dev": "cross-env NODE_ENV=production nodemon --exec babel-node app/server.js",
"start": "node dist/server.js"
},
"engines": {
"node": "14.x"
},
"dependencies": {
"apollo-errors": "^1.9.0",
"apollo-server-express": "^2.24.1",
"bcryptjs": "^2.4.3",
"better-module-alias": "^1.1.0",
"body-parser": "^1.19.0",
"cors": "^2.8.5",
"cross-env": "^7.0.3",
"dataloader": "^2.0.0",
"env-cmd": "^10.1.0",
"express": "^4.17.1",
"express-session": "^1.17.2",
"github-graphql-client": "^1.0.0",
"graphql": "^15.5.0",
"graphql-tools": "^7.0.5",
"helmet": "^4.6.0",
"http-status": "^1.5.0",
"isomorphic-git": "^1.8.2",
"jsonwebtoken": "^8.5.1",
"lodash": "^4.17.21",
"merge-graphql-schemas": "^1.7.8",
"mongoose": "^5.12.10",
"node-fetch": "^2.6.1",
"passport": "^0.4.1",
"passport-github2": "^0.1.12",
"passport-gitlab2": "^5.0.0"
},
"devDependencies": {
"@babel/cli": "^7.14.3",
"@babel/core": "^7.14.3",
"@babel/node": "^7.14.2",
"@babel/preset-env": "^7.14.2",
"axios": "^0.21.1",
"babel-plugin-graphql-tag": "^3.3.0",
"babel-plugin-module-resolver": "^4.1.0",
"eslint": "^7.26.0",
"eslint-config-airbnb-base": "^14.2.1",
"eslint-plugin-import": "^2.23.2",
"fs-extra": "^10.0.0",
"husky": "^6.0.0",
"jest": "^26.6.3",
"nodemon": "^2.0.7",
"xmlhttprequest": "^1.8.0"
}
}
尝试
.mjs
而不是.js
。但是.js在我运行纱线开发时工作。Heroku执行npm start
而不是npm start:dev
。因此,您应该首先确保本地npm start
工作。共享您的整个包.json
。它提供了有关依赖项和开发人员依赖项的信息。Heroku不安装开发依赖项。此外,共享您的整个构建。log@TinNguyen我已经共享了我的package.json,在构建(git-push-heroku-master)时没有问题。我附上了一个错误的截图。