运行babel脚本时,node.js无法找到模块

时间:2018-03-29 02:33:01

标签: javascript node.js express ecmascript-6 babeljs

在我运行我的开发脚本时,我的项目目录出错了。我假设这是由于babel转换,但我不知道还有什么可以调试这个问题。我已经删除并重新安装了我的节点模块,重新启动了我的服务器,并确保babel-node位于node_modules目录中。我不明白为什么在项目目录上抛出错误。 我的项目结构如下:

/server
 - package.json
 - /node_modules
 - /src
    - server.js
    - /routes
    - /models
    - /controllers
    - /etc....

运行yarn dev

时抛出的错误
➜ yarn dev
yarn run v1.5.1
$ nodemon -w src --exec "babel-node src --presets env,stage-0"
[nodemon] 1.17.2
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: /Users/altrock/Desktop/work/app/server/src/**/*
[nodemon] starting `babel-node src --presets env,stage-0`
module.js:557
    throw err;
    ^

Error: Cannot find module '/Users/altrock/Desktop/work/app/server/src'
    at Function.Module._resolveFilename (module.js:555:15)
    at Function.Module._load (module.js:482:25)
    at Function.Module.runMain (module.js:701:10)
    at Object.<anonymous> (/Users/altrock/Desktop/work/app/server/node_modules/babel-cli/lib/_babel-node.js:154:22)
    at Module._compile (module.js:660:30)
    at Object.Module._extensions..js (module.js:671:10)
    at Module.load (module.js:573:32)
    at tryModuleLoad (module.js:513:12)
    at Function.Module._load (module.js:505:3)
    at Function.Module.runMain (module.js:701:10)
[nodemon] app crashed - waiting for file changes before starting...

package.json with dev script and dependencies

{
  "name": "reactapp",
  "version": "0.0.0",
  "private": true,
  "presets": [
    "env"
  ],
  "scripts": {
    "dev": "nodemon -w src --exec \"babel-node src --presets env,stage-0\"",
    "build": "babel src -s -D -d dist --presets env,stage-0",
    "start": "node src/app.js"
  },
  "dependencies": {
    "archiver": "^2.1.1",
    "body-parser": "~1.18.2",
    "cookie-parser": "~1.4.3",
    "debug": "^3.1.0",
    "express": "^4.16.2",
    "fs": "0.0.1-security",
    "mocha": "^5.0.0",
    "mongoose": "^5.0.3"
  },
  "devDependencies": {
    "babel-cli": "^6.26.0",
    "babel-core": "^6.26.0",
    "babel-preset-env": "^1.6.1",
    "babel-preset-stage-0": "^6.24.1",
    "eslint": "^4.9.0",
    "nodemon": "^1.12.1"
  }
}

1 个答案:

答案 0 :(得分:0)

我重新安装了babel模块并密切关注他们的文档。我不得不将babel-node直接指向server.js文件。

“nodemon -w src --exec babel-node src / server.js --presets env,stage-0”