简体   繁体   English

Babel无法找到模块核心/启动Node.js

[英]Babel cannot find module core/splash Node.js

I have a Node.js project written by another developer that I am having trouble executing due to Babel being unable (probably) to trace correct paths for module imports: 我有一个由其他开发人员编写的Node.js项目,由于Babel无法(可能)无法跟踪模块导入的正确路径,因此我在执行时遇到了麻烦:

There is a line in the main.js that reads import Splash from 'core/splash' . main.js中有一行import Splash from 'core/splash'读取import Splash from 'core/splash' The project's main.js file is located in path "project_name/webapp/core/main.js" The "project_name/webapp/core/" directory also contains a file named splash.js but as far as I understand it is not a package since there is no package.json associated with it. 该项目的main.js文件位于路径"project_name/webapp/core/main.js""project_name/webapp/core/"目录还包含一个名为splash.js的文件,但据我了解,它不是软件包因为没有关联的package.json。 Am I right to assume that the import line above is trying to load this file but is failing due to it not being a package or is it simply because I am not understanding the directory structure correctly? 我是否可以假设上面的导入行正在尝试加载此文件,但是由于它不是软件包而导致失败,或者仅仅是因为我没有正确理解目录结构?
I am executing the main.js using command babel-node .\\webapp\\core\\main.js 我执行main.js使用命令babel-node .\\webapp\\core\\main.js

The exact error I am receiving is: 我收到的确切错误是:

Error: Cannot find module 'core/splash'
    at Function.Module._resolveFilename (module.js:547:15)
    at Function.Module._load (module.js:474:25)
    at Module.require (module.js:596:17)
    at require (internal/module.js:11:18)

Node.js version v8.11.3, babel-cli version ^6.26.0, Babel installed globally. Node.js版本v8.11.3,babel-cli版本^ 6.26.0,全球安装的Babel。

If you specify an absolute path in your import statement (ie core/splash ), Node.js will assume you are referencing an npm module in the node_modules directory. 如果在import语句中指定了绝对路径(即core/splash ),则Node.js将假定您在node_modules目录中引用了npm模块。

In this example, it will look for a module named core that contains a file splash.js . 在此示例中,它将查找名为core的模块,该模块包含文件splash.js

If you want to import a module that is part of your own source code (not installed from the npm registry), use a relative path instead. 如果要导入属于自己的源代码(不是从npm注册表中安装)的模块,请改用相对路径。

If main.js and splash.js are located in the same directory core , the correct import statement is: 如果main.jssplash.js位于同一目录core ,则正确的import语句是:

import Splash from './splash'

The leading ./ tells Node.js: “This is a relative path, starting in the directory of the current module.” 前导./告诉Node.js:“这是一个相对路径,从当前模块的目录开始。”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM